试图使用JS突出显示文本

发布于 2025-01-21 10:08:17 字数 2273 浏览 4 评论 0原文

当页面滚动下面标记在下面,我正在尝试突出显示文本,从我在网上发现的内容上标记,代码是这样的。但是看来我做错了什么,我做不到。 请注意,我很新,刚刚开始学习网络开发。

(function(document) {
  const markers = [...document.querySelectorAll('mark')];

  const observer = new IntersectionObserver((entries) => {
    entries.forEach((entry) => {
      if (entry.intersectionRatio > 0) {
        entry.target.style.animationPlayState = 'running';
        observer.unobserve(entry.target);
      }
    });
  }, {
    threshold: 0.8,
  });

  markers.forEach((mark) => {
    observer.observe(mark);
  });
}(document));
.mark {
  --color1: springgreen;
  --color2: springgreen;
  --bg-height: 40%;
  all: unset;
  background-image: linear-gradient(var(--color1) var(--color2));
  background-position: 0 100%;
  background-repeat: no-repeat;
  background-size: 0 var(--bg-height);
  -webkit-animation: highlight 800ms 1 ease-out;
  animation: highlight 800ms 1 ease-out;
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
  -webkit-animation-play-state: paused;
  animation-play-state: paused;
}

@-webkit-keyframes highlight {
  to {
    background-size: 100% var(--bg-height);
  }
}

@keyframes highlight {
  to {
    background-size: 100% var(--bg-height);
  }
}
<main>
  <h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis quaerat quas asperiores ut numquam, quidem blanditiis sint explicabo qui amet reiciendis doloremque, minima illo. Repudiandae iste quis nihil itaque porro.</h2>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ut aut laudantium ullam! <mark>Tempore sapiente molestiae</mark> nam amet quaerat quisquam doloremque eveniet dolores? Laborum asperiores, obcaecati minima minus qui esse ab?</p>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Veniam sapiente ea alias unde qui mollitia earum pariatur nesciunt incidunt, <mark>tenetur nemo iure aspernatur maiores.</mark> Laboriosam placeat quae eos recusandae explicabo?</p>
</main>

I am trying to highlight a text when the page scrolls it gets marked underneath, and from what I've found on the net, the code goes like this. But it seems I am doing something wrong and I cannot get it right.
Please note I am very new and just started learning web-development.

(function(document) {
  const markers = [...document.querySelectorAll('mark')];

  const observer = new IntersectionObserver((entries) => {
    entries.forEach((entry) => {
      if (entry.intersectionRatio > 0) {
        entry.target.style.animationPlayState = 'running';
        observer.unobserve(entry.target);
      }
    });
  }, {
    threshold: 0.8,
  });

  markers.forEach((mark) => {
    observer.observe(mark);
  });
}(document));
.mark {
  --color1: springgreen;
  --color2: springgreen;
  --bg-height: 40%;
  all: unset;
  background-image: linear-gradient(var(--color1) var(--color2));
  background-position: 0 100%;
  background-repeat: no-repeat;
  background-size: 0 var(--bg-height);
  -webkit-animation: highlight 800ms 1 ease-out;
  animation: highlight 800ms 1 ease-out;
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
  -webkit-animation-play-state: paused;
  animation-play-state: paused;
}

@-webkit-keyframes highlight {
  to {
    background-size: 100% var(--bg-height);
  }
}

@keyframes highlight {
  to {
    background-size: 100% var(--bg-height);
  }
}
<main>
  <h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis quaerat quas asperiores ut numquam, quidem blanditiis sint explicabo qui amet reiciendis doloremque, minima illo. Repudiandae iste quis nihil itaque porro.</h2>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ut aut laudantium ullam! <mark>Tempore sapiente molestiae</mark> nam amet quaerat quisquam doloremque eveniet dolores? Laborum asperiores, obcaecati minima minus qui esse ab?</p>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Veniam sapiente ea alias unde qui mollitia earum pariatur nesciunt incidunt, <mark>tenetur nemo iure aspernatur maiores.</mark> Laboriosam placeat quae eos recusandae explicabo?</p>
</main>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

