css吸顶效果(例如:实现滚动页面导航栏固定在顶部)

主要针对需要吸顶的元素设置

/* 兼容chorme */
position: -webkit-sticky;
position: sticky;
top: 0px;

示例

<!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">
    <title>Document</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    .box1,.box2{
        width: 100%;
        height: 80px;
    }
    .box1 {
        background-color: aqua;
    }
    .box2 {
        background-color: #FF0000;
        /* 兼容chorme */
        position: -webkit-sticky;
        position: sticky;
        top: 0px;

    }
    .box3,.box4{
        width: 100%;
        height: 500px;
    }
    .box3{

        background-color: blue;
    }
    .box4{
        width: 100%;
        height: 500px;
        background-color: green;
    }
</style>
<body>
    <div class="box1"></div>
    <div class="box2">
        
    </div>
    <div class="box3"></div>
    <div class="box4"></div>
</body>
</html>

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