butter-slider
Version:
Simple hold and drag slider
17 lines (14 loc) • 8.77 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var t=function(){return(t=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var i in e=arguments[o])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function e(){for(var t=0,e=0,o=arguments.length;e<o;e++)t+=arguments[e].length;var n=Array(t),i=0;for(e=0;e<o;e++)for(var s=arguments[e],r=0,a=s.length;r<a;r++,i++)n[i]=s[r];return n}var o=function(t){return!!(t instanceof Element||t instanceof HTMLDocument)},n=function(t,e,o,n){if(isNaN(+t))return+e.toFixed(3);var i=parseFloat((+t).toFixed(3));return i>n?+n.toFixed(3):i<o?+o.toFixed(3):i},i=function(t){return!(!t||"function"!=typeof t)},s=function(t){return t.targetTouches?t.targetTouches[0]:t},r=function(e){var r,a,l,u,d,c,h,v,m,g,f,p,T=this;if(this.getScrollWidth=function(){return T.sliderTag.scrollWidth-T.containerTag.offsetWidth+T.sliderTagLeft+T.sliderTagRight},this.callCallback=function(t,e){var o,n,s,r,a;switch(t){case"mousedown":i(null===(o=T.options)||void 0===o?void 0:o.mouseDown)&&T.options.mouseDown();break;case"mouseleave":i(null===(n=T.options)||void 0===n?void 0:n.mouseLeave)&&T.options.mouseLeave();break;case"mouseup":i(null===(s=T.options)||void 0===s?void 0:s.mouseUp)&&T.options.mouseUp();break;case"mousemove":i(null===(r=T.options)||void 0===r?void 0:r.mouseEnter)&&T.options.mouseEnter();break;case"getscrollpercent":i(null===(a=T.options)||void 0===a?void 0:a.getScrollPercent)&&T.options.getScrollPercent(e);break;default:console.warn("No default callback")}},this.mousedown=function(t){T.isAnimating||T.anime();var e=s(t);T.down=!0,T.startX=e.pageX-T.sliderTag.offsetLeft,T.scrollLeft=T.scrollAmount,T.sliderTag.classList.add("active"),T.callCallback("mousedown",null)},this.mouseleave=function(){T.down=!1,T.sliderTag.classList.remove("active"),T.callCallback("mouseleave",null)},this.mouseup=function(){T.down=!1,T.sliderTag.classList.remove("active"),T.callCallback("mouseup",null)},this.mousemove=function(t){T.callCallback("mousemove",null);var e=s(t);T.down&&(t.preventDefault(),T.x=e.pageX-T.sliderTag.offsetLeft,T.dist=T.scrollLeft-(T.x-T.startX)*T.dragSpeed)},this.transformElement=function(){var t=-T.scrollAmount.toFixed(3);T.sliderTag.style.transform="translate3D("+t+"px, 0, 0)",T.sliderTag.style.webkitTransform="translate3D("+t+"px, 0, 0)",T.sliderTag.style.msTransform="translate3D("+t+"px, 0, 0)"},this.getScrollPercent=function(){var t,e,o,n,i=(t=T.scrollAmount,e=0,o=T.sliderTag.scrollWidth-T.sliderTag.offsetWidth,(t-e)*(100-(n=0))/(o-e)+n);T.callCallback("getscrollpercent",+i.toFixed(3))},this.anime=function(){var t,e,o;T.isAnimating=!0,T.dist+T.scrollAmount<=0?T.dist=0:T.dist>=T.scrollWidth&&(T.dist=T.scrollWidth),T.scrollAmount=(t=T.scrollAmount,e=T.dist,o=T.smoothAmount,t*(1-o)+e*o),T.transformElement(),T.getScrollPercent(),T.stopAnimation?cancelAnimationFrame(T.animationRef):T.animationRef=requestAnimationFrame(T.anime)},this.setRelativePosition=function(t){T.x=T.sliderTag.offsetLeft-t,T.startX=T.sliderTag.offsetLeft,T.scrollLeft=T.scrollAmount,T.dist=T.scrollLeft-(T.x-T.startX)*T.dragSpeed,T.dist+T.scrollAmount<=0||T.dist>=T.scrollWidth||(T.sliderTag.classList.add("active"),T.anime(),T.sliderTag.classList.remove("active"))},this.init=function(){T.isAnimating=!1,T.stopAnimation=!1,T.sliderTag.style.willChange="transform",T.getScrollPercent();var t="ontouchstart"in window||navigator.maxTouchPoints>0||navigator.msMaxTouchPoints>0;t?t&&T.options.hasTouchEvent?(T.containerTag.addEventListener("touchstart",T.mousedown),T.containerTag.addEventListener("touchleave",T.mouseleave),T.containerTag.addEventListener("touchend",T.mouseup),T.containerTag.addEventListener("touchmove",T.mousemove)):t&&!T.options.hasTouchEvent&&(T.containerTag.style.overflowX="scroll"):(T.containerTag.addEventListener("mousedown",T.mousedown),T.containerTag.addEventListener("mouseleave",T.mouseleave),T.containerTag.addEventListener("mouseup",T.mouseup),T.containerTag.addEventListener("mousemove",T.mousemove))},this.destroy=function(){T.stopAnimation=!0,T.containerTag.removeEventListener("mousedown",T.mousedown),T.containerTag.removeEventListener("mouseleave",T.mouseleave),T.containerTag.removeEventListener("mouseup",T.mouseup),T.containerTag.removeEventListener("mousemove",T.mousemove),T.containerTag.removeEventListener("touchstart",T.mousedown),T.containerTag.removeEventListener("touchleave",T.mouseleave),T.containerTag.removeEventListener("touchend",T.mouseup),T.containerTag.removeEventListener("touchmove",T.mousemove,!1)},this.options=t({},e),!this.options.container&&!this.options.slider||!this.options)throw new Error("No container and slider selector.");if(this.containerTag=o(null===(r=this.options)||void 0===r?void 0:r.container)?null===(a=this.options)||void 0===a?void 0:a.container:document.querySelector(null===(l=this.options)||void 0===l?void 0:l.container),this.sliderTag=o(null===(u=this.options)||void 0===u?void 0:u.slider)?null===(d=this.options)||void 0===d?void 0:d.slider:document.querySelector(null===(c=this.options)||void 0===c?void 0:c.slider),null===this.sliderTag)throw new Error("Target element does not exist on the page. "+this.sliderTag);if(null===this.containerTag)throw new Error("Target element does not exist on the page. { this.containerTag}");!0===(null===(h=this.options)||void 0===h?void 0:h.hasTouchEvent)&&"true"===(null===(v=this.options)||void 0===v?void 0:v.hasTouchEvent)?this.options.hasTouchEvent=!0:(null===(m=this.options)||void 0===m?void 0:m.hasTouchEvent)&&"false"!==(null===(g=this.options)||void 0===g?void 0:g.hasTouchEvent)||(this.options.hasTouchEvent=!1);var w=window.getComputedStyle(this.sliderTag).getPropertyValue("margin-left"),E=window.getComputedStyle(this.sliderTag).getPropertyValue("margin-right");this.sliderTagLeft=parseInt(w),this.sliderTagRight=parseInt(E),this.dragSpeed=n(null===(f=this.options)||void 0===f?void 0:f.dragSpeed,1,1,100),this.smoothAmount=n(null===(p=this.options)||void 0===p?void 0:p.smoothAmount,.15,.01,1),this.down=!1,this.startX=0,this.scrollLeft=0,this.isAnimating=!1,this.x=0,this.dist=0,this.scrollAmount=0,this.stopAnimation=!1,this.animationRef=null,this.scrollWidth=this.getScrollWidth(),this.init()},a=function(){var n=this;this.getOptions=function(n){var i=document.querySelector("[data-butter-"+n+"-options]");return o(i)?e(i.dataset[function(t){for(var e=t.toLowerCase().split("-"),o=1;o<e.length;o++)e[o]=e[o].charAt(0).toUpperCase()+e[o].substring(1);return e.join("")}("butter-"+n+"-options")].split(",")).reduce((function(o,n){var i={option:e(n.split(":"))[0],value:e(n.split(":"))[1]};return e(o,[t({},i)])}),[]).reduce((function(e,o){var n;return t(t({},e),((n={})[""+o.option]=o.value,n))}),{}):{}},this.getProgressBar=function(t){var e=Array.from(n.initBars).find((function(e){return e.dataset.butterProgress===t}));return o(e)?function(t){e.style.willChange="width",e.style.width=t+"%"}:null},this.getSlider=function(e){var i=e.dataset.butterContainer;if(!i)throw new Error("You need to add a unique id on `data-butter-container`");if(!o(e))throw new Error("No container was found for this slider : "+i);var s=Array.from(n.initSliders).find((function(t){return t.dataset.butterSlidable===i}));if(!o(s))throw new Error("No slidable element was found for this slider : "+i);return t(t({container:e,slider:s},n.getOptions(i)),{getScrollPercent:n.getProgressBar(i)})},this.init=function(){if(0===n.initContainers.length||0===n.initSliders.length)throw new Error("No container or slider selector.");n.initContainers.forEach((function(t){var e=n.getSlider(t),o=new r(e);n.sliders.push(o)}))},this.sliders=[],this.initContainers=document.querySelectorAll("[data-butter-container]"),this.initSliders=document.querySelectorAll("[data-butter-slidable]"),this.initBars=document.querySelectorAll("[data-butter-progress]"),this.init()};exports.CreateSlider=r,exports.autoInit=function(){return(new a).sliders};