javascript - Jerky parallaxing with scrollwheel in WebKit -
i'm having issues parallaxing backgrounds. i've made little website event organized friends of mine, , on site have bunch of background images alternating in between content sections. i've added logic offset these background images when scrolling, create parallaxing effect.
it's working enough , haven't noticed performance issues, when using scrollwheel, parallaxing seems lagging behind in webkit browsers.
here's link website:
the effect i've tried mimic, @ least background images, 1 seen on spotify website:
from looking @ source code, seem doing more or less same thing: have parallaxing function calculates background transform based on scrolltop
value of document, , function throttled 16 ms , bound window's scroll event. still, background transformation on spotify site instant, while mine visually lags behind content. it's not "broken" in works in firefox/ie , works in webkit browsers when sliding scrollbar manually... it's annoying.
does have tips on causing jerkiness?
here's code parallaxing functionality (i'm using prototype sorry this
spam):
parallaxbackground: function () { var viewporttop = this.elements.$document.scrolltop(); var viewportbottom = viewporttop + this.elements.$window.height(); var scrolldelta = this.slideheight + this.elements.$window.height(); $.each( this.backgroundslides, function ( index, slide ) { var slidetop = slide.$container.offset().top; var slidebottom = slidetop + this.slideheight; if ( slidebottom < viewporttop || slidetop > viewportbottom ) return true; var factor = 1 - ( slidebottom - viewporttop ) / scrolldelta; this.transformbackground( slide.$image, this.slidelength * ( factor - 1 ) ); }.bind( ) ); }, transformbackground: modernizr.csstransforms ? function ( $backgroundelement, distance ) { $backgroundelement.css( { '-ms-transform': 'translate(0px, ' + distance + 'px)', '-webkit-transform': 'translate(0px, ' + distance + 'px)', 'transform': 'translate(0px, ' + distance + 'px)' } ); } : function ( $backgroundelement, distance ) { $backgroundelement.css( 'top', distance ); }
and here's how gets bound (using underscore throttling):
this.elements.$window.on( 'scroll', _.throttle( this.parallaxbackground.bind( ), 16 ) );
i recreated own parallax effect based on spotify's website , ran across many of issues mention here.
while cannot rid of stutter on safari specifically, i've managed smooth 60fps on chrome , firefox.
i've published jquery plugin here, adapt use framework of choice:
http://pixelcog.com/parallax.js/
there several tips helped me optimize it:
avoid triggering layout whenever possible
don't attach lot of logic scroll event directly, defer settimeout or requestanimationframe call instead
utilize
position:fixed;
on background elements. on browsers lag bit, stutter minimal.enforce browser layering null transforms, sparingly.
explore chrome devtools diagnose bottlenecks
these helped me rid of of stutter seeing in own implementation.
Comments
Post a Comment