├── bower.json ├── README.rdoc ├── jquery.scrollIntoView.min.js └── jquery.scrollIntoView.js /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jQuery.scrollIntoView", 3 | "version": "0.3.0", 4 | "authors": [ 5 | "Arwid Bancewicz e?e=n.offsetTop+n.offsetHeight:null)})}e-=h;var k=this.commonAncestor().get(0);var g=a(window).height();while(k){var d=k.scrollTop,l=k.clientHeight;if(l>g){l=g}if(l==0&&k.tagName=="BODY"){l=g}if((k.scrollTop!=((k.scrollTop+=1)==null||k.scrollTop)&&(k.scrollTop-=1)!=null)||(k.scrollTop!=((k.scrollTop-=1)==null||k.scrollTop)&&(k.scrollTop+=1)!=null)){if(h<=d){i(k,h)}else{if((h+e)>(d+l)){i(k,h+e-l)}else{i(k,undefined)}}return}k=k.parentNode}function i(n,m){if(m===undefined){if(a.isFunction(b.complete)){b.complete.call(n)}}else{if(b.smooth){a(n).stop().animate({scrollTop:m},b)}else{n.scrollTop=m;if(a.isFunction(b.complete)){b.complete.call(n)}}}}return this};a.fn.scrollIntoView.defaults={smooth:true,duration:null,easing:a.easing&&a.easing.easeOutExpo?"easeOutExpo":null,complete:a.noop(),step:null,specialEasing:{}};a.fn.isOutOfView=function(b){var c=true;this.each(function(){var h=this.parentNode,d=h.scrollTop,g=h.clientHeight,f=this.offsetTop,e=this.offsetHeight;if(b?(f)>(d+g):(f+e)>(d+g)){}else{if(b?(f+e)elH?elH=el.offsetTop+el.offsetHeight:null)}); 36 | elH -= elY; 37 | 38 | // start from the common ancester 39 | var pEl = this.commonAncestor().get(0); 40 | 41 | var wH = $(window).height(); 42 | 43 | // go up parents until we find one that scrolls 44 | while (pEl) { 45 | var pY = pEl.scrollTop, pH = pEl.clientHeight; 46 | if (pH > wH) pH = wH; 47 | 48 | // case: if body's elements are all absolutely/fixed positioned, use window height 49 | if (pH == 0 && pEl.tagName == "BODY") pH = wH; 50 | 51 | if ( 52 | // it wiggles? 53 | (pEl.scrollTop != ((pEl.scrollTop += 1) == null || pEl.scrollTop) && (pEl.scrollTop -= 1) != null) || 54 | (pEl.scrollTop != ((pEl.scrollTop -= 1) == null || pEl.scrollTop) && (pEl.scrollTop += 1) != null)) { 55 | if (elY <= pY) scrollTo(pEl, elY); // scroll up 56 | else if ((elY + elH) > (pY + pH)) scrollTo(pEl, elY + elH - pH); // scroll down 57 | else scrollTo(pEl, undefined) // no scroll 58 | return; 59 | } 60 | 61 | // try next parent 62 | pEl = pEl.parentNode; 63 | } 64 | 65 | function scrollTo(el, scrollTo) { 66 | if (scrollTo === undefined) { 67 | if ($.isFunction(opts.complete)) opts.complete.call(el); 68 | } else if (opts.smooth) { 69 | $(el).stop().animate({ scrollTop: scrollTo }, opts); 70 | } else { 71 | el.scrollTop = scrollTo; 72 | if ($.isFunction(opts.complete)) opts.complete.call(el); 73 | } 74 | } 75 | return this; 76 | }; 77 | 78 | $.fn.scrollIntoView.defaults = { 79 | smooth: true, 80 | duration: null, 81 | easing: $.easing && $.easing.easeOutExpo ? 'easeOutExpo': null, 82 | // Note: easeOutExpo requires jquery.effects.core.js 83 | // otherwise jQuery will default to use 'swing' 84 | complete: $.noop(), 85 | step: null, 86 | specialEasing: {} // cannot be null in jQuery 1.8.3 87 | }; 88 | 89 | /* 90 | Returns whether the elements are in view 91 | */ 92 | $.fn.isOutOfView = function(completely) { 93 | // completely? whether element is out of view completely 94 | var outOfView = true; 95 | this.each(function() { 96 | var pEl = this.parentNode, pY = pEl.scrollTop, pH = pEl.clientHeight, elY = this.offsetTop, elH = this.offsetHeight; 97 | if (completely ? (elY) > (pY + pH) : (elY + elH) > (pY + pH)) {} 98 | else if (completely ? (elY + elH) < pY: elY < pY) {} 99 | else outOfView = false; 100 | }); 101 | return outOfView; 102 | }; 103 | 104 | /* 105 | Returns the common ancestor of the elements. 106 | It was taken from http://stackoverflow.com/questions/3217147/jquery-first-parent-containing-all-children 107 | It has received minimal testing. 108 | */ 109 | $.fn.commonAncestor = function() { 110 | var parents = []; 111 | var minlen = Infinity; 112 | 113 | $(this).each(function() { 114 | var curparents = $(this).parents(); 115 | parents.push(curparents); 116 | minlen = Math.min(minlen, curparents.length); 117 | }); 118 | 119 | for (var i = 0; i < parents.length; i++) { 120 | parents[i] = parents[i].slice(parents[i].length - minlen); 121 | } 122 | 123 | // Iterate until equality is found 124 | for (var i = 0; i < parents[0].length; i++) { 125 | var equal = true; 126 | for (var j in parents) { 127 | if (parents[j][i] != parents[0][i]) { 128 | equal = false; 129 | break; 130 | } 131 | } 132 | if (equal) return $(parents[0][i]); 133 | } 134 | return $([]); 135 | } 136 | 137 | })(jQuery); --------------------------------------------------------------------------------