@fancyapps/ui
Version:
Robust JavaScript UI Component Library
3 lines (2 loc) • 1.76 kB
JavaScript
/*! License details at fancyapps.com/license */
import{isPlainObject as e}from"../utils/isPlainObject.js";const t={autoStart:!0,speed:3},n=1/60*1e3*1,o=()=>{let o,i,s=null,u=!1,r=!1,a=!1,c=!1;function f(){const n=o?.getOptions().Autoscroll;return e(n)?{...t,...n}:t}function l(){s||(i=performance.now(),s=requestAnimationFrame(g))}function d(){s&&(cancelAnimationFrame(s),s=null)}function m(){if(o&&o.isInfinite()){if(!u){u=!0;const e=o?.getContainer();o?.getTween()?.on("start",w),e&&matchMedia("(hover: hover)").matches&&(e.addEventListener("mouseenter",p,!1),e.addEventListener("mouseleave",h,!1)),o.emit("autoscroll:start")}l()}}function v(){if(d(),u){u=!1,a=!1,o?.getTween()?.off("start",w);const e=o?.getContainer();e?.addEventListener("mouseenter",p,!1),e?.addEventListener("mouseleave",h,!1),o?.emit("autoscroll:end")}}function g(e){if(o&&u&&!a&&i){if(!o.isInTransition()){let t=e-i;t>n&&(t=n);const s=1/60*t,u=f().speed,a=f().speedOnHover;let c=s*(r&&void 0!==a?a:u);const l=o.getPosition()+c;o.setPosition(l)}s=null,l()}}function p(){r=!0;0===f().speedOnHover&&(c=!0,o?.getTween()?.pause(),d())}function h(){r=!1,c=!1,u&&!a&&l()}function E(){o&&!1!==o.getOptions().Autoscroll&&f().autoStart&&m()}function w(){d()}function L(){!u||a||c||l()}function T(){u&&(document.hidden?(o?.getTween()?.pause(),d()):a||c||l())}return{init:function(e){o=e,o.on("ready",E),o.on("settle",L),document.addEventListener("visibilitychange",T)},destroy:function(){c=!1,v(),o&&(o.off("ready",E),o.off("settle",L)),o=void 0,document.removeEventListener("visibilitychange",T)},isEnabled:()=>u,pause:function(){a||(a=!0,o?.getTween()?.pause(),d())},resume:function(){a=!1,u&&!c&&l()},start:function(){m()},stop:function(){v()},toggle(){u?v():m()}}};export{o as Autoscroll};