Alb
Alb

Reputation: 221

Parallax H1 Over BG Image

I have an h1 atop a background image,

<section class="cd-intro">
    <h1>Content</h1>
</section>`

With the following css:

.cd-intro {
    position: relative;
    height: 100%;
    background: url("../img/buff.jpg") no-repeat center center;
    background-size: cover;
    z-index: 1;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

I'm hoping to duplicate more or less exactly this effect here, as succintly as possible. Input appreciated.

Upvotes: 0

Views: 311

Answers (1)

jcuenod
jcuenod

Reputation: 58435

Questions have been asked about the basic parallax effect before so take a look around stackoverflow if you're still wondering about that.

As for the parallax header movement, the short answer is:

jQuery(window).scroll(function() {
    //Get the scoll position of the page
    scrollPos = jQuery(this).scrollTop();

    //Scroll and fade out the banner text
    jQuery('.featured-title').css({
      'margin-top' : -(scrollPos/3)+"px",
      'opacity' : 1-(scrollPos/300)
    });    
});

The way I came to that was by having a look at the source javascript on the website. You will see in main.js:

///////////////////////////////
// Parallax Scrolling
///////////////////////////////

// Calcualte the home banner parallax scrolling
function scrollBanner() {
    //Get the scoll position of the page
    scrollPos = jQuery(this).scrollTop();

    //Scroll and fade out the banner text
    jQuery('.featured-title').css({
      'margin-top' : -(scrollPos/3)+"px",
      'opacity' : 1-(scrollPos/300)
    });
}


jQuery(document).ready(function(){
///////////////////////////////
// Initialize Parallax 
/////////////////////////////// 
    if(!isMobile()) {
        jQuery(window).scroll(function() {        
                scrollBanner();       
        });
    }
    else {
        jQuery('#banner-text').css({
            'position': 'relative',
            'text-align': 'center',
            'margin': 'auto',
            'padding': '0',
            'top':'0'
        });
    }
    /* bunch of other stuff */
}

What's going on here is that the scroll event on the window object is being listened for. When this event fires, the margin-top value and the opacity are being changed proportionally to the distance scrolled (see the scrollBanner function).

Upvotes: 1

Related Questions