棒棒糖 2025-01-28 10:08:17

JS代码是对的,您只有2个错误,在CSS代码中,您错过了线性级别声明中的一个逗号,并且您正在设计.mark .mark类,您需要样式标记元素:

const markers = [...document.querySelectorAll('mark')];

const observer = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    if (entry.intersectionRatio > 0) {
      entry.target.style.animationPlayState = 'running';
      observer.unobserve(entry.target);
    }
  });
}, {
  threshold: 0.8,
});

markers.forEach((mark) => {
  observer.observe(mark);
});
p,
h2 {
  margin-bottom: 500px;
}

mark {
  --color1: springgreen;
  --color2: blue;
  --bg-height: 40%;
  all: unset;
  background-image: linear-gradient(var(--color1), var(--color2));
  background-position: 100% 100%;
  background-repeat: no-repeat;
  background-size: 0% var(--bg-height);
  animation: highlight 800ms 1 ease-out;
  animation-fill-mode: forwards;
  animation-play-state: paused;
}

@-webkit-keyframes highlight {
  to {
    background-size: 100% var(--bg-height);
  }
}

@keyframes highlight {
  to {
    background-size: 100% var(--bg-height);
  }
}
<main>
  <h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis quaerat quas asperiores ut numquam, quidem blanditiis sint explicabo qui amet reiciendis doloremque, minima illo. Repudiandae iste quis nihil itaque porro.</h2>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ut aut laudantium ullam! <mark>Tempore sapiente molestiae</mark> nam amet quaerat quisquam doloremque eveniet dolores? Laborum asperiores, obcaecati minima minus qui esse ab?</p>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Veniam sapiente ea alias unde qui mollitia earum pariatur nesciunt incidunt, <mark>tenetur nemo iure aspernatur maiores.</mark> Laboriosam placeat quae eos recusandae explicabo?</p>
</main>

如果您想对滚动事件进行更详细的控制,例如,您可能想恢复动画,添加其他行为等其他行为等...我建议您尝试 gsap scrolltrigger 插件事情要比直接操纵CSS和交叉观察者要容易得多。

The JS code is right, you just have 2 errors in CSS code, you miss a comma inside the linear-gradient declaration, and you are styling the .mark class, while you need to style the mark elements:

const markers = [...document.querySelectorAll('mark')];

const observer = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    if (entry.intersectionRatio > 0) {
      entry.target.style.animationPlayState = 'running';
      observer.unobserve(entry.target);
    }
  });
}, {
  threshold: 0.8,
});

markers.forEach((mark) => {
  observer.observe(mark);
});
p,
h2 {
  margin-bottom: 500px;
}

mark {
  --color1: springgreen;
  --color2: blue;
  --bg-height: 40%;
  all: unset;
  background-image: linear-gradient(var(--color1), var(--color2));
  background-position: 100% 100%;
  background-repeat: no-repeat;
  background-size: 0% var(--bg-height);
  animation: highlight 800ms 1 ease-out;
  animation-fill-mode: forwards;
  animation-play-state: paused;
}

@-webkit-keyframes highlight {
  to {
    background-size: 100% var(--bg-height);
  }
}

@keyframes highlight {
  to {
    background-size: 100% var(--bg-height);
  }
}
<main>
  <h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis quaerat quas asperiores ut numquam, quidem blanditiis sint explicabo qui amet reiciendis doloremque, minima illo. Repudiandae iste quis nihil itaque porro.</h2>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Ut aut laudantium ullam! <mark>Tempore sapiente molestiae</mark> nam amet quaerat quisquam doloremque eveniet dolores? Laborum asperiores, obcaecati minima minus qui esse ab?</p>
  <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Veniam sapiente ea alias unde qui mollitia earum pariatur nesciunt incidunt, <mark>tenetur nemo iure aspernatur maiores.</mark> Laboriosam placeat quae eos recusandae explicabo?</p>
</main>

If you want a more granular control over the scroll events, for example you might want to revert animations, add additional behaviours like staggering, etc... I suggest you to try the GSAP ScrollTrigger plugin, it makes things much easier than having to manipulate css and Intersection Observer directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文