CSS3背景固定——background-attachment:fixed;
了解background-attachment属性
值 | 描述 |
---|---|
scroll | 背景图片随着页面的滚动而滚动,这是默认的。并不随着div内的滚动而滚动(比如文本大于元素高度时的滚动,背景图不动) |
fixed | 背景图片不会随着元素或页面的滚动而滚动。 |
local | 背景图片会随着元素或页面的滚动而滚动。 |
initial | 表示默认值。 |
inherit | 表继承 |
fixed值
写HTML ,当背景图片大于元素高度时,设置background-attachment:fixed; 当页面滚动,当前元素就可以得到不一样的背景图,很好看。
<!DOCTYPE html> <html lang="cn"> <head> <title>移动</title> <mate charset="UTF-8"> </head> <style> body{ padding: 0; margin: 0; font-family:Raleway,Helvetica,arial; } #bg-img{ background-image: url("https://ld-wp.template-help.com/rockthemes/21999/wp-content/uploads/2018/03/home_paralax2.jpg"); background-position: center center; background-attachment: fixed; background-repeat: no-repeat; background-size: cover; transition: background 0.3s, border 0.3s, border-radius 0.3s, box-shadow 0.3s; padding: 121px 0px 120px 0px; position: relative; } #bg-img::after{ content: " "; height: 100%; width: 100%; top: 0; left: 0; position: absolute; background-color: #000000; opacity: 0.65; z-index: 1; } .empty{ height: 800px; background-color: bisque; } #bg-img ul{ padding:20px; margin: 0; position: relative; z-index: 2; } #bg-img li{ line-height: 30px; list-style-type: none; text-align: center; color: #fff; } </style> <body> <div class="empty"> 空 </div> <section id="bg-img"> <ul> <li>Need fresh and classy design? </li> <li>We are here to offer you the most unique and classy solutions! Every design idea and project we perform,</li> <li>completely meet our clients' requirements being at the same time creative and extraordinary.</li> <li>Read more</li> </ul> </section> <div class="empty"> 空 </div> </body> </html>