Advertisement
sandyd

Untitled

Oct 7th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.98 KB | None | 0 0
  1. function throttle(fn, delay) {
  2.     "use strict";
  3.     var timer = null;
  4.     return function () {
  5.     var context = this, args = arguments;
  6.     clearTimeout(timer);
  7.     timer = setTimeout(function () {
  8.       fn.apply(context, args);
  9.     }, delay);
  10.   };
  11. }
  12. jQuery(document).ready(function(){
  13.     "use strict";
  14.     var prevScrollTop = 0;
  15.     var $scrollDiv    = jQuery('div#container');
  16.     var $currentDiv   = $scrollDiv.children('div:first-child');
  17.     var scrollup=1;
  18.     var scrolldown=0;
  19.     var threadrunning=true;
  20.     var divid=null;
  21.  
  22.     jQuery('#container').animate({scrollTop:jQuery('#slideshow-wrapper').position().top}, 'fast', function() { threadrunning=false; console.log('jump'); });
  23.    
  24.     /*$scrollDiv.scroll(throttle(function(eventObj) */
  25.     $scrollDiv.on("scroll", throttle(function(){
  26.         if (threadrunning) {return;}
  27.         threadrunning=true;
  28.         var curScrollTop = $scrollDiv.scrollTop();
  29.  
  30.         if (prevScrollTop < curScrollTop && scrolldown === 0 )
  31.         {
  32.         // Scrolling down:
  33.         threadrunning=true;
  34.         $currentDiv = $currentDiv.next().scrollTo();
  35.         console.log($currentDiv.attr('id'));
  36.         divid =$currentDiv.attr('id');
  37.         if (divid==="wrapper-section-photovideos") {
  38.             scrolldown=1;
  39.         }
  40.         if (scrollup === 1) {
  41.             scrollup=0;
  42.         }
  43.         jQuery('#container').animate({scrollTop:jQuery('#'+divid).position().top}, 'fast', function() { threadrunning=false; console.log('down'); });
  44.  
  45.  
  46.         }
  47.         if (prevScrollTop > curScrollTop && scrollup === 0) {
  48.             // Scrolling up:
  49.             threadrunning=true;
  50.             $currentDiv = $currentDiv.prev().scrollTo();
  51.             console.log($currentDiv.attr('id'));
  52.             divid =$currentDiv.attr('id');
  53.             if (divid==="slideshow-wrapper") {
  54.                 scrollup=1;
  55.             }
  56.             if (scrolldown === 1) {
  57.             scrolldown=0;
  58.             }
  59.             jQuery('#container').animate({scrollTop:jQuery('#'+divid).position().top}, 'fast', function() { threadrunning=false; console.log('up');});
  60.         }
  61.         prevScrollTop = curScrollTop;
  62.  
  63.     },800));
  64.  
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement