javascript - Animate Header based on different scroll positions -
currently have header animates (squishes) smaller version different logo , different nav after scrolling amount of pixels. if user scrolls past pixel amount (170 in case), header expands normal state. (because there long pages on site) client wants header 'expand' time the user scrolling in up. think getting close, events firing zillion times when begin scroll page. here current javascript:
function squishheader() { compactheader = true; header.animate({height: '48px'}, 500); mainnav.fadeout("fast"); logo.fadeout("fast"); scrolllogo.delay(300).slidedown("fast"); if (overlayisvisible == true) { overlay.fadeout("slow"); overlayisvisible = false; } } function expandheader() { compactheader = false; header.animate({height: '130px'}, 100); scrolllogo.css("display", "none"); logo.fadein("slow"); mainnav.delay(300).fadein("fast"); } var compactheader = false; var lastscrolltop = 0; $(window).on('scroll', function() { if (win.width() > 1024) { scrollposition = $(this).scrolltop(); if (scrollposition >= 170 && !compactheader) { squishheader(); } else if (scrollposition < 170 && compactheader) { expandheader(); } else if ( scrollposition < lastscrolltop && compactheader){ expandheader(); } lastscrolltop = scrollposition; } });
can please me this, it's driving me crazy! in advance
i think you're looking jquery debounce
jquery throttle / debounce allows rate-limit functions in multiple useful ways. passing delay , callback $.throttle returns new function execute no more once every delay milliseconds. passing delay , callback $.debounce returns new function execute once, coalescing multiple sequential calls single execution @ either beginning or end.
basically keep functions being called faster limit set. can (taken https://stackoverflow.com/a/9144827/2687861) limits $('#scrollmsg').html('scrolling!');
being called every 250 milliseconds.
$(window).scroll($.debounce( 250, true, function(){ $('#scrollmsg').html('scrolling!'); }));
some light reading. http://davidwalsh.name/javascript-debounce-function
Comments
Post a Comment