How to create a background-attachment attribute that does not require JavaScript but only CSS can achieve a fixed and scrolling effect on the page background. We see that there are a lot of site projects using parallax effect, through the picture and background dynamic changes as well as JS script to produce parallax, and today we only need CSS.
The HTML structure is simple, one class is. CD-FIXED-BG's div element is used to place a fixed background graph, and a. CD-SCROLLING-BG div element is used to scroll the part. We can place multiple. CD-FIXED-BG and. CD-SCROLLING-BG groupings.
The code is as follows |
Copy Code |
<main> <div class= "CD-FIXED-BG cd-bg-1" > </div> br> <div class= "CD-SCROLLING-BG cd-color-2" > <div class= "Cd-container" > <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur autem laborum fuga quas ipsam doloribus sequi.. . </p> </div> </div> ... Multiple groups of Div ... </main> |
Css
So how to achieve the background fixed and scrolling effect? In the beginning, I want to use jquery, maybe I should let a div fixed position, and then when scrolling the page to change the background picture, but feel bad. And simple we use a few lines of CSS can do, will be fixed elements of the background background-size value set to the Cover,background-attachment value set to fixed, so that the single page of the background fixed and scrolling effect. Please see the following code:
The code is as follows |
Copy Code |
Body, HTML, Main { /* Important */ height:100%; } . cd-fixed-bg { min-height:100%; Background-size:cover; background-attachment:fixed; background-repeat:no-repeat; Background-position:center Center; } . cd-fixed-bg.cd-bg-1 { Background-image:url (".. /img/cd-background-1.jpg "); } . cd-fixed-bg.cd-bg-2 { Background-image:url (".. /img/cd-background-2.jpg "); } . cd-fixed-bg.cd-bg-3 { Background-image:url (".. /img/cd-background-3.jpg "); } . cd-scrolling-bg { min-height:100%; } |