[ 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 Ariel Flesler - aflesler ○ gmail • com | https://github.com/flesler
   7   * Licensed under MIT
   8   * https://github.com/flesler/jquery.scrollTo
   9   * @projectDescription Lightweight, cross-browser and highly customizable animated scrolling with jQuery
  10   * @author Ariel Flesler
  11   * @version 2.1.3
  12   */
  13  ;(function(factory) {
  14      'use strict';
  15      if (typeof define === 'function' && define.amd) {
  16          // AMD
  17          define(['jquery'], factory);
  18      } else if (typeof module !== 'undefined' && module.exports) {
  19          // CommonJS
  20          module.exports = factory(require('jquery'));
  21      } else {
  22          // Global
  23          factory(jQuery);
  24      }
  25  })(function($) {
  26      'use strict';
  27  
  28      var $scrollTo = $.scrollTo = function(target, duration, settings) {
  29          return $(window).scrollTo(target, duration, settings);
  30      };
  31  
  32      $scrollTo.defaults = {
  33          axis:'xy',
  34          duration: 0,
  35          limit:true
  36      };
  37  
  38  	function isWin(elem) {
  39          return !elem.nodeName ||
  40              $.inArray(elem.nodeName.toLowerCase(), ['iframe','#document','html','body']) !== -1;
  41      }
  42  
  43  	function isFunction(obj) {
  44          // Brought from jQuery since it's deprecated
  45          return typeof obj === 'function'
  46      }
  47  
  48      $.fn.scrollTo = function(target, duration, settings) {
  49          if (typeof duration === 'object') {
  50              settings = duration;
  51              duration = 0;
  52          }
  53          if (typeof settings === 'function') {
  54              settings = { onAfter:settings };
  55          }
  56          if (target === 'max') {
  57              target = 9e9;
  58          }
  59  
  60          settings = $.extend({}, $scrollTo.defaults, settings);
  61          // Speed is still recognized for backwards compatibility
  62          duration = duration || settings.duration;
  63          // Make sure the settings are given right
  64          var queue = settings.queue && settings.axis.length > 1;
  65          if (queue) {
  66              // Let's keep the overall duration
  67              duration /= 2;
  68          }
  69          settings.offset = both(settings.offset);
  70          settings.over = both(settings.over);
  71  
  72          return this.each(function() {
  73              // Null target yields nothing, just like jQuery does
  74              if (target === null) return;
  75  
  76              var win = isWin(this),
  77                  elem = win ? this.contentWindow || window : this,
  78                  $elem = $(elem),
  79                  targ = target,
  80                  attr = {},
  81                  toff;
  82  
  83              switch (typeof targ) {
  84                  // A number will pass the regex
  85                  case 'number':
  86                  case 'string':
  87                      if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)) {
  88                          targ = both(targ);
  89                          // We are done
  90                          break;
  91                      }
  92                      // Relative/Absolute selector
  93                      targ = win ? $(targ) : $(targ, elem);
  94                      /* falls through */
  95                  case 'object':
  96                      if (targ.length === 0) return;
  97                      // DOMElement / jQuery
  98                      if (targ.is || targ.style) {
  99                          // Get the real position of the target
 100                          toff = (targ = $(targ)).offset();
 101                      }
 102              }
 103  
 104              var offset = isFunction(settings.offset) && settings.offset(elem, targ) || settings.offset;
 105  
 106              $.each(settings.axis.split(''), function(i, axis) {
 107                  var Pos    = axis === 'x' ? 'Left' : 'Top',
 108                      pos = Pos.toLowerCase(),
 109                      key = 'scroll' + Pos,
 110                      prev = $elem[key](),
 111                      max = $scrollTo.max(elem, axis);
 112  
 113                  if (toff) {// jQuery / DOMElement
 114                      attr[key] = toff[pos] + (win ? 0 : prev - $elem.offset()[pos]);
 115  
 116                      // If it's a dom element, reduce the margin
 117                      if (settings.margin) {
 118                          attr[key] -= parseInt(targ.css('margin'+Pos), 10) || 0;
 119                          attr[key] -= parseInt(targ.css('border'+Pos+'Width'), 10) || 0;
 120                      }
 121  
 122                      attr[key] += offset[pos] || 0;
 123  
 124                      if (settings.over[pos]) {
 125                          // Scroll to a fraction of its width/height
 126                          attr[key] += targ[axis === 'x'?'width':'height']() * settings.over[pos];
 127                      }
 128                  } else {
 129                      var val = targ[pos];
 130                      // Handle percentage values
 131                      attr[key] = val.slice && val.slice(-1) === '%' ?
 132                          parseFloat(val) / 100 * max
 133                          : val;
 134                  }
 135  
 136                  // Number or 'number'
 137                  if (settings.limit && /^\d+$/.test(attr[key])) {
 138                      // Check the limits
 139                      attr[key] = attr[key] <= 0 ? 0 : Math.min(attr[key], max);
 140                  }
 141  
 142                  // Don't waste time animating, if there's no need.
 143                  if (!i && settings.axis.length > 1) {
 144                      if (prev === attr[key]) {
 145                          // No animation needed
 146                          attr = {};
 147                      } else if (queue) {
 148                          // Intermediate animation
 149                          animate(settings.onAfterFirst);
 150                          // Don't animate this axis again in the next iteration.
 151                          attr = {};
 152                      }
 153                  }
 154              });
 155  
 156              animate(settings.onAfter);
 157  
 158  			function animate(callback) {
 159                  var opts = $.extend({}, settings, {
 160                      // The queue setting conflicts with animate()
 161                      // Force it to always be true
 162                      queue: true,
 163                      duration: duration,
 164                      complete: callback && function() {
 165                          callback.call(elem, targ, settings);
 166                      }
 167                  });
 168                  $elem.animate(attr, opts);
 169              }
 170          });
 171      };
 172  
 173      // Max scrolling position, works on quirks mode
 174      // It only fails (not too badly) on IE, quirks mode.
 175      $scrollTo.max = function(elem, axis) {
 176          var Dim = axis === 'x' ? 'Width' : 'Height',
 177              scroll = 'scroll'+Dim;
 178  
 179          if (!isWin(elem))
 180              return elem[scroll] - $(elem)[Dim.toLowerCase()]();
 181  
 182          var size = 'client' + Dim,
 183              doc = elem.ownerDocument || elem.document,
 184              html = doc.documentElement,
 185              body = doc.body;
 186  
 187          return Math.max(html[scroll], body[scroll]) - Math.min(html[size], body[size]);
 188      };
 189  
 190  	function both(val) {
 191          return isFunction(val) || $.isPlainObject(val) ? val : { top:val, left:val };
 192      }
 193  
 194      // Add special hooks so that window scroll properties can be animated
 195      $.Tween.propHooks.scrollLeft =
 196      $.Tween.propHooks.scrollTop = {
 197          get: function(t) {
 198              return $(t.elem)[t.prop]();
 199          },
 200          set: function(t) {
 201              var curr = this.get(t);
 202              // If interrupt is true and user scrolled, stop animating
 203              if (t.options.interrupt && t._last && t._last !== curr) {
 204                  return $(t.elem).stop();
 205              }
 206              var next = Math.round(t.now);
 207              // Don't waste CPU
 208              // Browsers don't render floating point scroll
 209              if (curr !== next) {
 210                  $(t.elem)[t.prop](next);
 211                  t._last = this.get(t);
 212              }
 213          }
 214      };
 215  
 216      // AMD requirement
 217      return $scrollTo;
 218  });


Generated: Sat Apr 20 01:00:58 2024 Cross-referenced by PHPXref 0.7.1