nano Posted December 21, 2011 Share Posted December 21, 2011 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 More sharing options...
ignace Posted December 22, 2011 Share Posted December 22, 2011 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}); }); }); Link to comment https://forums.phpfreaks.com/topic/253604-jquery-parallax-help-multiple-objects/#findComment-1300659 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.