RtThemesSupport

quform jquery.smooth-scroll.min.js fix for rtthemes

Jan 29th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.  
  3. var version = '@VERSION',
  4.     defaults = {
  5.       exclude: [],
  6.       excludeWithin:[],
  7.       offset: 0,
  8.       direction: 'top', // one of 'top' or 'left'
  9.       scrollElement: null, // jQuery set of elements you wish to scroll (for $.smoothScroll).
  10.                           //  if null (default), $('html, body').firstScrollable() is used.
  11.       scrollTarget: null, // only use if you want to override default behavior
  12.       beforeScroll: function() {},  // fn(opts) function to be called before scrolling occurs. "this" is the element(s) being scrolled
  13.       afterScroll: function() {},   // fn(opts) function to be called after scrolling occurs. "this" is the triggering element
  14.       easing: 'swing',
  15.       speed: 400,
  16.       autoCoefficent: 2 // coefficient for "auto" speed
  17.     },
  18.  
  19.     getScrollable = function(opts) {
  20.       var scrollable = [],
  21.           scrolled = false,
  22.           dir = opts.dir && opts.dir == 'left' ? 'scrollLeft' : 'scrollTop';
  23.  
  24.       this.each(function() {
  25.  
  26.         if (this == document || this == window) { return; }
  27.         var el = $(this);
  28.         if ( el[dir]() > 0 ) {
  29.           scrollable.push(this);
  30.         } else {
  31.           // if scroll(Top|Left) === 0, nudge the element 1px and see if it moves
  32.           el[dir](1);
  33.           scrolled = el[dir]() > 0;
  34.           if ( scrolled ) {
  35.             scrollable.push(this);
  36.           }
  37.           // then put it back, of course
  38.           el[dir](0);
  39.         }
  40.       });
  41.  
  42.       // If no scrollable elements, fall back to <body>,
  43.       // if it's in the jQuery collection
  44.       // (doing this because Safari sets scrollTop async,
  45.       // so can't set it to 1 and immediately get the value.)
  46.       if (!scrollable.length) {
  47.         this.each(function(index) {
  48.           if (this.nodeName === 'BODY') {
  49.             scrollable = [this];
  50.           }
  51.         });
  52.       }
  53.  
  54.       // Use the first scrollable element if we're calling firstScrollable()
  55.       if ( opts.el === 'first' && scrollable.length > 1 ) {
  56.         scrollable = [ scrollable[0] ];
  57.       }
  58.  
  59.       return scrollable;
  60.     },
  61.     isTouch = 'ontouchend' in document;
  62.  
  63. $.fn.extend({
  64.   firstScrollable: function(dir) {
  65.     var scrl = getScrollable.call(this, {el: 'first', dir: dir});
  66.     return this.pushStack(scrl);
  67.   },
  68.  
  69.   smoothScroll: function(options) {
  70.     options = options || {};
  71.     var opts = $.extend({}, $.fn.smoothScroll.defaults, options),
  72.         locationPath = $.smoothScroll.filterPath(location.pathname);
  73.  
  74.     this
  75.     .unbind('click.smoothscroll')
  76.     .bind('click.smoothscroll', function(event) {
  77.       var link = this,
  78.           $link = $(this),
  79.           exclude = opts.exclude,
  80.           excludeWithin = opts.excludeWithin,
  81.           elCounter = 0, ewlCounter = 0,
  82.           include = true,
  83.           clickOpts = {},
  84.           hostMatch = ((location.hostname === link.hostname) || !link.hostname),
  85.           pathMatch = opts.scrollTarget || ( $.smoothScroll.filterPath(link.pathname) || locationPath ) === locationPath,
  86.           thisHash = escapeSelector(link.hash);
  87.  
  88.       if ( !opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) {
  89.         include = false;
  90.       } else {
  91.         while (include && elCounter < exclude.length) {
  92.           if ($link.is(escapeSelector(exclude[elCounter++]))) {
  93.             include = false;
  94.           }
  95.         }
  96.         while ( include && ewlCounter < excludeWithin.length ) {
  97.           if ($link.closest(excludeWithin[ewlCounter++]).length) {
  98.             include = false;
  99.           }
  100.         }
  101.       }
  102.  
  103.       if ( include ) {
  104.         event.preventDefault();
  105.  
  106.         $.extend( clickOpts, opts, {
  107.           scrollTarget: opts.scrollTarget || thisHash,
  108.           link: link
  109.         });
  110.  
  111.         $.smoothScroll( clickOpts );
  112.       }
  113.     });
  114.  
  115.     return this;
  116.   }
  117. });
  118.  
  119. $.smoothScroll = function(options, px) {
  120.   var opts, $scroller, scrollTargetOffset, speed,
  121.       scrollerOffset = 0,
  122.       offPos = 'offset',
  123.       scrollDir = 'scrollTop',
  124.       aniProps = {},
  125.       aniOpts = {},
  126.       scrollprops = [];
  127.  
  128.  
  129.   if (typeof options === 'number') {
  130.     opts = $.fn.smoothScroll.defaults;
  131.     scrollTargetOffset = options;
  132.   } else {
  133.     opts = $.extend({}, $.fn.smoothScroll.defaults, options || {});
  134.     if (opts.scrollElement) {
  135.       offPos = 'position';
  136.       if (opts.scrollElement.css('position') == 'static') {
  137.         opts.scrollElement.css('position', 'relative');
  138.       }
  139.     }
  140.   }
  141.  
  142.   opts = $.extend({link: null}, opts);
  143.   scrollDir = opts.direction == 'left' ? 'scrollLeft' : scrollDir;
  144.  
  145.   if ( opts.scrollElement ) {
  146.     $scroller = opts.scrollElement;
  147.     scrollerOffset = $scroller[scrollDir]();
  148.   } else {
  149.     $scroller = $('html, body').firstScrollable();
  150.   }
  151.  
  152.   // beforeScroll callback function must fire before calculating offset
  153.   opts.beforeScroll.call($scroller, opts);
  154.  
  155.   scrollTargetOffset = (typeof options === 'number') ? options :
  156.                         px ||
  157.                         ( $(opts.scrollTarget)[offPos]() &&
  158.                         $(opts.scrollTarget)[offPos]()[opts.direction] ) ||
  159.                         0;
  160.  
  161.   aniProps[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;
  162.   speed = opts.speed;
  163.  
  164.   // automatically calculate the speed of the scroll based on distance / coefficient
  165.   if (speed === 'auto') {
  166.  
  167.     // if aniProps[scrollDir] == 0 then we'll use scrollTop() value instead
  168.     speed = aniProps[scrollDir] || $scroller.scrollTop();
  169.  
  170.     // divide the speed by the coefficient
  171.     speed = speed / opts.autoCoefficent;
  172.   }
  173.  
  174.   aniOpts = {
  175.     duration: speed,
  176.     easing: opts.easing,
  177.     complete: function() {
  178.       opts.afterScroll.call(opts.link, opts);
  179.     }
  180.   };
  181.  
  182.   if (opts.step) {
  183.     aniOpts.step = opts.step;
  184.   }
  185.  
  186.   if ($scroller.length) {
  187.     $scroller.stop().animate(aniProps, aniOpts);
  188.   } else {
  189.     opts.afterScroll.call(opts.link, opts);
  190.   }
  191. };
  192.  
  193. $.smoothScroll.version = version;
  194. $.smoothScroll.filterPath = function(string) {
  195.   return string
  196.     .replace(/^\//,'')
  197.     .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
  198.     .replace(/\/$/,'');
  199. };
  200.  
  201. // default options
  202. $.fn.smoothScroll.defaults = defaults;
  203.  
  204. function escapeSelector (str) {
  205.   return str.replace(/(:|\.)/g,'\\$1');
  206. }
  207.  
  208. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment