UNPKG

stroll.js

Version:

stroll.js is strolling along in your viewport

1 lines 4.74 kB
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):t.stroll=n()}(this,function(){"use strict";function t(t,n,e){return Object.assign({ignoreUserScroll:!1,allowInvalidPositions:!1,offset:{x:0,y:0},duration:500,focus:!0,easing:function(t,n,e,r){return(t/=r/2)<1?e/2*t*t+n:(t--,-e/2*(t*(t-2)-1)+n)}},t,n,{start:e.getCurrentPosition()})}function n(t,n){var e=void 0===t?"undefined":m(t);if("number"===e)return t;if("function"===e)return t(n);var r=parseFloat(t);if(isNaN(r))throw new Error("invalid duration");return r}function e(t,n){var e=void 0===t?"undefined":m(t);if("number"===e)return n.createPositionFromNumber(t);if("object"===e)return void 0===t.x&&(t.x=0),void 0===t.y&&(t.y=0),t;throw new Error("invalid offset")}function r(t){return{x:Math.abs(t.x),y:Math.abs(t.y)}}function o(t,n){return t.x===n.x&&t.y===n.y}function i(t,n){return{x:t.x+n.x,y:t.y+n.y}}function u(t,n,e,r,o){return{x:Math.round(t(n,e.x,r.x,o)),y:Math.round(t(n,e.y,r.y,o))}}function l(t,n){function e(e){function i(i){a||(a=i);var f=i-a,d=u(n.easing,f,n.start,n.target,n.duration);return!n.allowInvalidPositions&&l.isPositionOutsideOfElement(d)?t.currentStroll("invalid_position"):n.ignoreUserScroll||!s||o(s,l.getCurrentPosition())?void(f>n.duration?r(e):(l.scrollTo(d),s=l.getCurrentPosition(),c())):t.currentStroll("user_scrolled")}function c(){f=window.requestAnimationFrame(i)}var f=void 0,a=void 0,s=void 0;return t.currentStroll&&t.currentStroll("new_stroll"),c(),t.currentStroll=function(t){window.cancelAnimationFrame(f),e({wasCancelled:!0,cancelReason:t})},t.currentStroll}function r(e){t.currentStroll=null,l.scrollTo(i(n.start,n.target)),n.element&&n.focus&&(n.element.hasAttribute("tabindex")||n.element.setAttribute("tabindex","-1"),n.element.focus()),e({wasCancelled:!1})}var l=n.adapter;return{start:e}}function c(o){function i(u){var c=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return new Promise(function(f){var a=t(i.DEFAULTS,c,o),s=e(a.offset,o),d=o.resolveTarget(u,a.start,s),y=n(a.duration,r(d.target));l(i,Object.assign({},a,d,{duration:y,offset:s,adapter:o})).start(f)})}return i.currentStroll=null,i.relative=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return i(null,Object.assign({offset:t},n))},i.DEFAULTS={},i}function f(t,n){return function e(r,o,i){var u=void 0===r?"undefined":x(r),l="string"===u?t.querySelector(r):r,c={};if(null===r||"undefined"===u){var f={x:0,y:0};return e(i,f,f)}if("number"===u){var a=n(r);return a.x=a.x-o.x+i.x,a.y=a.y-o.y+i.y,c.target=a,c.focus=!1,c}if("object"===u&&!(r instanceof v.HTMLElement))return c.target={x:(r.x||0)-o.x+i.x,y:(r.y||0)-o.y+i.y},c.focus=!1,c;if(!l||!l.getBoundingClientRect)throw new Error("invalid target");var s=l.getBoundingClientRect();return c.element=l,c.target={x:s.left+i.x,y:s.top+i.y},c}}function a(t){return function(n){var e=t(),r=e.y>e.x?"y":"x",o={};return o[r]=n,o["x"===r?"y":"x"]=0,o}}function s(t){return function(n){if("object"!==(void 0===n?"undefined":x(n)))return!0;var e=t();return n.x<0||n.x>e.x||n.y<0||n.y>e.y}}function d(t,n){var e=a(n),r=s(n);return{createPositionFromNumber:e,resolveTarget:f(t,e),isPositionOutsideOfElement:r}}function y(t){function n(){return{x:Math.max(0,t.scrollWidth-t.clientWidth),y:Math.max(0,t.scrollHeight-t.clientHeight)}}return Object.assign(d(t,n),{getCurrentPosition:function(){return{x:t.scrollLeft,y:t.scrollTop}},scrollTo:function(n){t.scrollLeft=n.x,t.scrollTop=n.y}})}var m="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},x="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},g="undefined"!=typeof document?document:{},v="undefined"!=typeof window?window:{},b={element:y},h=c(function(){function t(){var t=Math.max(g.body.scrollWidth,g.body.offsetWidth,g.documentElement.clientWidth,g.documentElement.scrollWidth,g.documentElement.offsetWidth)-v.innerWidth,n=Math.max(0,t),e=Math.max(g.body.scrollHeight,g.body.offsetHeight,g.documentElement.clientHeight,g.documentElement.scrollHeight,g.documentElement.offsetHeight)-v.innerHeight;return{x:n,y:Math.max(0,e)}}return Object.assign(d(g,t),{getCurrentPosition:function(){return{x:v.scrollX||v.pageXOffset,y:v.scrollY||v.pageYOffset}},scrollTo:function(t){v.scrollTo(t.x,t.y)}})}());return h.factory=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"element";return"string"==typeof n&&(n=b[n]),c(n(t))},h});