简体   繁体   中英

Parallax effect with jquery

I am trying to incorporate a parallax effect on multiple sections throughout a page. The following code i'm using works for the first section (hero) but subsequent sections wont work because it's calculating the offset based off of the top of the page instead of the parent div. Any ideas?

function parallax(){
    var scrolled = $(window).scrollTop();
    $('.parallax').css({'transform':'translate(0px,'+scrolled /10+'%'});
}

$(window).scroll(function(e){
    parallax();
});

Get the width of parent div or container and use parallax.

Try this

function parallax(){
    var scrolled  = $(".container").parent().width();
    $('.parallax').css({'transform':'translate(0px,'+scrolled /10+'%'});
}

$(window).scroll(function(e){
    parallax();
});

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM