2017-01-27 16:07:14 -07:00
|
|
|
!function(window,document){"use strict";function Debouncer(callback){this.callback=callback,this.ticking=!1}function isDOMElement(obj){return obj&&"undefined"!=typeof window&&(obj===window||obj.nodeType)}function extend(object){if(arguments.length<=0)throw new Error("Missing arguments in extend function");var key,i,result=object||{};for(i=1;i<arguments.length;i++){var replacement=arguments[i]||{};for(key in replacement)"object"!=typeof result[key]||isDOMElement(result[key])?result[key]=result[key]||replacement[key]:result[key]=extend(result[key],replacement[key])}return result}function normalizeTolerance(t){return t===Object(t)?t:{down:t,up:t}}function addEventListenerWithOptions(target,type,handler,options){var optionsOrCapture=options;supportsCaptureOption||(optionsOrCapture=options.capture),target.addEventListener(type,handler,optionsOrCapture)}function removeEventListenerWithOptions(target,type,handler,options){var optionsOrCapture=options;supportsCaptureOption||(optionsOrCapture=options.capture),target.removeEventListener(type,handler,optionsOrCapture)}function Headroom(elems,options){options=extend(options,Headroom.options),this.lastKnownScrollY=0,this.elems=elems,this.debouncer=new Debouncer(this.update.bind(this)),this.tolerance=normalizeTolerance(options.tolerance),this.classes=options.classes,this.offset=options.offset,this.scroller=options.scroller,this.initialised=!1,this.onPin=options.onPin,this.onUnpin=options.onUnpin}var requestAnimationFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame;Debouncer.prototype={constructor:Debouncer,update:function(){this.callback&&this.callback(),this.ticking=!1},requestTick:function(){this.ticking||(requestAnimationFrame(this.rafCallback||(this.rafCallback=this.update.bind(this))),this.ticking=!0)},handleEvent:function(){this.requestTick()}};var supportsCaptureOption=!1;try{var opts=Object.defineProperty({},"capture",{get:function(){supportsCaptureOption=!0}});window.addEventListener("test",null,opts)}catch(e){}Headroom.prototype={constructor:Headroom,init:function(){for(var i=0,length=this.elems.length;i<length;i++)this.elems[i].classList.add(this.classes.initial);return this.attachEvent(),this},add:function(elem){elem.classList.add(this.classes.initial),this.elems.push(elem)},remove:function(elem){var classes=this.classes;elem.classList.remove(classes.unpinned,classes.pinned,classes.initial);var i=this.elems.indexOf(elem);i!=-1&&this.elems.splice(i,1)},destroy:function(){var classes=this.classes;this.initialised=!1;for(var i=0,length=this.elems.length;i<length;i++)this.elems[i].classList.remove(classes.unpinned,classes.pinned,classes.initial);removeEventListenerWithOptions(this.scroller,"scroll",this.debouncer,{capture:!1,passive:!0})},attachEvent:function(){this.initialised||(this.lastKnownScrollY=this.getScrollY(),this.initialised=!0,addEventListenerWithOptions(this.scroller,"scroll",this.debouncer,{capture:!1,passive:!0}),this.debouncer.handleEvent())},unpin:function(){for(var classes=this.classes,i=0,length=this.elems.length;i<length;i++){var classList=this.elems[i].classList;!classList.contains(classes.pinned)&&classList.contains(classes.unpinned)||(classList.add(classes.unpinned),classList.remove(classes.pinned),this.onUnpin&&this.onUnpin.call(this))}},pin:function(){for(var classes=this.classes,i=0,length=this.elems.length;i<length;i++){var classList=this.elems[i].classList;classList.contains(classes.unpinned)&&(classList.remove(classes.unpinned),classList.add(classes.pinned),this.onPin&&this.onPin.call(this))}},getScrollY:function(){var pageYOffset=this.scroller.pageYOffset;if(void 0!==pageYOffset)return pageYOffset;var scrollTop=this.scroller.scrollTop;return void 0!==scrollTop?scrollTop:(document.documentElement||document.body).scrollTop},toleranceExceeded:function(currentScrollY,direction){return Math.abs(currentScrollY-this.lastKnownScrollY)>=this.tolerance[direction]},shouldUnpin:function(currentScrollY,toleranceExceeded){var scrollingDown=currentScrollY>this.lastKnownScrollY,pastOffset=currentScrollY>=this
|