-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fixedscroll.min.js
17 lines (17 loc) · 2.83 KB
/
jquery.fixedscroll.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* jquery.fixedscroll.js
* Copyright (c) 2012 Josep del Rio (http://www.joseprio.com/)
* Licensed under the MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* @author Josep del Rio
*
* @projectDescription jQuery plugin to make areas of the page follow other content while scrolling..
*
* @version 0.1.0
*
* @requires jquery.js (tested with 1.8.2)
*
* @param follow string or jQuery container - string containing a jQuery selector, or jQuery container; will be used as the box to follow.
* default: box's parent
*/
(function(e){function s(){var e=parseInt(r.height()),t=parseInt(i.scrollTop()),s=parseInt(i.height()),u=0,a=0,f=null;for(var l=0;l<n.length;l++)f!=n[l].follow&&(u=n[l].follow.cachedTop,a=parseInt(n[l].follow.innerHeight()),f=n[l].follow),o(n[l],e,t,s,u,a)}function o(e,t,n,r,i,s){var o=e.innerHeight(),u=parseInt(e.offset().top),a=parseInt(e.css("top"))||0;if(e.isFixed)if(o>s)e.css("top",""+(i-e.offsetDiff)+"px"),e.css("bottom","auto"),e.css("position","absolute"),e.isFixed=!1;else if(n<i)e.css("top",""+(i-e.offsetDiff)+"px"),e.css("bottom","auto"),e.css("position","absolute"),e.isFixed=!1;else if(n+t>i+s){if(o>t||o>i+s-n)e.css("top",""+(i+s-o-e.offsetDiff)+"px"),e.css("bottom","auto"),e.css("position","absolute"),e.isFixed=!1}else o>t&&(e.previousScroll-n>0?e.get(0).style.bottom!="auto"&&(e.previousScroll+t-o>n?(e.css("top",0),e.css("bottom","auto")):(e.css("top",""+(e.previousScroll+t-o)+"px"),e.css("bottom","auto"),e.css("position","absolute"),e.isFixed=!1)):e.previousScroll-n<0&&e.get(0).style.top!="auto"&&(e.previousScroll+o<n+t?(e.css("top","auto"),e.css("bottom",0)):(e.css("top",""+(e.previousScroll-e.offsetDiff)+"px"),e.css("bottom","auto"),e.css("position","absolute"),e.isFixed=!1)));else n<i||o>=s?(e.css("top",""+(i-e.offsetDiff)+"px"),e.css("bottom","auto")):n+t>i+s&&o>t?(e.css("top",""+(i+s-o-e.offsetDiff)+"px"),e.css("bottom","auto")):n>i&&(o<t||u>n)&&o<s&&o<i+s-n?(e.css("top",0),e.css("bottom","auto"),e.css("position","fixed"),e.isFixed=!0):n+t>u+o&&o>t&&o<s&&(e.css("top","auto"),e.css("bottom",0),e.css("position","fixed"),e.isFixed=!0);e.previousScroll=n}var t=!1,n=[],r=e(window),i=e(document);e.fixedScroll=function(u,a){u=e(u);var f=u.css("position");a.follow?(typeof a.follow=="string"?u.follow=e(a.follow):u.follow=a.follow,u.follow.cachedTop=parseInt(u.follow.offset().top)):u.follow=u.parent(),u.initialOffsetTop=parseInt(u.offset().top),u.initialTop=parseInt(u.css("top"))||0,u.offsetDiff=u.initialOffsetTop-u.initialTop,t||(e(window).scroll(function(){s()}),e(window).resize(function(){s()}),t=!0),n.push(u);var l=parseInt(r.height()),c=parseInt(i.scrollTop()),h=parseInt(i.height()),p=u.follow.cachedTop,d=parseInt(u.follow.innerHeight());o(u,l,c,h,p,d)},e.fn.fixedScroll=function(t){return t=t||{},t.follow=t.follow||null,this.each(function(){new e.fixedScroll(this,t)}),this}})(jQuery);