自学内容网 自学内容网

前端开发攻略---使用css实现滚动吸附效果

实现代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      .box {
        width: 100%;
        height: 300px;
        display: flex;
        overflow-x: auto;
        /* 第一个参数表示滚动方向,x:横向滚动 y:纵向滚动 */
        /* 第二个参数表示吸附方式,mandatory:表示强制吸附  proximity:表示接近吸附*/
        scroll-snap-type: x mandatory;
      }
      .item {
        width: 100%;
        height: 100%;
        flex-shrink: 0;
        /* 吸附的对齐方式,start表示开始位置  center表示中间位置,end表示结束位置 */
        scroll-snap-align: start;
        /* 停在下一个元素 */
        scroll-snap-stop: always;
      }
      .item:nth-child(1) {
        background-color: rgb(13, 255, 0);
      }
      .item:nth-child(2) {
        background-color: rgb(255, 0, 149);
      }
      .item:nth-child(3) {
        background-color: rgb(255, 242, 0);
      }
      .item:nth-child(4) {
        background-color: rgb(0, 255, 47);
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
    </div>
  </body>
</html>


原文地址:https://blog.csdn.net/nibabaoo/article/details/142876830

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!