UNPKG

svelty-material

Version:

Svelte Materialify fork with ts support and updated scss.

101 lines (100 loc) 3 kB
var easeInOutQuad = function easeInOutQuad(t, b, c, d) { t /= d / 2; if (t < 1) return (c / 2) * t * t + b; t--; return (-c / 2) * (t * (t - 2) - 1) + b; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var jumper = function jumper() { var element = null; var start = 0; var stop = 0; var offset = 0; var easing = void 0; var a11y = false; var distance = 0; var duration = 0; var timeStart = void 0; var timeElapsed = void 0; var next = 0; var callback = void 0; function location() { return window.scrollY || window.pageYOffset; } function top(element) { return element?.getBoundingClientRect().top || 0 + start; } function loop(timeCurrent) { if (!timeStart) { timeStart = timeCurrent; } timeElapsed = timeCurrent - timeStart; next = typeof easing !== "undefined" ? easing(timeElapsed, start, distance, duration) : 0; window.scrollTo(0, next); timeElapsed < duration ? window.requestAnimationFrame(loop) : done(); } function done() { window.scrollTo(0, start + distance); if (element && a11y) { element.setAttribute("tabindex", "-1"); element.focus(); } if (typeof callback === "function") { callback(); } timeStart = undefined; } function jump(target, options = {}) { duration = options.duration || 1000; offset = options.offset || 0; callback = options.callback; easing = options.easing || easeInOutQuad; a11y = options.a11y || false; start = location(); switch (typeof target === "undefined" ? "undefined" : _typeof(target)) { case "number": element = undefined; a11y = false; stop = start + target; break; case "object": element = target; stop = top(element); break; case "string": element = document.querySelector(target); stop = top(element); break; } distance = stop - start + offset; switch (_typeof(options.duration)) { case "number": duration = options.duration || 0; break; case "function": duration = options.duration(distance); break; } window.requestAnimationFrame(loop); } return jump; }; var singleton = jumper(); export default singleton;