CSS-made page background fixed and scrolling effect

Source: Internet
Author: User
Tags min

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%;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.