forked from Arwid/jQuery.scrollIntoView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scrollIntoView.min.js
19 lines (16 loc) · 2.01 KB
/
jquery.scrollIntoView.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
$.fn.scrollIntoView - similar to the default browser scrollIntoView
The default browser behavior always places the element at the top or bottom of its container.
This override is smart enough to not scroll if the element is already visible.
Original script was developed by Arwid Bancewicz. Here follows the original licensing:
Copyright 2011 Arwid Bancewicz
Licensed under the MIT license
http://www.opensource.org/licenses/mit-license.php
@date 15 Feb 2016
@author Arwid Bancewicz http://arwid.ca (up to version 0.3)
@author Leonardo Fernandes <leonardo.monteiro.fernandes@gmail.com>
@version 0.5
*/
(function(b){var k=function(){for(var a=this.parents(),c=0;c<a.length;c++){var d=b(a[c]),e=!0;this.each(function(){if(!d.has(this))return e=!1});if(e)return d}return b([])},l=function(){for(var a=k.call(this);!a.is("html");){var b=a.get(0).scrollTop;a.get(0).scrollTop=b;a.get(0).scrollTop++;if(a.get(0).scrollTop!=b){a.get(0).scrollTop=b;break}a.get(0).scrollTop=b;a.get(0).scrollTop--;if(a.get(0).scrollTop!=b){a.get(0).scrollTop=b;break}a=a.parent()}return a},h=function(a,c,d){d.smooth?a.stop().animate({scrollTop:a.get(0).scrollTop+
c},d):(a.get(0).scrollTop+=c,b.isFunction(d.complete)&&d.complete.call(a.get(0)))};b.fn.scrollIntoView=function(a,c,d){if(0==this.length)return this;var e=b.extend({},b.fn.scrollIntoView.defaults);"object"==b.type(a)?b.extend(e,a):"number"==b.type(a)?b.extend(e,{duration:a,easing:c,complete:d}):0==a&&(e.smooth=!1);var f=Infinity,g=0;this.each(function(){b(this).offset().top<f&&(f=b(this).offset().top);b(this).offset().top+b(this).outerHeight(!1)>f+g&&(g=b(this).offset().top+b(this).outerHeight(!1)-
f)});a=l.call(this);c=a.offset().top;d=a.height();if(a.is("html")||a.is("body"))c=a.get(0).scrollTop,d=b(window).height();f<=c||g>d?h(a,f-c,e):f+g>c+d?h(a,f+g-c-d,e):b.isFunction(e.complete)&&e.complete.call(el);return this};b.fn.scrollIntoView.defaults={smooth:!0,duration:null,easing:b.easing&&b.easing.easeOutExpo?"easeOutExpo":null,complete:b.noop(),step:null,specialEasing:{}}})(jQuery);