分享一个css的吸附效果scroll-snap-type

html 复制代码
<!DOCTYPE html>

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>
        
    </title>
   
    <meta charset="UTF-8" />
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no" />


 
</head>
<style>
    .box{
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        position: fixed;
        overflow-y: scroll;
        /* scroll-snap-type: y mandatory; */
        scroll-snap-type: y proximity;
    }
    .item{
        position: relative;
        min-height: 100vh;
        scroll-snap-align: start;
        scroll-snap-stop: always;
    }
</style>
<body>
    <div class="box">
        <div class="item" style="background-color: black;"></div>
        <div class="item" style="background-color: #e0e0e0;height: 150vh;"><p style="position: absolute;top: 0;">头部文字</p><p style="position: absolute;bottom: 0;">底部文字</p></div>
        <div class="item" style="background-color: #999999;height: 120vh;"></div>
    </div>
</body>

</html>
相关推荐
Pedantic27 分钟前
SwiftUI 手势层级(Gesture Hierarchy)详解
前端
飘尘43 分钟前
前端转型全栈(Java后端)的快速上手指引
前端·后端·全栈
一颗烂土豆1 小时前
Meshopt 压缩深度解析,为什么它比 Draco 更快
前端·javascript·webgl
浏览器工程师2 小时前
AI Agent 接浏览器任务,先别让它一路点到底
前端·后端
雨季mo浅忆2 小时前
VSCode自动格式化三要素
前端
爱勇宝3 小时前
深扒 Anthropic 1680 位工程师简历:应届生几乎没机会,AI 公司最缺的不是博士
前端·后端·程序员
kyriewen3 小时前
同事每天催我 Code Review,我写了个脚本让 AI 替我 review PR——现在他反过来催 AI 了
前端·javascript·ai编程
user20585561518136 小时前
Windows 项目安装时报 `node-sass` 错误,如何快速处理
前端
LiaCode6 小时前
Redis 在生产项目的使用
前端·后端