-
Notifications
You must be signed in to change notification settings - Fork 28
/
scrollmenu.min.js
8 lines (7 loc) · 7.21 KB
/
scrollmenu.min.js
1
2
3
4
5
6
7
8
/*
ScrollMenu v 1.0.4
Author: Sudhanshu Yadav
Copyright (c) 2015 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license.
Demo on: http://ignitersworld.com/lab/scrollmenu/
*/
!function(o,n,e,t){"use strict";function r(o,n){var t=/\W/.test(o)?new Function("obj","obj = obj || {}; var p=[],print=function(){p.push.apply(p,arguments);};p.push('"+o.replace(/[\r\t\n]/g," ").split("<%").join(" ").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',obj.$1 || \"\",'").split(" ").join("');").split("%>").join("p.push('").split("\r").join("\\'")+"');return p.join('');"):p[o]=p[o]||r(e.getElementById(o).innerHTML);return n?t(n):t}function l(n,t){var r=this;n&&o(n)[0]instanceof Element||(t=n,n=e.body),t=t||{},t=r.options=o.extend({},d,t),r.container=o(n),r.sections=r.container.find(r.options.sectionClass),r._vertical="vertical"==t.menuType,r._sectionTops=[],r._sectionHeights=[],r._scrollAnchorPos=[]}o.single=function(){var n=o({});return function(o){return n[0]=o,n}}();var i=o(e),s=o(n),c=o("body"),a=function(){return this||(1,eval)("this")}(),h=function(){},u=function(){var o=e.createElement("div");o.style.visibility="hidden",o.style.width="100px",o.style.msOverflowStyle="scrollbar",e.body.appendChild(o);var n=o.offsetWidth;o.style.overflow="scroll";var t=e.createElement("div");t.style.width="100%",o.appendChild(t);var r=t.offsetWidth;return o.parentNode.removeChild(o),n-r}(),p={},d={sectionClass:"section",menuType:"vertical",appendTo:null,animateOnScroll:!0,nativeScroll:!0,scrollbarVisible:!1,onhover:h,onhoverOut:h,onscrollToSection:h,menuMod:h,anchorSetup:[],animationDuration:600,scrollAnchorSpacing:10};l.prototype={constructor:l,_init:function(){var e=this,t=e.options,r=t.nativeScroll,l=e.container;e._prepareMenu();var a=e.scrollElm;return r&&a.on("scroll",function(){e._onScroll()}),e._setScrollAchorSize(),s.on("resize.scrollMenu",function(){e.resizeTimeout&&n.clearTimeout(e.resizeTimeout),e.resizeTimeout=setTimeout(function(){r&&(l.height(""),a.add(l).height(l.is("body,html")?s.height():l.height())),e._setScrollAchorSize()},200)}),e.scrollMenuWrap.on("mousedown touchstart",function(n){n.preventDefault();var r=o.single(n.target),l="touchstart"==n.type?"touchmove":"mousemove",s="touchstart"==n.type?"touchend":"mouseup";if(r.is(".scroll-handle")){c.addClass("scroll-menu-dragging"),e._setAnchorPos();var a=e._vertical?"clientY":"clientX",h=n[a]||n.originalEvent.touches[0][a],u=e.scrollTop(),p=e._inBoundry(h);i.on(l+".scrollMenu",function(o){o.preventDefault();var n=o[a]||o.originalEvent.touches[0][a],r=e._inBoundry(n);r!==!1&&e.scrollTo(u+(n-h-(r-p)*t.scrollAnchorSpacing)/e._sizeFactor,!1)}),i.one(s,function(){i.off(l+".scrollMenu"),c.removeClass("scroll-menu-dragging")})}else{var d=e.scrollAnchors.index(r.closest(".scroll-anchor"));if(-1==d)return;setTimeout(function(){e.scrollToSection(d)},0)}}),e.scrollAnchors.hover(function(n){var r=(o.single(this),e.scrollAnchors.index(this)),l=t.anchorSetup,i=l[r]&&l[r].onhover||t.onhover;o.single(this).find(".scroll-menu-content").addClass("hovered"),i.call(this,n,e.getIndexData(r))},function(n){var r=(o.single(this),e.scrollAnchors.index(this)),l=t.anchorSetup,i=l[r]&&l[r].onhoverOut||t.onhoverOut;o.single(this).find(".scroll-menu-content").removeClass("hovered"),i.call(this,n,e.getIndexData(r))}),r&&e._onScroll(),e._setScrollAchorSize(),e},_prepareMenu:function(){var n=this,e=n.container,t=n.options,l=t.scrollbarVisible;if(e.addClass("scroller-container"),t.nativeScroll){var i=l?"width:100%":"padding-right:10px; right:-"+(10+u)+"px";e.wrapInner('<div class="content-inner-wrapper"></div>').wrapInner('<div class="content-wrapper" style="'+i+'"></div>');var c=n.scrollElm=e.find(".content-wrapper");c.add(e).height(e.is("body")?s.height():e.height()),c.css("position","absolute")}for(var a=t.anchorSetup,h=['<div class="scroll-menu-wrapper '+n.options.menuType+" "+(t.className||"")+'">'],p=0,d=n.sections.length;d>p;p++){var v=a[p]||{},f=v.template||t.template||"",g=p==d-1?"":'style="margin-'+(n._vertical?"bottom":"right")+":"+t.scrollAnchorSpacing+'px"';f=f&&'<div class="scroll-menu-content <%= className %>" style="'+("horizontal-menu"!=n.options.menuType&&"background-color : <%= backgroundColor %>")+'">'+f+"</div>";var m='<div class="scroll-anchor" '+g+">"+f+'<div class="scroll-handle-wrap" style="background-color : <%= backgroundColor %>"><div class="scroll-handle"></div></div></div>';h.push(r(m,v))}h.push("</div>");var _=t.appendTo?o(t.appendTo):e;_.append(h.join(""));var S=n.scrollMenuWrap=_.find(".scroll-menu-wrapper");if(n.scrollAnchors=S.find(".scroll-anchor"),n.scrollHandles=S.find(".scroll-handle"),o.each(n.scrollAnchors,function(o){var e=a[o]&&a[o].menuMod||t.menuMod;e.call(this,n.getIndexData(o))}),l){S.css("padding-right",parseInt(S.css("padding-right"),10)+u+"px")}},scrollTop:function(){return this.scrollElm.scrollTop()},_scrollHeight:function(){var o=this;return(o.options.nativeScroll?o.scrollElm:o.container)[0].scrollHeight},_setAnchorPos:function(){var n=this,e=s.scrollTop(),t=s.scrollLeft(),r=n._vertical;n.scrollAnchors.each(function(l){var i=o(this),s=i.offset(),c=s.top-e,a=s.left-t;n._scrollAnchorPos[l]={min:r?c:a,max:r?c+i.height():a+i.width()}})},_inBoundry:function(o){for(var n,e=this._scrollAnchorPos,t=0,r=e.length;r>t;t++)if(n=e[t],o>=n.min&&o<=n.max)return t;return!1},getIndexData:function(o){var n=this;return{index:o,anchor:n.scrollAnchors[o],section:n.sections[o],anchorOptions:n.options.anchorSetup[o]}},_scrollTo:function(o,n,e){this.scrollElm.animate({scrollTop:o+"px"},n,e)},scrollTo:function(o,n,e){var r=this,l=r.options;e=e||("function"==typeof n?n:h),n=n==t||"function"==typeof n?!0:n;var i=l.animateOnScroll&&n?l.animationDuration:0;r._scrollTo(o,i,e)},scrollToSection:function(o,n,e){e=e||("function"==typeof n?n:h);var r=this,l=isNaN(o)?r.sections.index(r.sections.filter(o)):o,i=r.options,s=i.anchorSetup,c=r._sectionTops[l];c!=t&&r.scrollTo(c,n,function(){var o=r.getIndexData(l);(s[l]&&s[l].onscrollToSection||i.onscrollToSection)(o),e(o)})},_onScroll:function(){var o=this;o._updateScrollAnchors(o.scrollTop())},_setScrollAchorSize:function(){var n=this,e=n.options,t=n.container,r=n._vertical,l=n.scrollMenuWrap,i=t.is("body,html")?s.height():t.height(),c=t.width(),a=r?Math.min(i,l.innerHeight()):Math.min(c,l.innerWidth()||1e6);100>a&&(a=r?i:c),n.scrollHeight=n._scrollHeight();var h=parseInt(l.css(r?"padding-top":"padding-left"),10)||0,u=parseInt(l.css(r?"padding-bottom":"padding-right"),10)||0,p=(a-h-u-e.scrollAnchorSpacing*(n.scrollHandles.length-1))*i/n.scrollHeight;return n.scrollHandles[r?"height":"width"](p),n._sizeFactor=p/i,n.sections.each(function(e){var t=o.single(this),l=t.innerHeight(),s=t.position().top;n._sectionHeights[e]=l,n._sectionTops[e]=s,o.single(n.scrollAnchors[e])[r?"height":"width"](l*p/i)}),n},_updateScrollAnchors:function(n){var e=this;e.scrollAnchors.each(function(t){var r=o.single(e.scrollHandles[t]),l=100*(n-e._sectionTops[t])/e._sectionHeights[t];r.css(e._vertical?"top":"left",l+"%")})},refresh:function(){this._setScrollAchorSize()},destroy:function(){var o=this;o.scrollMenuWrap.remove(),o.scrollElm&&o.scrollElm.children().children().unwrap().unwrap(),o.container.removeClass("scroller-container"),s.off("resize.scrollMenu")}},a.ScrollMenu=function(o,n){return new l(o,n)._init()},a.ScrollMenu.defaults=d}(jQuery,window,document);