wordpress 显示html文本的字母由字母顺利地在页面滚动?

zazmityj  于 2023-06-21  发布在  WordPress
关注(0)|答案(1)|浏览(122)

我需要帮助来完成一个JavaScript效果。我期待着完成对这个网站的效果https://www.lucidmotors.com/-在第三节在主页上下来,你可以看到文本滚动/揭示是在其他文本顺利。
我在codepen https://codepen.io/Bes7weB/pen/zYKoexK上发现这个选项与我需要的效果相似,但它有点波涛汹涌,我需要它更平滑。

    • JS**
var textWrapper = document.querySelector(".ml3");
textWrapper.innerHTML = textWrapper.textContent.replace(
  /\S/g,
  "<span class='letter'>$&</span>"
);

var letter = document.querySelectorAll(".letter");
var i = 0;
var currentID = 0;
var slideCount = letter.length;

document.addEventListener("scroll", (e) => {
  let scrolled =
    document.documentElement.scrollTop /
    (document.documentElement.scrollHeight -
      document.documentElement.clientHeight);

  //   var nextID = currentID + 1;

  //   if (nextID < slideCount) {
  //     letter[nextID].style.setProperty(
  //       "--percentage",
  //       `${scrolled / 1}` * nextID
  //     );
  //   }

  //   currentID = nextID;

  letter.forEach(function (l, i) {
    // console.log("====",i / letter.length, i, letter.length)
    if (i / letter.length < scrolled) {
      l.style.setProperty("--percentage", 1);
    } else {
      l.style.setProperty("--percentage", 0);
    }
  });
});
    • CSS**
:root {
  --percentage: 0;
}

body {
  background-color: #000;
  margin: 0;
  height: 600vh;
}

.ml3 {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

span {
  font-family: Helvetica;
  margin: 0;
  padding: 0;
  font-size: 48px;
  color: #fff;
  letter-spacing: -0.3px;
}

.ml3 span {
  opacity: var(--percentage);
}
    • HTML**
<div class="ml3">
  <h1>THIS IS MY TEXT THAT IT'S GOING TO SHOW IN SCROLL</h1>
</div>

任何帮助都会很好

fjaof16o

fjaof16o1#

这可能是你正在寻找的,发现它很有趣,我认为我的答案可以改进,如果你只对垂直滚动感兴趣,你应该检查window.scrollY变量。

var textWrapper = document.querySelector(".ml3");
textWrapper.innerHTML = textWrapper.textContent.replace(
  /\S/g,
  "<span class='letter'>$&</span>"
);

var letter = document.querySelectorAll(".letter");

document.addEventListener("scroll", (e) => {
  let scrolled =
    document.documentElement.scrollTop /
    (document.documentElement.scrollHeight -
      document.documentElement.clientHeight) *
    letter.length;

  letter.forEach(function(l, i) {
    if ((scrolled - i) > 1)
      l.style.setProperty("--percentage", 1);
    else if ((scrolled - i) < 0.2)
      l.style.setProperty("--percentage", 0.2);
    else
      l.style.setProperty("--percentage", (scrolled - i));
  });
});
:root {
  --percentage: 0.2;
}

body {
  background-color: #000;
  margin: 0;
  height: 600vh;
}

.ml3 {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

span {
  font-family: Helvetica;
  margin: 0;
  padding: 0;
  font-size: 48px;
  color: #fff;
  letter-spacing: -0.3px;
}

.ml3 span {
  opacity: var(--percentage);
}
<div class="ml3">
  <h1>THIS IS MY TEXT THAT IT'S GOING TO SHOW IN SCROLL</h1>
</div>

相关问题