Advertisement
AncientPC

scroll.To()

Mar 15th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. http://demos.flesler.com/jquery/scrollTo/
  2.  
  3. Presentation wise, we need a div element with {position:fixed} and a known height to serve as the 'scroll-area',
  4. and another div placed inside it to serve as the 'scroll-block'. The scroll-block's size/position obviously needs
  5. to be determined with a bit of scripting.
  6.  
  7. 1. Calculate the percentage of scroll-zone to "fill" with scroll-block:
  8.     var height_scale = $(window).height() / $(document).height()
  9. 2. Set height of scroll-block div:
  10.     var block_height = height_scale * $('#scroll-zone').height();
  11.     $('#scroll-block').css('height', block_height);
  12. 3. Create a mouseover event for the scroll-zone, and inside the handler:
  13.     $('#scroll-zone').mouseover(function(e) {
  14.         //get mouse coordinate of y-axis inside the scroll-zone
  15.         var y = e.pageY - this.offsetTop;
  16.         //now we need to find out the percentage of the page this corresponds with
  17.         var percent_of_page = y / $('#scroll-zone').height();
  18.         //this in turn lets us determine how much of page to scroll
  19.         var scroll_position = percent_of_page * $(document).height()
  20.         //so now we do the actual scrolling with that plugin from earlier
  21.         $.scrollTo( {top:''+scroll_position+'px'}, 800);
  22.         //and readjust the position of the scroll-block accordingly with the y mouse-coord from earlier
  23.         $('#scroll-block).animate({top: y+'px'}, 5000, function() { });
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement