• CSS基础11-视差滚动


    background-attachment实现(将图片一开始就固定在初始位置)

    background-attachment三种属性取值

    • scroll:背景固定于背景容器本身区域,它会随着背景容器的滚动,而不是随着它的内容滚动(也可以看成是对元素边框固定,正常浏览器就是这个效果)
    • fixed: 背景图片附着在浏览器的可视区域,因为浏览器的可视区域不具备滚动的性格,所以它是不滚动的
    • local:这是CSS3新增的属性值,背景图片附着在背景容器的内容区域,它会随着内容的滚动而滚动
      效果
      请添加图片描述

    代码

    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            div {
                height: 100vh;
                background: rgba(0, 0, 0, .7);
                color: #fff;
                line-height: 100vh;
                text-align: center;
                font-size: 20vh;
            }
            .img1{
                background-attachment: fixed; /*背景相对于浏览器固定 视差滚动关键*/
                background-size: cover; /*将图片放大直到铺满整个容器*/
                background-position: center center; /*左右上下居中*/
                background-image: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F0G020114924%2F200G0114924-15-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658835043&t=dd74082e5025a0f4f87530900371344b");
            }
            .img2{
                background-attachment: fixed;
                background-size: cover;
                background-position: center center;
                background-image: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F0F420110430%2F200F4110430-6-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658835043&t=405bde91b907771427c6e55e9a480dde");
            }
            .img3{
                background-attachment: fixed;
                background-size: cover;
                background-position: center center;
                background-image: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F210F2130512J47-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658835043&t=d66d9f36c430b399ff81b507359e4efe");
             }
    
        </style>
    </head>
    <body>
        <div class="text">1</div>
        <div class="img1">2</div>
        <div class="text">3</div>
        <div class="img2">4</div>
        <div class="text">5</div>
        <div class="img3">6</div>
        <div class="text">7</div>
    
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    MATLAB打开历史命令窗口并保持
    国产化操作系统改造oracle proc依赖库文件缺失处理
    CSS中的浮动float(元素怎样浮动以及浮动元素的特点--脱标)
    具有独特底部轮廓的剥离光刻胶的开发
    linux-vsftp虚拟多用户
    用例图中include和extend的含义
    Unity 之 Post Processing后处理不同项目配置(URP项目配置)
    数字化转型安全问题频发,山石网科助力数字政府建设
    pytest自动化框架运行全局配置文件pytest.ini
    09数据结构与算法刷题之【位运算】篇
  • 原文地址:https://blog.csdn.net/weixin_64925940/article/details/125473562