forked from litera/jquery-scrollintoview
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.scrollintoview.min.js
12 lines (12 loc) · 4.07 KB
/
jquery.scrollintoview.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*!
* jQuery scrollintoview() plugin and :scrollable selector filter
*
* Version 1.8 (14 Jul 2011)
* Version 1.9 (30 Sept 2015) - Chris Barth
* Requires jQuery 1.4 or newer
*
* Copyright (c) 2011 Robert Koritnik
* Licensed under the terms of the MIT license
* http://www.opensource.org/licenses/mit-license.php
*/
(function(h){var d={vertical:{x:false,y:true},horizontal:{x:true,y:false},both:{x:true,y:true},x:{x:true,y:false},y:{x:false,y:true}};var e={direction:"both",topOffset:0};var k=/^(?:html)$/i;var j=function(o,n){n=n||(document.defaultView&&document.defaultView.getComputedStyle?document.defaultView.getComputedStyle(o,null):o.currentStyle);var m=document.defaultView&&document.defaultView.getComputedStyle?true:false;var l={top:(parseFloat(m?n.borderTopWidth:h.css(o,"borderTopWidth"))||0),left:(parseFloat(m?n.borderLeftWidth:h.css(o,"borderLeftWidth"))||0),bottom:(parseFloat(m?n.borderBottomWidth:h.css(o,"borderBottomWidth"))||0),right:(parseFloat(m?n.borderRightWidth:h.css(o,"borderRightWidth"))||0)};return{top:l.top,left:l.left,bottom:l.bottom,right:l.right,vertical:l.top+l.bottom,horizontal:l.left+l.right}};var a=function(l){var n=h(window);var m=k.test(l[0].nodeName);return{border:m?{top:0,left:0,bottom:0,right:0}:j(l[0]),scroll:{top:(m?n:l).scrollTop(),left:(m?n:l).scrollLeft()},scrollbar:{right:m?0:l.innerWidth()-l[0].clientWidth,bottom:m?0:l.innerHeight()-l[0].clientHeight},rect:(function(){var o=l[0].getBoundingClientRect();return{top:m?0:o.top,left:m?0:o.left,bottom:m?l[0].clientHeight:o.bottom,right:m?l[0].clientWidth:o.right}})()}};function f(l,n,m){if(k.test(l[0].nodeName)){l=h("html,body")}l.animate(n,m).eq(0).queue(function(o){h.isFunction(m.complete)&&m.complete.call(l[0]);o()})}function g(){var l={};if(arguments.length>0){if(typeof arguments[0][0]!=="object"){l.duration=arguments[0][0];if(arguments[0].length>1){l.easing=arguments[0][1];if(arguments[0].length>2){l.complete=arguments[0][2]}}}else{l=h.extend(l,arguments[0][0])}}l=h.extend({},e,l);l.direction=d[typeof(l.direction)==="string"&&l.direction.toLowerCase()]||d.both;if(l.direction.x===true){l.direction.selector="horizontal"}if(l.direction.y===true){l.direction.selector=l.direction.selector?"both":"vertical"}return l}h.fn.extend({scrollIntoView:c,scrollintoview:c,scrollToTop:i,scrolltotop:i});function c(){var n=g(arguments);var p=this.eq(0);var m=p.closest(":scrollable("+n.direction.selector+")");if(m.length>0){m=m.eq(0);var q={e:a(p),s:a(m)};var l={top:q.e.rect.top-(q.s.rect.top+q.s.border.top),bottom:q.s.rect.bottom-q.s.border.bottom-q.s.scrollbar.bottom-q.e.rect.bottom,left:q.e.rect.left-(q.s.rect.left+q.s.border.left),right:q.s.rect.right-q.s.border.right-q.s.scrollbar.right-q.e.rect.right};var o={};if(n.direction.y===true){if(l.top<0){o.scrollTop=q.s.scroll.top+l.top}else{if(l.top>0&&l.bottom<0){o.scrollTop=q.s.scroll.top+Math.min(l.top,-l.bottom)}}}if(n.direction.x===true){if(l.left<0){o.scrollLeft=q.s.scroll.left+l.left}else{if(l.left>0&&l.right<0){o.scrollLeft=q.s.scroll.left+Math.min(l.left,-l.right)}}}if(!h.isEmptyObject(o)){f(m,o,n)}else{h.isFunction(n.complete)&&n.complete.call(m[0])}}return this}function i(){var m=g(arguments);var o=this.eq(0);var l=o.closest(":scrollable(vertical)");if(l.length>0){l=l.eq(0);var n={};n.scrollTop=l.scrollTop()+o.offset().top-l.offset().top-parseInt(m.topOffset,10);f(l,n,m)}return this}var b={auto:true,scroll:true,visible:false,hidden:false};h.extend(h.expr[":"],{scrollable:function(o,m,r,l){var q=d[typeof(r[3])==="string"&&r[3].toLowerCase()]||d.both;var p=(document.defaultView&&document.defaultView.getComputedStyle?document.defaultView.getComputedStyle(o,null):o.currentStyle);var s={x:b[p.overflowX.toLowerCase()]||false,y:b[p.overflowY.toLowerCase()]||false,isRoot:k.test(o.nodeName)};if(!s.x&&!s.y&&!s.isRoot){return false}var n={height:{scroll:o.scrollHeight,client:o.clientHeight},width:{scroll:o.scrollWidth,client:o.clientWidth},scrollableX:function(){return(s.x||s.isRoot)&&this.width.scroll>this.width.client},scrollableY:function(){return(s.y||s.isRoot)&&this.height.scroll>this.height.client}};return q.y&&n.scrollableY()||q.x&&n.scrollableX()}})})(jQuery);