scroll-scout
Version:
scroll-scout
2 lines (1 loc) • 3.31 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.ScrollScout=n()}(this,function(){"use strict";function e(e){return e&&e.__esModule?e.default:e}function n(e,n){return n={exports:{}},e(n,n.exports),n.exports}for(var t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},r=n(function(e){(function(){var n,t,r;"undefined"!=typeof performance&&null!==performance&&performance.now?e.exports=function(){return performance.now()}:"undefined"!=typeof process&&null!==process&&process.hrtime?(e.exports=function(){return(n()-r)/1e6},t=process.hrtime,n=function(){var e;return e=t(),1e9*e[0]+e[1]},r=n()):Date.now?(e.exports=function(){return Date.now()-r},r=Date.now()):(e.exports=function(){return(new Date).getTime()-r},r=(new Date).getTime())}).call(t)}),o=r,i="undefined"==typeof window?t:window,u=["moz","webkit"],c="AnimationFrame",f=i["request"+c],l=i["cancel"+c]||i["cancelRequest"+c],a=0;!f&&a<u.length;a++)f=i[u[a]+"Request"+c],l=i[u[a]+"Cancel"+c]||i[u[a]+"CancelRequest"+c];if(!f||!l){var d=0,s=0,p=[],v=1e3/60;f=function(e){if(0===p.length){var n=o(),t=Math.max(0,v-(n-d));d=t+n,setTimeout(function(){var e=p.slice(0);p.length=0;for(var n=0;n<e.length;n++)if(!e[n].cancelled)try{e[n].callback(d)}catch(e){setTimeout(function(){throw e},0)}},Math.round(t))}return p.push({handle:++s,callback:e,cancelled:!1}),s},l=function(e){for(var n=0;n<p.length;n++)p[n].handle===e&&(p[n].cancelled=!0)}}var h=function(e){return f.call(i,e)},m=function(){l.apply(i,arguments)},w=function(){i.requestAnimationFrame=f,i.cancelAnimationFrame=l};h.cancel=m,h.polyfill=w;var y=n(function(e,n){function t(e){return e&&e.__esModule?e:{default:e}}function r(e){if(Array.isArray(e)){for(var n=0,t=Array(e.length);n<e.length;n++)t[n]=e[n];return t}return Array.from(e)}Object.defineProperty(n,"__esModule",{value:!0});var o=h,i=t(o),u=function(e){var n=void 0,t=function(t){return function(){n=null,e.apply(void 0,r(t))}},o=function(){for(var e=arguments.length,r=Array(e),o=0;o<e;o++)r[o]=arguments[o];null==n&&(n=(0,i.default)(t(r)))};return o.cancel=function(){return i.default.cancel(n)},o};n.default=u}),g=e(y);window.requestAnimationFrame||h.polyfill();var b=new function(){function e(e){h=e}function n(){l=window.innerHeight,a=window.scrollY||window.pageYOffset}function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:50;if(!f(e))return!1;var t=a,r=t+l,o=u(e),i=o+e.offsetHeight,c=n/100*l;return i>=t-c&&o<=r+c}function r(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:50;return new Promise(function(u){n(),t(e,r)?u(e):(o({element:e,threshold:r,resolve:u}),i(!0))})}function o(e){s++,d[s]=e}function i(e){var n=void 0;e?(n=!m&&"addEventListener",m=!0):(n="removeEventListener",m=!1),n&&p.forEach(function(e){return window[n](e,v)})}function u(e){return e.getBoundingClientRect().top+a}function c(){h(),n(),Object.keys(d).forEach(function(e){t(d[e].element,d[e].threshold)&&(d[e].resolve(d[e].element),delete d[e])}),0===Object.keys(d).length&&i(!1)}function f(e){return e.offsetHeight>0}var l=void 0,a=void 0,d={},s=0,p=["scroll","resize"],v=g(c),h=function(){},m=!1;return{becomesVisible:r,isVisible:t,setOnEvent:e,checkElements:c}};return b});