使用 offsetTop 实现滚动循环效果和旋转 div 的问题

Issue implementing scroll loop effect and rotating div with offsetTop

我现在正在处理一个具有滚动循环效果的网站(当您到达页面底部时,它会无缝地跳回顶部,从而形成无限循环)。尽管我在尝试实现根据 offsetTop 旋转 individual div 的效果时遇到问题。

这里有一个 fiddle link 有旋转效果但没有滚动循环效果-> https://jsfiddle.net/jacob_truax/bgrkewny/3/

这里是 link 到 fiddle 两种效果 -> https://jsfiddle.net/jacob_truax/b1x4dow7/18/

正如您在第二个 fiddle 中看到的那样,在实现旋转效果的同时添加滚动循环效果会破坏代码。有人可以帮我解决这个问题吗?

这是损坏的js fiddle

const sections = document.querySelectorAll("section")
const divTag = document.querySelector("div.Loop")
const mainTag = document.querySelector("main")

var doc = window.document,
  clones = divTag.querySelectorAll('.is-clone'),
  disableScroll = false,
  scrollHeight = 0,
  scrollPos = 0,
  clonesHeight = 0,
  i = 0;

const addMovement = function() {
  const topViewport = divTag.offsetTop
  const midViewport = topViewport + (divTag.offsetHeight / 2)

  sections.forEach(section => {
    const topSection = section.offsetTop
    const midSection = topSection + (section.offsetHeight / 2)

    const distanceToSection = (midViewport - midSection)
    console.log(distanceToSection)

    const image = section.querySelector(".info")

    image.style.transform = `rotate(${distanceToSection}deg)`
  })
}

addMovement()

function getScrollPos () {
  return (divTag.offsetTop || divTag.scrollTop) - (divTag.clientTop || 0);
}

function setScrollPos (pos) {
  divTag.scrollTop = pos;
}

function getClonesHeight () {
  clonesHeight = 0;

  for (i = 0; i < clones.length; i += 1) {
    clonesHeight = clonesHeight + clones[i].offsetHeight;
  }

  return clonesHeight;
}

function reCalc () {
  scrollPos = getScrollPos();
  scrollHeight = divTag.scrollHeight;
  clonesHeight = getClonesHeight();

  if (scrollPos <= 0) {
    setScrollPos(1); // Scroll 1 pixel to allow upwards scrolling
  }
}

function scrollUpdate () {
  if (!disableScroll) {
    scrollPos = getScrollPos();

    if (clonesHeight + scrollPos >= scrollHeight) {
      // Scroll to the top when you’ve reached the bottom
      setScrollPos(1); // Scroll down 1 pixel to allow upwards scrolling
      disableScroll = true;
    } else if (scrollPos <= 0) {
      // Scroll to the bottom when you reach the top
      setScrollPos(scrollHeight - clonesHeight);
      disableScroll = true;
    }
  }

  if (disableScroll) {
    // Disable scroll-jumping for a short time to avoid flickering
    window.setTimeout(function () {
      disableScroll = false;
    }, 40);
  }
}

function init () {
  reCalc();

  divTag.addEventListener('scroll', function () {
    window.requestAnimationFrame(scrollUpdate);
    addMovement()
  }, false);

  window.addEventListener('resize', function () {
    window.requestAnimationFrame(reCalc);
    addMovement()
  }, false);
}

if (document.readyState !== 'loading') {
  init()
} else {
  doc.addEventListener('DOMContentLoaded', init, false)
}

这是css

html,
body {
  height: 100%;
  /* overflow: hidden; */
}

body {
  color: #000;
}

main {
  height: 100%;
  position: relative;
  top: 0;
  left: 0;
}

header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  text-align: center;
  z-index: 1;
}

.Loop {
  position: absolute;
  height: 100%;
  overflow: auto;
}

section {
  position: relative;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
}

::scrollbar {
  display: none;
}

section div {
  position: absolute;
  z-index: 2;
  text-align: center;
  width: 50%;
  background-color: #ff0000;
}

section img {
  position: relative;
  width: 50%;
  background-color: #000;
}

The offsetTop property returns the top position (in pixels) relative to the top of the offsetParent element.

将第 14 行改为使用 scrollTop 有效:

  const topViewport = divTag.scrollTop;