css background-attachment属性进阶

2020-05-02 07:44:36易采站长站整理

body{
background-image: url(img/cartooncat.png);
background-position: bottom left;
background-attachment: fixed;
background-repeat: no-repeat;
height: 1000px;
}
</style>
</head>
<body>
<h1>下拉看效果:</h1>
</body>

或者看mozilla的demo。

这里我要强调一点我的看法:

给任何元素的背景图设置background-attachment: fixed;效果都是一样的,都是相对于视口,因为一个网页只有一个视口,该背景和元素已经没关系了,要说有关大概也只是元素不可见则背景图不可见。

而这个视口是什么呢?这里推荐一篇文章《像素与浏览器视口的细节》

四、多背景图background-attachment
也可以为多个背景图设置background-attachment

body {
background-image: url("img1.png"), url("img2.png");
background-attachment: scroll, fixed;
}

五、资源链接

w3c background-attachment

像素与浏览器视口的细节

a table of two viewports