var scrollElement=function(t,i,e){this.scrollingElement=t,this.origOffset=t.parent().offset().top,this.currentPos=t.position().top,this.height=t.outerHeight(!0),this.width=t.parent().width(),this.top=t.offset().top,this.bottom=Math.ceil(t.offset().top+t.outerHeight(!0)),this.container={element:i,height:i.outerHeight(),bottom:i.outerHeight()+i.offset().top},this.offsetEl={element:e||null,height:e?e.outerHeight():0},this.viewport={height:window.innerHeight,bottom:window.innerHeight}};scrollElement.prototype.init=function(){this.distanceScrolled=$(window).scrollTop(),this.scrollDir=!1,this.isTallerThanViewport=this.height>this.viewport.height,this.distanceScrolled=$(window).scrollTop();var t=this;$(document).ready(function(){t.update()}),$(window).scroll(function(){t.update()}),$(window).resize(function(){t.handleResize()}),$("a").click(function(){setTimeout(function(){t.update()},100)})},scrollElement.prototype.handleResize=function(){this.viewport.height=window.innerHeight,this.isTallerThanViewport=this.height>this.viewport.height},scrollElement.prototype.changePosition=function(t,i,e,o){this.scrollingElement.css({position:t,top:i,bottom:e}),o?this.scrollingElement.css({width:o+"px"}):this.scrollingElement.css({width:"auto"})},scrollElement.prototype.update=function(){var t=this.scrollingElement,i=this.container.element;this.currentPos=t.position().top,this.height=t.outerHeight(!0),this.width=t.parent().width(),this.top=t.offset().top-parseInt(t.css("marginTop")),this.bottom=Math.ceil(this.top+this.height),this.container.height=i.outerHeight(),this.container.heightWithMargin=i.outerHeight(!0),this.container.bottom=this.container.height+i.offset().top,this.offsetEl.height=this.offsetEl.element?this.offsetEl.element.outerHeight():0,this.lastDistanceScrolled=this.distanceScrolled,this.distanceScrolled=$(window).scrollTop(),this.scrollDir=0<this.distanceScrolled-this.lastDistanceScrolled?"down":"up",this.viewport.bottom=window.innerHeight+this.distanceScrolled;var e=this.distanceScrolled>this.top-this.offsetEl.height?Math.floor(this.distanceScrolled-this.top):0,o=this.distanceScrolled+this.height-e+this.offsetEl.height>=this.container.bottom,h=this.distanceScrolled>this.origOffset-this.offsetEl.height,s=this.height>=this.container.height,n=this.isTallerThanViewport;this.container.heightWithMargin<this.height&&this.container.element.css("minHeight",this.height+"px"),h?o?this.changePosition("absolute",this.container.height-this.height,"auto",this.width):h&&!o&&(s?this.changePosition("relative","auto","auto"):n?this.distanceScrolled+this.offsetEl.height<=this.top&&"up"===this.scrollDir?this.changePosition("fixed",0,"auto",this.width):this.viewport.bottom>=this.bottom&&"down"===this.scrollDir?this.changePosition("fixed","auto",0,this.width):"fixed"===this.scrollingElement.css("position")&&this.changePosition("absolute",this.top-this.origOffset,"auto",this.width):this.changePosition("fixed",this.offsetEl.height,"auto",this.width)):this.changePosition("relative","auto","auto")},$.fn.scrollWithPage=function(t,i){var e=$(this),o=$(t),h=i?$(i):null;try{if(!o||null==o[0])throw new Error("ScrollWithPage: Container doesn't exist");if(!e||null==e[0])throw new Error("ScrollWithPage: Element doesn't exist")}catch(t){return void console.error(t)}var s=new scrollElement(e,o,h);s.init(),console.log(s)};
|