/*! * jquery.scrollto * copyright (c) 2007-2015 ariel flesler - afleslergmailcom | http://flesler.blogspot.com * licensed under mit * http://flesler.blogspot.com/2007/10/jqueryscrollto.html * @projectdescription lightweight, cross-browser and highly customizable animated scrolling with jquery * @author ariel flesler * @version 2.1.2 */ ;(function(factory) { 'use strict'; if (typeof define === 'function' && define.amd) { // amd define( ['jquery'], factory ); } else if (typeof module !== 'undefined' && module.exports) { // commonjs module.exports = factory( require( 'jquery' ) ); } else { // global factory( jquery ); } })(function($) { 'use strict'; var $scrollto = $.scrollto = function(target, duration, settings) { return $( window ).scrollto( target, duration, settings ); }; $scrollto.defaults = { axis:'xy', duration: 0, limit:true }; function iswin(elem) { return ! elem.nodename || $.inarray( elem.nodename.tolowercase(), ['iframe','#document','html','body'] ) !== -1; } $.fn.scrollto = function(target, duration, settings) { if (typeof duration === 'object') { settings = duration; duration = 0; } if (typeof settings === 'function') { settings = { onafter:settings }; } if (target === 'max') { target = 9e9; } settings = $.extend( {}, $scrollto.defaults, settings ); // speed is still recognized for backwards compatibility duration = duration || settings.duration; // make sure the settings are given right var queue = settings.queue && settings.axis.length > 1; if (queue) { // let's keep the overall duration duration /= 2; } settings.offset = both( settings.offset ); settings.over = both( settings.over ); return this.each(function() { // null target yields nothing, just like jquery does if (target === null) { return; } var win = iswin( this ), elem = win ? this.contentwindow || window : this, $elem = $( elem ), targ = target, attr = {}, toff; switch (typeof targ) { // a number will pass the regex case 'number': case 'string': if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test( targ )) { targ = both( targ ); // we are done break; } // relative/absolute selector targ = win ? $( targ ) : $( targ, elem ); /* falls through */ case 'object': if (targ.length === 0) { return; } // domelement / jquery if (targ.is || targ.style) { // get the real position of the target toff = (targ = $( targ )).offset(); } } var offset = $.isfunction( settings.offset ) && settings.offset( elem, targ ) || settings.offset; $.each(settings.axis.split( '' ), function(i, axis) { var pos = axis === 'x' ? 'left' : 'top', pos = pos.tolowercase(), key = 'scroll' + pos, prev = $elem[key](), max = $scrollto.max( elem, axis ); if (toff) {// jquery / domelement attr[key] = toff[pos] + (win ? 0 : prev - $elem.offset()[pos]); // if it's a dom element, reduce the margin if (settings.margin) { attr[key] -= parseint( targ.css( 'margin' + pos ), 10 ) || 0; attr[key] -= parseint( targ.css( 'border' + pos + 'width' ), 10 ) || 0; } attr[key] += offset[pos] || 0; if (settings.over[pos]) { // scroll to a fraction of its width/height attr[key] += targ[axis === 'x'?'width':'height']() * settings.over[pos]; } } else { var val = targ[pos]; // handle percentage values attr[key] = val.slice && val.slice( -1 ) === '%' ? parsefloat( val ) / 100 * max : val; } // number or 'number' if (settings.limit && /^\d+$/.test( attr[key] )) { // check the limits attr[key] = attr[key] <= 0 ? 0 : math.min( attr[key], max ); } // don't waste time animating, if there's no need. if ( ! i && settings.axis.length > 1) { if (prev === attr[key]) { // no animation needed attr = {}; } else if (queue) { // intermediate animation animate( settings.onafterfirst ); // don't animate this axis again in the next iteration. attr = {}; } } }); animate( settings.onafter ); function animate(callback) { var opts = $.extend({}, settings, { // the queue setting conflicts with animate() // force it to always be true queue: true, duration: duration, complete: callback && function() { callback.call( elem, targ, settings ); } }); $elem.animate( attr, opts ); } }); }; // max scrolling position, works on quirks mode // it only fails (not too badly) on ie, quirks mode. $scrollto.max = function(elem, axis) { var dim = axis === 'x' ? 'width' : 'height', scroll = 'scroll' + dim; if ( ! iswin( elem )) { return elem[scroll] - $( elem )[dim.tolowercase()](); } var size = 'client' + dim, doc = elem.ownerdocument || elem.document, html = doc.documentelement, body = doc.body; return math.max( html[scroll], body[scroll] ) - math.min( html[size], body[size] ); }; function both(val) { return $.isfunction( val ) || $.isplainobject( val ) ? val : { top:val, left:val }; } // add special hooks so that window scroll properties can be animated $.tween.prophooks.scrollleft = $.tween.prophooks.scrolltop = { get: function(t) { return $( t.elem )[t.prop](); }, set: function(t) { var curr = this.get( t ); // if interrupt is true and user scrolled, stop animating if (t.options.interrupt && t._last && t._last !== curr) { return $( t.elem ).stop(); } var next = math.round( t.now ); // don't waste cpu // browsers don't render floating point scroll if (curr !== next) { $( t.elem )[t.prop](next); t._last = this.get( t ); } } }; // amd requirement return $scrollto; });