Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 18th, 2012  |  syntax: None  |  size: 1.47 KB  |  views: 152  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     // EDIT: Cache the text elements. This is very time consuming.
  2.     var texts = $('[data-type="scroll-text"]', $self);
  3.  
  4.     // EDIT: Also a bit faster than querying the DOM each time.
  5.     var speed = $self.data('speed');
  6.     var offset_y = $self.data('offsetY');
  7.  
  8.     // When the window is scrolled...
  9.     $(window).scroll(function () {
  10.  
  11.         // EDIT: Cache the scroll top. Much cleaner and a bit faster.
  12.         // You want to optimize scroll listeners as much as possible.
  13.         var scroll_top = $window.scrollTop();
  14.  
  15.         // If this section is in view
  16.         if (scroll_top + $window.height() > topOffset &&
  17.             topOffset + $self.height() > scroll_top) {
  18.  
  19.             // Scroll the background at var speed
  20.             // the yPos is a negative value because we're scrolling it UP!                              
  21.             var yPos = -(scroll_top / speed);
  22.  
  23.             // If this element has a Y offset then add it on
  24.             if (offset_y) {
  25.                 yPos += offset_y;
  26.             }
  27.  
  28.             // Put together our final background position
  29.             var coords = '50% '+ yPos + 'px';
  30.  
  31.             // Move the background
  32.             $self.css({ backgroundPosition: coords });
  33.  
  34.             texts.each(function() {
  35.                 var $text = $(this);
  36.                 var pos = (scroll_top / 10) * $text.data('speed');
  37.                 $text.css('margin-top', pos);
  38.             });
  39.         }; // in view
  40.     }); // window scroll
clone this paste RAW Paste Data