Jump to content

jQuery Parallax help - multiple objects


nano

Recommended Posts

Hey Guys,

 

I am building a parallax website where different elements move on scroll. It's different to the traditional 'changing background position' style, as there are multiple individual elements that I want to move.

 

Here is what I have so far:

 

var parallax = {
init: function(){
	parallax.scroll('.cc',0.95);
},
scroll: function(el,speed){
	var $window = $(window);
	$(el).each(function(){
		var $this=$(this),top=$this.position().top,offset=$this.offset().top;
		$window.bind('scroll', function() {
			var scrollTop=$window.scrollTop();
                if ((scrollTop + $window.height()) >= (offset) && ((offset + $this.height()) > scrollTop)) { // el is in-view	
				pos = ;// how to set 'pos' variable?!
				$this.css({"top": pos}); // set new position
                }
            });
        });
}
};
$(document).ready(parallax.init());

 

The issue I am having is I can't come up with the logic required to work out the 'pos' variable, the new position.

 

My head now hurts trying to work it out so if anyone has any ideas, they would be greatly appreciated!

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/253604-jquery-parallax-help-multiple-objects/
Share on other sites

Try this:

 

var $cc = $('.cc');
$(window).scroll(function() {
    var yScrollOffset = window.pageYOffset;
    $cc.each(function() {
        var $this = $(this);
        $this.css({top:$this.position().top + yScrollOffset});
    });
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.