【问题标题】:animating elements when you scroll down向下滚动时动画元素
【发布时间】:2021-02-17 19:17:00
【问题描述】:

我正在尝试向仅在向下滚动后才能查看的元素添加动画,我如何确保动画仅在用户向下滚动并可以查看它们之后才发生。最好使用 CSS 而不是 Javascript。

【问题讨论】:

标签: javascript html css css-animations


【解决方案1】:

没有 javascipt 就无法做到。

您可以使用 IntersectionObserver 来执行此操作。它主要用于延迟加载,但 你需要一小块才能观察你的卷轴。

var sections = document.querySelectorAll('section');

var options = {
  rootMargin: '0px',
  threshold: 0.25
}

var callback = (entries) => {
  entries.forEach((entry) => {
      var target = entry.target;
      console.log(target);
    if (entry.intersectionRatio >= 0.25) {
      target.classList.add("is-inview");
    } else {
      target.classList.remove("is-inview");
    }
  })
}

var observer = new IntersectionObserver(callback, options)
sections.forEach((section, index) => {
  observer.observe(section)
});

你可以看到最小的工作代码here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多