CSS特效

粘性文本整页滚动效果

2024-03-26  本文已影响0人  林中白虎

效果展示

粘性文本整页滚动效果1.png 粘性文本整页滚动效果2.png 粘性文本整页滚动效果3.png

CSS 知识点

整体页面效果实现

<div class="container">
  <!-- 第一屏 -->
  <div class="sec">
    <h2>Scroll Down</h2>
  </div>

  <!-- 第二屏 至 第七屏 -->
  <div class="sec"></div>
  <div class="sec"></div>
  <div class="sec"></div>
  <div class="sec"></div>
  <div class="sec"></div>
  <div class="sec"></div>

  <!-- 第二屏 至 第七屏 的文字 -->
  <div class="content">
    <h2>
      <!-- 因为每屏都在上滑移动,所以这里定义每屏字符的偏移量 -->
      <span style="--i:1">S</span>
      <span style="--i:2">t</span>
      <span style="--i:3">i</span>
      <span style="--i:4">c</span>
      <span style="--i:5">k</span>
      <span style="--i:6">y</span>
    </h2>
  </div>

  <!-- 第八屏 -->
  <div class="sec">
    <h2>Thank For Watching :)</h2>
  </div>
</div>

使用 scroll 相关属性完成每屏滚动效果

.container {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: auto;
  scroll-behavior: smooth;
  scroll-snap-type: y mandatory;
}

.sec {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  flex-flow: row wrap;
  justify-content: center;
  align-items: center;
  scroll-snap-align: center;
}

实现每屏的样式

/* 每屏的样式,这里只是展示第一屏的 */
.sec:nth-child(1) {
  background: rgba(23, 143, 255, 0.685) url(./images/bg1.jpg);
  background-size: cover;
  background-attachment: fixed;
  background-position: center;
  background-blend-mode: multiply;
}

实现第二屏开始的字符样式

.content {
  position: absolute;
  top: 0;
  width: 100%;
  text-align: center;
}

.content h2 {
  position: relative;
  display: flex;
  justify-content: center;
}

.content h2 span {
  position: sticky;
  top: 0;
  line-height: 100vh;
  height: 100vh;
  color: #fff;
  font-size: 14vw;
  /* 页面中已定义了对应的字母偏移量基础值,获取对应的基础值就可以 */
  margin-top: calc(100vh * var(--i));
}

完整代码下载

完整代码下载

上一篇 下一篇

猜你喜欢

热点阅读