[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/js/vendor/ -> jquery-scroll-to.js (source)

   1  /* jshint undef: false */
   2  /* jshint -W065 */
   3  
   4  /*!
   5   * jQuery.ScrollTo
   6   * Copyright (c) 2007-2014 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com
   7   * Licensed under MIT
   8   * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
   9   * @projectDescription Easy element scrolling using jQuery.
  10   * @author Ariel Flesler
  11   * @version 1.4.12
  12   */
  13  
  14  (function(factory) {
  15      // AMD
  16      if (typeof define === 'function' && define.amd) {
  17          define(['jquery'], factory);
  18      // CommonJS
  19      } else if (typeof exports === 'object') {
  20          factory(require('jquery'));
  21      // Browser globals
  22      } else {
  23          factory(jQuery);
  24      }
  25  }(function($) {
  26  
  27      var $scrollTo = $.scrollTo = function(target, duration, settings) {
  28          return $(window).scrollTo(target, duration, settings);
  29      };
  30  
  31      $scrollTo.defaults = {
  32          axis: 'xy',
  33          duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1,
  34          limit: true
  35      };
  36  
  37      // Returns the element that needs to be animated to scroll the window.
  38      // Kept for backwards compatibility (specially for localScroll & serialScroll)
  39      $scrollTo.window = function() {
  40          return $(window)._scrollable();
  41      };
  42  
  43      // Hack, hack, hack :)
  44      // Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
  45      $.fn._scrollable = function() {
  46          return this.map(function() {
  47              var elem = this,
  48                      isWin = !elem.nodeName || $.inArray(elem.nodeName.toLowerCase(), ['iframe', '#document', 'html', 'body']) !== -1;
  49  
  50              if (!isWin) {
  51                  return elem;
  52              }
  53  
  54              var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
  55  
  56              return /webkit/i.test(navigator.userAgent) || doc.compatMode === 'BackCompat' ?
  57                      doc.body :
  58                      doc.documentElement;
  59          });
  60      };
  61  
  62      $.fn.scrollTo = function(target, duration, settings) {
  63          if (typeof duration === 'object') {
  64              settings = duration;
  65              duration = 0;
  66          }
  67          if (typeof settings === 'function') {
  68              settings = {onAfter: settings};
  69          }
  70  
  71          if (target === 'max') {
  72              target = 9e9;
  73          }
  74  
  75          settings = $.extend({}, $scrollTo.defaults, settings);
  76          // Speed is still recognized for backwards compatibility
  77          duration = duration || settings.duration;
  78          // Make sure the settings are given right
  79          settings.queue = settings.queue && settings.axis.length > 1;
  80  
  81          // Let's keep the overall duration
  82          if (settings.queue) {
  83              duration /= 2;
  84          }
  85  
  86          settings.offset = both(settings.offset);
  87          settings.over = both(settings.over);
  88  
  89          return this._scrollable().each(function() {
  90  
  91              // Null target yields nothing, just like jQuery does
  92              if (target === null) {
  93                  return;
  94              }
  95  
  96              var elem = this,
  97                      $elem = $(elem),
  98                      targ = target, toff, attr = {},
  99                      win = $elem.is('html,body');
 100  
 101              switch (typeof targ) {
 102                  // A number will pass the regex
 103                  case 'number':
 104                  case 'string':
 105                      if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)) {
 106                          targ = both(targ);
 107                          // We are done
 108                          break;
 109                      }
 110                      // Relative/Absolute selector, no break!
 111                      targ = win ? $(targ) : $(targ, this);
 112                      if (!targ.length) {
 113                          return;
 114                      }
 115                      /* falls through */
 116                  case 'object':
 117                      // DOMElement / jQuery
 118                      if (targ.is || targ.style) {
 119                          // Get the real position of the target
 120                          toff = (targ = $(targ)).offset();
 121                      }
 122              }
 123  
 124              var offset = $.isFunction(settings.offset) && settings.offset(elem, targ) || settings.offset;
 125  
 126              $.each(settings.axis.split(''), function(i, axis) {
 127                  var Pos = axis === 'x' ? 'Left' : 'Top',
 128                          pos = Pos.toLowerCase(),
 129                          key = 'scroll' + Pos,
 130                          old = elem[key],
 131                          max = $scrollTo.max(elem, axis);
 132  
 133                  if (toff) {// jQuery / DOMElement
 134                      attr[key] = toff[pos] + (win ? 0 : old - $elem.offset()[pos]);
 135  
 136                      // If it's a dom element, reduce the margin
 137                      if (settings.margin) {
 138                          attr[key] -= parseInt(targ.css('margin' + Pos)) || 0;
 139                          attr[key] -= parseInt(targ.css('border' + Pos + 'Width')) || 0;
 140                      }
 141  
 142                      attr[key] += offset[pos] || 0;
 143  
 144                      // Scroll to a fraction of its width/height
 145                      if (settings.over[pos]) {
 146                          attr[key] += targ[axis === 'x' ? 'width' : 'height']() * settings.over[pos];
 147                      }
 148                  } else {
 149                      var val = targ[pos];
 150                      // Handle percentage values
 151                      attr[key] = val.slice && val.slice(-1) === '%' ?
 152                              parseFloat(val) / 100 * max
 153                              : val;
 154                  }
 155  
 156                  // Number or 'number'
 157                  if (settings.limit && /^\d+$/.test(attr[key])) {
 158                      // Check the limits
 159                      attr[key] = attr[key] <= 0 ? 0 : Math.min(attr[key], max);
 160                  }
 161  
 162                  // Queueing axes
 163                  if (!i && settings.queue) {
 164                      // Don't waste time animating, if there's no need.
 165                      if (old !== attr[key]) {
 166                          // Intermediate animation
 167                          animate(settings.onAfterFirst);
 168                      }
 169                      // Don't animate this axis again in the next iteration.
 170                      delete attr[key];
 171                  }
 172              });
 173  
 174              animate(settings.onAfter);
 175  
 176  			function animate(callback) {
 177                  $elem.animate(attr, duration, settings.easing, callback && function() {
 178                      callback.call(this, targ, settings);
 179                  });
 180              }
 181  
 182          }).end();
 183      };
 184  
 185      // Max scrolling position, works on quirks mode
 186      // It only fails (not too badly) on IE, quirks mode.
 187      $scrollTo.max = function(elem, axis) {
 188          var Dim = axis === 'x' ? 'Width' : 'Height',
 189                  scroll = 'scroll' + Dim;
 190  
 191          if (!$(elem).is('html,body')) {
 192              return elem[scroll] - $(elem)[Dim.toLowerCase()]();
 193          }
 194  
 195          var size = 'client' + Dim,
 196                  html = elem.ownerDocument.documentElement,
 197                  body = elem.ownerDocument.body;
 198  
 199          return Math.max(html[scroll], body[scroll]) - Math.min(html[size], body[size]);
 200      };
 201  
 202  	function both(val) {
 203          return $.isFunction(val) || typeof val === 'object' ? val : {top: val, left: val};
 204      }
 205  
 206      // AMD requirement
 207      return $scrollTo;
 208  }));


Generated: Fri Oct 18 01:01:36 2019 Cross-referenced by PHPXref 0.7.1