Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.