简体   繁体   中英

Trying to create a simple parallax effect in jquery

I'm learning JS and JQ and I'm working on a simple parallax effect and I need help to get him right. Here is the code: http://codepen.io/ronka/pen/JGxxBb .

$(document).ready(function(){
  $('section[data-type="parallax"]').each(function(){
    var $bgobj = $(this);
    $(window).scroll(function(){
        var $yPos = -($(window).scrollTop()- $bgobj.offset().top);
        console.log($(this));
        $bgobj.css('background-position','50% ' + $yPos +'px');
    });
  });
});

As you can see when you scroll you see the red background. I don't want it to show the red background just the image. what is the solution?

Try with something like this

$(document).ready(function(){
  $('section[data-type="parallax"]').each(function(){
    var $bgobj = $(this);
    $(window).scroll(function(){
        var yPos = $(window).scrollTop();
        $bgobj.css('background-position','50% -' + (yPos*0.4) +'px');
    });
  });
});

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