-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.scrollable-sticky.min.js
9 lines (9 loc) · 1.68 KB
/
jquery.scrollable-sticky.min.js
1
2
3
4
5
6
7
8
9
/*
* jQuery Scrollable Sticky - v0.1.0
* A jQuery plugin which enables fixed positioned elements, that are higher than the viewport, to be scrolled.
* https://github.com/egeriis/jquery-scrollable-sticky/
*
* Made by Ronni Egeriis Persson
* Under MIT License
*/
!function(a,b){function c(b,c){this.el=b,this.$el=a(b),this.settings=a.extend({},e,c),this._defaults=e,this._name=d,this.init()}var d="scrollableSticky",e={offset:0};c.prototype={init:function(){this.offset=this.$el.offset(),this.parentsOffset=this.$el.offsetParent().offset(),this.offsetParent={top:this.offset.top-this.parentsOffset.top,left:this.offset.left-this.parentsOffset.left},this.dimensions={width:this.$el.width(),height:this.$el.height()},this.scrollTop=0,this.adjustmentStart=0,a(b).scroll(a.proxy(this.onScroll,this)),this.placeElement()},placeElement:function(){this.$el.css({position:"absolute",top:this.offsetParent.top,left:this.offsetParent.left,width:this.dimensions.width})},placeElementFixed:function(a){this.$el.css({position:"fixed",top:a||this.offset.top,left:this.offset.left})},onScroll:function(){var c=a(b).height(),d=a(b).scrollTop();if(c>this.dimensions.height)return this.placeElementFixed();var e=this.dimensions.height-c+this.offset.top+this.settings.offset,f=0;if(this.adjustmentStart>0?(f=this.adjustmentStart-d,0>f?this.adjustmentStart=0:f>e&&(this.adjustmentStart=d+e)):d<this.scrollTop&&(this.adjustmentStart=d),d>e-f){var g=-1*e+this.offset.top;f>=0&&(g+=Math.min(f,e)),this.placeElementFixed(g)}else this.adjustmentStart=0,this.placeElement();this.scrollTop=d}},a.fn[d]=function(b){return this.each(function(){a.data(this,"plugin_"+d)||a.data(this,"plugin_"+d,new c(this,b))}),this}}(jQuery,window,document);