Files
dis-2019/js/lib/_del-snap-scroll.min.js
2019-08-06 19:24:58 +02:00

6 lines
6.6 KiB
JavaScript

/*
* SnapScroll v1.3.0 Copyright (c) 2018 AJ Savino
* https://github.com/koga73/SnapScroll
* MIT License
*/
!function(h){function t(e){var n=null,t={callbackDelay:300,_lastOrientation:window.orientation,_timeout:0},i={init:function(){window.addEventListener?(window.addEventListener("resize",i._handler_resize,!1),window.addEventListener("orientationchange",i._handler_resize,!1)):window.attachEvent&&(window.attachEvent("onresize",i._handler_resize),window.attachEvent("onorientationchange",i._handler_resize))},destroy:function(){var e=t._timeout;e&&(clearTimeout(e),t._timeout=0),n.onResize=null,window.removeEventListener?(window.removeEventListener("resize",i._handler_resize),window.removeEventListener("orientationchange",i._handler_resize)):window.detachEvent&&(window.detachEvent("onresize",i._handler_resize),window.detachEvent("onorientationchange",i._handler_resize))},getWidth:function(){return window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},getHeight:function(){return window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},_handler_resize:function(){if("onorientationchange"in window){var e=window.orientation;if(e==t._lastOrientation)return;t._lastOrientation=e}t._timeout&&clearTimeout(t._timeout),t._timeout=setTimeout(i._handler_timeout,n.callbackDelay)},_handler_timeout:function(){clearTimeout(t._timeout),t._timeout=0,n.onResize(n.getWidth(),n.getHeight())}};for(var a in n={callbackDelay:t.callbackDelay,init:i.init,destroy:i.destroy,getWidth:i.getWidth,getHeight:i.getHeight,onResize:null},e)n[a]=e[a];return n.init(),n}h.fn.SnapScroll=function(e){var r=null,n={events:!0,eventChangeActive:"snapscroll_change-active",eventChangeVisible:"snapscroll_change-visible",classes:!0,classSnap:"snap-scroll",classVisible:"snap-scroll-visible",classActive:"snap-scroll-active",hashes:!1,scrollDelay:300,wheelInterval:1e3,animateDuration:250,animateTimeBuffer:100,snapTop:!0,snapBottom:!0,snaps:[],maxWheelDeviation:100};h.fn.SnapScroll.defaults=n;var l=100,c={_$this:this,_resizer:null,_snaps:null,_currentSnapIndex:-1,_scrollTimeout:0,_lastanimateDuration:0,_wheelDir:0,_lastWheelTime:0,_wheelEventDeltas:[],_wheelEventDeltaAvg:0,_lastWheelEventTime:0},_={init:function(){r.classes&&c._$this.addClass(r.classSnap),c._resizer=new t({onResize:_._handler_resize}),_._handler_resize(),h(document).on("scroll",_._handler_document_scroll),h(document).on("keydown",_._handler_document_keydown),h(window).on("DOMMouseScroll mousewheel wheel",_._handler_window_mousewheel)},destroy:function(){h(document).off("scroll",_._handler_document_scroll),h(document).off("keydown",_._handler_document_keydown),h(window).off("DOMMouseScroll mousewheel wheel",_._handler_window_mousewheel),c._resizer&&(c._resizer.destroy(),c._resizer=null),c._snaps=null,c._currentSnapIndex=-1,c._scrollTimeout&&(clearTimeout(c._scrollTimeout),c._scrollTimeout=0),c._lastanimateDuration=0,c._wheelDir=0,c._lastWheelTime=0,c._wheelEventDeltas.splice(0,c._wheelEventDeltas.length),c._wheelEventDeltaAvg=0,c._lastWheelEventTime=0,r.classes&&c._$this.removeClass(r.classSnap)},snapClosest:function(){for(var e=_._getScrollPosition(),n=-1,t=-1,i=c._snaps,a=i.length,s=0;s<a;s++){var l=Math.abs(i[s]-e);(-1==t||l<t)&&(t=l,n=s)}r.snapIndex(n)},snapPrev:function(){r.snapIndex(c._currentSnapIndex-1)},snapNext:function(){r.snapIndex(c._currentSnapIndex+1)},snapIndex:function(e){e=Math.min(Math.max(e,0),c._snaps.length-1),c._currentSnapIndex=e;var n=c._snaps[e],t=[],i=null;if(c._$this.each(function(){var e=h(this);i||e.offset().top!=n||(i=e),r.classes&&(i==e?e.hasClass(r.classActive)||(e.addClass(r.classActive),t.push(e)):e.hasClass(r.classActive)&&(e.removeClass(r.classActive),t.push(e)))}),r.events){var a=t.length;if(a)for(var s=0;s<a;s++)t[s].trigger(r.eventChangeActive,i)}if(r.hashes){var l="";if(i){var o=i.attr("id");o&&(l="#"+o)}history.replaceState({},window.location.href,window.location.href.replace(/#.*$/,"")+l)}_._evalVisibility(),_._scrollTo(n)},getSnapIndex:function(){return c._currentSnapIndex},update:function(){var e=r.snaps.concat(),t=h(document).height()-window.innerHeight;return c._$this.each(function(){e.push(h(this).offset().top)}),r.snapTop&&e.push(0),r.snapBottom&&e.push(t),(e=e.reduce(function(e,n){return-1==e.indexOf(n)&&n<=t&&e.push(n),e},[])).sort(_._sortNumeric),c._snaps=e,c._snaps},isVisible:function(e){var n=e.offset().top,t=n+e.height(),i=_._getScrollPosition(),a=i+window.innerHeight;return i<=n&&n<a||i<t&&t<=a},_evalVisibility:function(){if(r.classes){var n=[],t=[];if(c._$this.each(function(){var e=h(this);r.isVisible(e)?(e.hasClass(r.classVisible)||(e.addClass(r.classVisible),n.push(e)),t.push(e)):e.hasClass(r.classVisible)&&(e.removeClass(r.classVisible),n.push(e))}),r.events)for(var e=n.length,i=0;i<e;i++)n[i].trigger(r.eventChangeVisible,{data:t})}},_sortNumeric:function(e,n){return e-n},_handler_document_scroll:function(e){r.classes&&_._evalVisibility(),c._scrollTimeout&&clearTimeout(c._scrollTimeout);var n=c._lastanimateDuration+r.animateDuration+r.animateTimeBuffer-(new Date).getTime();c._scrollTimeout=setTimeout(_._handler_scroll_timeout,Math.max(r.scrollDelay,n))},_handler_scroll_timeout:function(){clearTimeout(c._scrollTimeout),c._scrollTimeout=0,r.snapClosest()},_scrollTo:function(e){if(_._getScrollPosition()!=e){c._lastanimateDuration=(new Date).getTime();var n=h("html,body");n.stop(!0),n.animate({scrollTop:e},r.animateDuration)}},_handler_window_mousewheel:function(e){e.preventDefault();var n=!1,t=(new Date).getTime();if(c._lastWheelEventTime){var i=t-c._lastWheelEventTime,a=c._wheelEventDeltas.length;a==l&&c._wheelEventDeltas.shift(),c._wheelEventDeltas.push(i),a++,c._wheelEventDeltaAvg=(c._wheelEventDeltaAvg*(a-1)+i)/a,Math.abs(i-c._wheelEventDeltaAvg)>=r.maxWheelDeviation&&(c._wheelEventDeltas.splice(0,a),n=!0)}else n=!0;c._lastWheelEventTime=t;var s=Math.max(-1,Math.min(1,e.originalEvent.deltaY||e.originalEvent.wheelDelta||-e.originalEvent.detail));return c._wheelDir=Math.abs(s)/s,(n||(new Date).getTime()>=c._lastWheelTime+r.wheelInterval)&&_._handler_wheel_timeout(),!1},_handler_wheel_timeout:function(){c._lastWheelTime=(new Date).getTime(),c._wheelDir<0?r.snapPrev():0<c._wheelDir&&r.snapNext()},_handler_document_keydown:function(e){switch(e.keyCode){case 38:r.snapPrev();break;case 40:r.snapNext()}},_handler_resize:function(e,n){r.update(),-1==c._currentSnapIndex?r.snapClosest():r.snapIndex(c._currentSnapIndex)},_getScrollPosition:function(){return window.scrollY||document.body.scrollTop||document.documentElement.scrollTop}};return(r=h.extend({init:_.init,destroy:_.destroy,snapClosest:_.snapClosest,snapPrev:_.snapPrev,snapNext:_.snapNext,snapIndex:_.snapIndex,getSnapIndex:_.getSnapIndex,update:_.update,isVisible:_.isVisible},n,e)).init(),r}}(jQuery);