使用css实现滚动贴合 Scroll Snap 特效

 

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <style>
      * {
        margin: 0;
        padding: 0;
        font-family: sans-serif;
      }
      main {
        width: 100vw;
        height: 100vh;
        max-width: 100%;
        display: grid;
        place-items: center;
        background-color: hsl(0deg, 0%, 5%);
      }

      ul {
        list-style: none;
      }

      li {
        padding: 0.6em;
      }

      a {
        color: white;
        font-size: 2em;
      }
    </style>
    <title>CSS 滚动贴合</title>
  </head>
  <body>
    <main>
      <ul>
        <li>
          <a href="./fullscreen-snap-y.html">垂直贴合</a>
        </li>
        <li><a href="./fullscreen-snap-x.html">水平贴合</a></li>
        <li><a href="./fullscreen-snap-proximity.html">近似贴合</a></li>
        <li><a href="./fullscreen-snap-y-padding.html">贴合间距</a></li>
        <li><a href="./list-scroll-y.html">Ul 列表贴合</a></li>
      </ul>
    </main>
  </body>
</html>

base.css

* {
  margin: 0;
  padding: 0;
  font-family: sans-serif;
}

/* 装饰性样式 */
section {
  display: grid;
  place-items: center;
  font-size: 2em;
}

section:nth-child(1) {
  background: rgb(229, 213, 255);
}

section:nth-child(2) {
  background: rgb(255, 219, 206);
}

section:nth-child(3) {
  background: rgb(242, 240, 186);
}

section:nth-child(4) {
  background: rgb(196, 244, 175);
}

h1 {
  box-shadow: 0 0 24px hsl(0, 0%, 0%, 0.2);
  display: grid;
  place-items: center;
  font-size: 2em;
  background: white;
}

ul {
  width: 50%;
  list-style: none;
  height: 240px;
  overflow: scroll;
}

li {
  padding: 1em;
  text-align: center;
  font-size: 2em;
}

li:nth-child(2n) {
  background: rgb(229, 213, 255);
}

li:nth-child(2n + 1) {
  background: rgb(255, 219, 206);
}

垂直贴合 ./fullscreen-snap-y.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="base.css" />
    <title>CSS Scroll Snap 垂直</title>
    <style>
      /* 核心样式 */
      main {
        /* mandatory:强制的 */
        scroll-snap-type: y mandatory;
        /* 需要把滚动条设置到直接父容器,
        scroll-snap-type 才能生效,
        默认是在 body 上,现在是 main 上 */
        overflow: scroll;
        height: 100vh;
      }
      section {
        width: 100vw;
        height: 150vh;
        scroll-snap-align: center;
      }
    </style>
  </head>
  <body>
    <main>
      <section>页面内容1</section>
      <section>页面内容2</section>
      <section>页面内容3</section>
      <section>页面内容4</section>
    </main>
  </body>
</html>

水平贴合 ./fullscreen-snap-x.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="base.css" />
    <title>CSS Scroll Snap 水平贴合</title>
    <style>
      /* 核心样式 */
      main {
        display: flex;
        scroll-snap-type: x mandatory;
        overflow: scroll;
        height: 100vh;
        width: 100vw;
      }

      section {
        width: 100vw;
        height: 100vh;
        scroll-snap-align: start;
        flex-shrink: 0;
      }
    </style>
  </head>
  <body>
    <main>
      <section>页面内容1</section>
      <section>页面内容2</section>
      <section>页面内容3</section>
      <section>页面内容4</section>
    </main>
  </body>
</html>

贴合间距  ./fullscreen-snap-y-padding.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="base.css" />
    <title>CSS Scroll Snap 贴合间距</title>
    <style>
      /* 核心样式 */
      h1 {
        height: 80px;
        position: sticky;
        top: 0;
      }
      main {
        scroll-snap-type: y mandatory;
        scroll-padding: 80px;
        overflow: scroll;
        height: 100vh;
      }
      section {
        width: 100vw;
        height: 100vh;
        scroll-snap-align: start;
      }
    </style>
  </head>
  <body>
    <main>
      <h1>H1标题</h1>
      <section>页面内容1</section>
      <section>页面内容2</section>
      <section>页面内容3</section>
      <section>页面内容4</section>
    </main>
  </body>
</html>

Ul 列表贴合  ./list-scroll-y.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="base.css" />
    <title>CSS Scroll Snap Ul 列表贴合</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        font-family: sans-serif;
      }

      main {
        width: 100vw;
        height: 100vh;
        display: grid;
        place-items: center;
      }

      /* 核心样式 */
      ul {
        scroll-snap-type: y mandatory;
      }

      li {
        scroll-snap-align: start;
      }
    </style>
  </head>
  <body>
    <main>
      <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
      </ul>
    </main>
  </body>
</html>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值