背景固定

276 阅读1分钟

background-attachment属性

决定背景图像的位置是在是口内固定,或者随着包含它的区块滚动。

background-attachment : scroll |fixed | local

scroll : 默认值,背景图片随外部滚动条滚动,不随内部滚动条滚动

fixed 背景图片不随外部滚动条滚动,也不随内部滚动条滚动

local:自己滚动条动,外部滚动条动

案例演示


<!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>
    <style>
        body {
            height:1000px;
        }
        .box {
            width:200px;
            height:200px;
            border: 1px solid #000;
            /* 纵向溢出内容用滚动条滚动 */
            overflow-y:scroll;
            background-image:url("logo.png");
            /*背景图片不随外部滚动条滚动,也不随内部滚动条滚动 */
            /* background-attachment:fixed; */
            /* 默认值,背景图片随外部滚动条滚动,不随内部滚动条滚动 */
            /* background-attachment:scroll; */
            /* 自己滚动条动,外部滚动条动 */
            background-attachment:local;
        }
    </style>
</head>
<body>
    <div class="box">
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
        <p>内容</p>
    </div>
</body>
</html>