diff --git a/dist/scrollsnap-polyfill.bundled.js b/dist/scrollsnap-polyfill.bundled.js index 48e2742..fca36cc 100644 --- a/dist/scrollsnap-polyfill.bundled.js +++ b/dist/scrollsnap-polyfill.bundled.js @@ -1 +1 @@ -!function(e,t,n){"use strict";function r(e){return e.replace(/^\s+|\s+$/g,"")}function i(e,t){var n,r=0;if(!e||!t)return!1;for(;n=t[r++];)if(e===n)return!0;return!1}function o(e){return l.test(e)}function s(e){var t,n=0;for(this._rules=[];t=e[n++];)this._rules.push(new a(t))}function a(e){this._rule=e}function u(e){return this instanceof u?(this._options=e,e.keywords||(this._options={keywords:e}),this._promise=[],this._getStylesheets(),this._downloadStylesheets(),this._parseStylesheets(),this._filterCSSByKeywords(),this._buildMediaQueryMap(),this._reportInitialMatches(),void this._addMediaListeners()):new u(e)}var l=RegExp("^"+String({}.valueOf).replace(/[.*+?\^${}()|\[\]\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),c=function(){var e=t.getElementsByTagName("base")[0],n=/^([a-zA-Z:]*\/\/)/;return function(t){var r=!n.test(t)&&!e||t.replace(RegExp.$1,"").split("/")[0]===location.host;return r}}(),f={matchMedia:e.matchMedia&&e.matchMedia("only all").matches,nativeMatchMedia:o(e.matchMedia)},h=function(){function t(e){for(var t,n=0;t=e[n++];)u[t]||i(t,l)||l.push(t)}function n(){if(0===h.readyState||4===h.readyState){var e;(e=l[0])&&r(e),e||s()}}function r(e){f++,h.open("GET",e,!0),h.onreadystatechange=function(){4!=h.readyState||200!=h.status&&304!=h.status||(u[e]=h.responseText,l.shift(),n())},h.send(null)}function o(e){for(var t,n=0,r=0;t=e[n++];)u[t]&&r++;return r===e.length}function s(){for(var e;e=c.shift();)a(e.urls,e.fn)}function a(e,t){for(var n,r=[],i=0;n=e[i++];)r.push(u[n]);t.call(null,r)}var u={},l=[],c=[],f=0,h=function(){var t;try{t=new e.XMLHttpRequest}catch(n){t=new e.ActiveXObject("Microsoft.XMLHTTP")}return t}();return{request:function(e,r){c.push({urls:e,fn:r}),o(e)?s():(t(e),n())},clearCache:function(){u={}},_getRequestCount:function(){return f}}}(),d={_cache:{},clearCache:function(){d._cache={}},parse:function(e,t){function n(){return s(/^\{\s*/)}function i(){return s(/^\}\s*/)}function o(){var t,n=[];for(a(),u(n);"}"!=e.charAt(0)&&(t=w()||S());)n.push(t),u(n);return n}function s(t){var n=t.exec(e);if(n)return e=e.slice(n[0].length),n}function a(){s(/^\s*/)}function u(e){e=e||[];for(var t;t=l();)e.push(t);return e}function l(){if("/"==e[0]&&"*"==e[1]){for(var t=2;"*"!=e[t]||"/"!=e[t+1];)++t;t+=2;var n=e.slice(2,t-2);return e=e.slice(t),a(),{comment:n}}}function c(){var e=s(/^([^{]+)/);if(e)return r(e[0]).split(/\s*,\s*/)}function f(){var e=s(/^(\*?[\-\w]+)\s*/);if(e&&(e=e[0],s(/^:\s*/))){var t=s(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)\s*/);if(t)return t=r(t[0]),s(/^[;\s]*/),{property:e,value:t}}}function h(){for(var e,t=[];e=s(/^(from|to|\d+%|\.\d+%|\d+\.\d+%)\s*/);)t.push(e[1]),s(/^,\s*/);return t.length?{values:t,declarations:L()}:void 0}function p(){var e=s(/^@([\-\w]+)?keyframes */);if(e){var t=e[1],e=s(/^([\-\w]+)\s*/);if(e){var r=e[1];if(n()){u();for(var o,a=[];o=h();)a.push(o),u();if(i()){var l={name:r,keyframes:a};return t&&(l.vendor=t),l}}}}}function m(){var e=s(/^@supports *([^{]+)/);if(e){var t=r(e[1]);if(n()){u();var a=o();if(i())return{supports:t,rules:a}}}}function y(){var e=s(/^@media *([^{]+)/);if(e){var t=r(e[1]);if(n()){u();var a=o();if(i())return{media:t,rules:a}}}}function v(){var e=s(/^@page */);if(e){var t=c()||[],r=[];if(n()){u();for(var o;o=f()||x();)r.push(o),u();if(i())return{type:"page",selectors:t,declarations:r}}}}function x(){var e=s(/^@([a-z\-]+) */);if(e){var t=e[1];return{type:t,declarations:L()}}}function _(){return E("import")}function g(){return E("charset")}function M(){return E("namespace")}function E(e){var t=s(new RegExp("^@"+e+" *([^;\\n]+);\\s*"));if(t){var n={};return n[e]=r(t[1]),n}}function L(){var e=[];if(n()){u();for(var t;t=f();)e.push(t),u();if(i())return e}}function w(){return p()||y()||m()||_()||g()||M()||v()}function S(){var e=c();if(e)return u(),{selectors:e,declarations:L()}}return t&&d._cache[t]?d._cache[t]:(e=e.replace(/\/\*[\s\S]*?\*\//g,""),d._cache[t]=o())},filter:function(e,t){function n(e,t){return e||t?e?e.concat(t):[t]:void 0}function i(e){null==e.media&&delete e.media,null==e.supports&&delete e.supports,c.push(e)}function o(e,t){if(t)for(var n=t.length;n--;)if(e.indexOf(t[n])>=0)return!0}function s(e,t){for(var n,i,o,s,a=/\*/,u=0;n=t[u++];)if(i=n.split(":"),o=new RegExp("^"+r(i[0]).replace(a,".*")+"$"),s=new RegExp("^"+r(i[1]).replace(a,".*")+"$"),o.test(e.property)&&s.test(e.value))return!0}function a(e,n,r){return t.selectors&&o(e.selectors.join(","),t.selectors)?(i({media:n,supports:r,selectors:e.selectors,declarations:e.declarations}),!0):void 0}function u(e,n,r){if(t.declarations)for(var o,a=0;o=e.declarations[a++];)if(s(o,t.declarations))return i({media:n,supports:r,selectors:e.selectors,declarations:e.declarations}),!0}function l(e,t,r){for(var i,o=0;i=e[o++];)i.declarations?a(i,t,r)||u(i,t,r):i.rules&&i.media?l(i.rules,n(t,i.media),r):i.rules&&i.supports&&l(i.rules,t,n(r,i.supports))}var c=[];return l(e),c}},p=function(){function n(){if(o)return o;var e=t.documentElement,n=t.body,r=e.style.fontSize,i=n.style.fontSize,s=t.createElement("div");return e.style.fontSize="1em",n.style.fontSize="1em",n.appendChild(s),s.style.width="1em",s.style.position="absolute",o=s.offsetWidth,n.removeChild(s),n.style.fontSize=i,e.style.fontSize=r,o}function r(t){return e.matchMedia(t)}function i(e){var r,i,o=!1;return s=t.documentElement.clientWidth,a.test(e)&&(r="em"===RegExp.$2?parseFloat(RegExp.$1)*n():parseFloat(RegExp.$1)),u.test(e)&&(i="em"===RegExp.$2?parseFloat(RegExp.$1)*n():parseFloat(RegExp.$1)),r&&i?o=s>=r&&i>=s:(r&&s>=r&&(o=!0),i&&i>=s&&(o=!0)),{matches:o,media:e}}var o,s,a=/\(min\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/,u=/\(max\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/,l={};return{matchMedia:function(e){return f.matchMedia?r(e):i(e)},clearCache:function(){f.nativeMatchMedia||(s=null,l={})}}}(),m=function(){function t(e,t){var n;return function(){clearTimeout(n),n=setTimeout(e,t)}}var n=function(){var e=[];return{add:function(t,n,r){for(var i,o=0;i=e[o++];)if(i.polyfill==t&&i.mql===n&&i.fn===r)return!1;n.addListener(r),e.push({polyfill:t,mql:n,fn:r})},remove:function(t){for(var n,r=0;n=e[r++];)n.polyfill===t&&(n.mql.removeListener(n.fn),e.splice(--r,1))}}}(),r=function(t){function n(){for(var e,n=0;e=t[n++];)e.fn()}return{add:function(r,i){t.length||(e.addEventListener?e.addEventListener("resize",n,!1):e.attachEvent("onresize",n)),t.push({polyfill:r,fn:i})},remove:function(r){for(var i,o=0;i=t[o++];)i.polyfill===r&&t.splice(--o,1);t.length||(e.removeEventListener?e.removeEventListener("resize",n,!1):e.detachEvent&&e.detachEvent("onresize",n))}}}([]);return{removeListeners:function(e){f.nativeMatchMedia?n.remove(e):r.remove(e)},addListeners:function(e,i){function o(){if(f.nativeMatchMedia)for(var o in a)a.hasOwnProperty(o)&&!function(t,r){n.add(e,t,function(){i.call(e,r,t.matches)})}(a[o],o);else{var u=t(function(e,t){return function(){s(e,t)}}(e,a),e._options.debounceTimeout||100);r.add(e,u)}}function s(e,t){var n,r={};p.clearCache();for(n in t)t.hasOwnProperty(n)&&(r[n]=p.matchMedia(n).matches,r[n]!=u[n]&&i.call(e,n,p.matchMedia(n).matches));u=r}var a=e._mediaQueryMap,u={};!function(){for(var e in a)a.hasOwnProperty(e)&&(u[e]=p.matchMedia(e).matches)}(),o()}}}();s.prototype.each=function(e,t){var n,r=0;for(t||(t=this);n=this._rules[r++];)e.call(t,n)},s.prototype.size=function(){return this._rules.length},s.prototype.at=function(e){return this._rules[e]},a.prototype.getDeclaration=function(){for(var e,t={},n=0,r=this._rule.declarations;e=r[n++];)t[e.property]=e.value;return t},a.prototype.getSelectors=function(){return this._rule.selectors.join(", ")},a.prototype.getMedia=function(){return this._rule.media.join(" and ")},u.prototype.doMatched=function(e){return this._doMatched=e,this._resolve(),this},u.prototype.undoUnmatched=function(e){return this._undoUnmatched=e,this._resolve(),this},u.prototype.getCurrentMatches=function(){for(var e,t,n=0,r=[];e=this._filteredRules[n++];)t=e.media&&e.media.join(" and "),(!t||p.matchMedia(t).matches)&&r.push(e);return new s(r)},u.prototype.destroy=function(){this._undoUnmatched&&(this._undoUnmatched(this.getCurrentMatches()),m.removeListeners(this))},u.prototype._defer=function(e,t){e.call(this)?t.call(this):this._promise.push({condition:e,callback:t})},u.prototype._resolve=function(){for(var e,t=0;e=this._promise[t];)e.condition.call(this)?(this._promise.splice(t,1),e.callback.call(this)):t++},u.prototype._getStylesheets=function(){var e,n,r,o,s,a,u,l=0,f=[];if(this._options.include){for(n=this._options.include;e=n[l++];)if(r=t.getElementById(e)){if("STYLE"===r.nodeName){u={text:r.textContent},f.push(u);continue}if(r.media&&"print"==r.media)continue;if(!c(r.href))continue;u={href:r.href},r.media&&(u.media=r.media),f.push(u)}}else{for(n=this._options.exclude,o=t.getElementsByTagName("link");r=o[l++];)r.rel&&"stylesheet"==r.rel&&"print"!=r.media&&c(r.href)&&!i(r.id,n)&&(u={href:r.href},r.media&&(u.media=r.media),f.push(u));for(a=t.getElementsByTagName("style"),l=0;s=a[l++];)u={text:s.textContent},f.push(u)}return this._stylesheets=f},u.prototype._downloadStylesheets=function(){for(var e,t=this,n=[],r=0;e=this._stylesheets[r++];)n.push(e.href);h.request(n,function(e){for(var n,r=0;n=e[r];)t._stylesheets[r++].text=n;t._resolve()})},u.prototype._parseStylesheets=function(){this._defer(function(){return this._stylesheets&&this._stylesheets.length&&this._stylesheets[0].text},function(){for(var e,t=0;e=this._stylesheets[t++];)e.rules=d.parse(e.text,e.url)})},u.prototype._filterCSSByKeywords=function(){this._defer(function(){return this._stylesheets&&this._stylesheets.length&&this._stylesheets[0].rules},function(){for(var e,t,n=[],r=0;e=this._stylesheets[r++];)t=e.media,t&&"all"!=t&&"screen"!=t?n.push({rules:e.rules,media:e.media}):n=n.concat(e.rules);this._filteredRules=d.filter(n,this._options.keywords)})},u.prototype._buildMediaQueryMap=function(){this._defer(function(){return this._filteredRules},function(){var e,t,n=0;for(this._mediaQueryMap={};t=this._filteredRules[n++];)t.media&&(e=t.media.join(" and "),this._mediaQueryMap[e]=p.matchMedia(e))})},u.prototype._reportInitialMatches=function(){this._defer(function(){return this._filteredRules&&this._doMatched},function(){this._doMatched(this.getCurrentMatches())})},u.prototype._addMediaListeners=function(){this._defer(function(){return this._filteredRules&&this._doMatched&&this._undoUnmatched},function(){m.addListeners(this,function(e,t){for(var n,r=0,i=[],o=[];n=this._filteredRules[r++];)n.media&&n.media.join(" and ")==e&&(t?i:o).push(n);i.length&&this._doMatched(new s(i)),o.length&&this._undoUnmatched(new s(o))})})},u.modules={DownloadManager:h,StyleManager:d,MediaManager:p,EventManager:m},u.constructors={Ruleset:s,Rule:a},e.Polyfill=u}(window,document),function(e,t,n){"use strict";function r(e){e.each(function(e){var n=t.querySelectorAll(e.getSelectors()),r=e.getDeclaration();[].forEach.call(n,function(e){o(e,r)})})}function i(e){e.each(function(e){var n=t.querySelectorAll(e.getSelectors());[].forEach.call(n,function(e){s(e)})})}function o(e,n){if("undefined"!=typeof n["scroll-snap-coordinate"])return e.snapLengthUnit=p(n),a(e);var r=e.tagName;("body"==r.toLowerCase()||"html"==r.toLowerCase())&&(e=t),e.addEventListener("scroll",q,!1),"undefined"!=typeof n["scroll-snap-destination"]?e.snapLengthUnit=p(n):e.snapLengthUnit=d(n),e.snapElements=[]}function s(e){var n=e.tagName;("body"==n.toLowerCase()||"html"==n.toLowerCase())&&(e=t),e.removeEventListener("scroll",q,!1),e.snapLengthUnit=null,e.snapElements=[]}function a(e){for(var t=e;e&&e!==document;e=e.parentNode)"undefined"!=typeof e.snapElements&&e.snapElements.push(t)}function u(e,t,n){var r={y:m(t,t.snapLengthUnit.y),x:y(t,t.snapLengthUnit.x)},i=e.scrollTop,o=e.scrollLeft,s={y:i/r.y,x:o/r.x},a={y:z.y/r.y,x:z.x/r.x},u={y:0,x:0};u.y=c(n.y,s.y),u.x=c(n.x,s.x),u.y=f(a.y,s.y,u.y,z.y,i),u.x=f(a.x,s.x,u.x,z.x,o);var l={y:u.y*r.y,x:u.x*r.x};return l.y=h(0,v(e),l.y),l.x=h(0,x(e),l.x),l}function l(e,t,n){for(var r=t.snapElements.length,i=e.scrollTop,o=e.scrollLeft,s=Math.min(n.y,n.x),a={y:m(t,t.snapLengthUnit.y),x:y(t,t.snapLengthUnit.x)},u={y:0,x:0},l=A+s;r>l&&l>=0;l+=s)if(k=t.snapElements[l],u={y:k.offsetTop-e.offsetTop+m(k,k.snapLengthUnit.y),x:k.offsetLeft-e.offsetLeft+y(k,k.snapLengthUnit.x)},k.snapCoords=u,o<=u.x&&o+g(e)>=u.x&&i<=u.y&&i+_(e)>=u.y)return A=l,{y:h(0,v(e),u.y-a.y),x:h(0,x(e),u.x-a.x)};return 1==s&&l===r-1?(A=r-1,{y:h(0,v(e),u.y-a.y),x:h(0,x(e),u.x-a.x)}):-1==s&&0===l?(A=0,{y:h(0,v(e),u.y-a.y),x:h(0,x(e),u.x-a.x)}):{y:h(0,v(e),t.snapElements[A].snapCoords.y-a.y),x:h(0,x(e),t.snapElements[A].snapCoords.x-a.x)}}function c(e,t){return-1===e?Math.floor(t):Math.ceil(t)}function f(e,t,n,r,i){return Math.abs(e-t)>=T&&Math.abs(n-t)>L?Math.round(t):Math.abs(r-i)w?Math.round(t):n}function h(e,t,n){return Math.max(Math.min(n,t),e)}function d(e){var t,n=/repeat\((\d+)(px|vh|vw|%)\)/g,r={y:{value:"px",unit:0},x:{value:"px",unit:0}};return"undefined"!==e["scroll-snap-points-y"]&&(t=n.exec(e["scroll-snap-points-y"]),null!==t&&(r.y={value:t[1],unit:t[2]})),"undefined"!==e["scroll-snap-points-x"]&&(t=n.exec(e["scroll-snap-points-x"]),null!==t&&(r.x={value:t[1],unit:t[2]})),r}function p(e){var t,n,r=/(\d+)(px|%) (\d+)(px|%)/g,i={y:{value:"px",unit:0},x:{value:"px",unit:0}};return"undefined"!=typeof e["scroll-snap-coordinate"]?t="scroll-snap-coordinate":"undefined"!=typeof e["scroll-snap-destination"]&&(t="scroll-snap-destination"),null!==t&&(n=r.exec(e[t]),null!==n&&(i.y={value:n[1],unit:n[2]},i.x={value:n[3],unit:n[4]})),i}function m(n,r){return"vh"==r.unit?Math.max(t.documentElement.clientHeight,e.innerHeight||1)/100*r.value:"%"==r.unit?_(n)/100*r.value:_(n)/r.value}function y(n,r){return"vw"==r.unit?Math.max(t.documentElement.clientWidth,e.innerWidth||1)/100*r.value:"%"==r.unit?g(n)/100*r.value:g(n)/r.value}function v(e){return e.scrollHeight}function x(e){return e.scrollWidth}function _(e){return e.offsetHeight}function g(e){return e.offsetWidth}function M(n){return n==t||n==e?t.documentElement.scrollTop>0||t.documentElement.scrollLeft>0?t.documentElement:t.querySelector("body"):n}function E(n,r){var i=Math.abs(n-r),o=100/Math.max(t.documentElement.clientHeight,e.innerHeight||1)*i,s=100/C*o;return isNaN(s)?0:Math.max(C/1.5,Math.min(s,C))}var L=1-.18,w=.95,S=5,T=2,R=45,C=768;if(!("scrollSnapType"in t.documentElement.style||"webkitScrollSnapType"in t.documentElement.style||"msScrollSnapType"in t.documentElement.style)){var b,N,U=null,z=null,q=function(e){b=e.target,N=M(b),j&&(cancelAnimationFrame(j)||clearTimeout(j)),U?clearTimeout(U):z={y:N.scrollTop,x:N.scrollLeft},U=setTimeout($,R)},$=function(){if(z.y!=N.scrollTop||z.x!=N.scrollLeft){var e,t={y:z.y-N.scrollTop>0?-1:1,x:z.x-N.scrollLeft>0?-1:1};e="undefined"!=typeof N.snapElements&&N.snapElements.length>0?l(N,b,t):u(N,b,t),b.removeEventListener("scroll",q,!1),O(N,e,function(){b.addEventListener("scroll",q,!1)}),isNaN(e.x||!isNaN(e.y))||(z=e)}},k=null,A=0,F=function(e){return e*e*e},H=function(e,t,n,r){return n>r?t:e+(t-e)*F(n/r)},j=null,O=function(t,n,r){var i={y:t.scrollTop,x:t.scrollLeft},o=Date.now(),s=e.requestAnimationFrame||e.mozRequestAnimationFrame||e.webkitRequestAnimationFrame||function(t){e.setTimeout(t,15)},a=Math.max(E(i.y,n.y),E(i.x,n.x)),u=function(){var e=Date.now()-o;if(isNaN(n.y)||(t.scrollTop=H(i.y,n.y,e,a)),isNaN(n.x)||(t.scrollLeft=H(i.x,n.x,e,a)),e>a){if("function"==typeof r)return r(n)}else j=s(u)};u()};new Polyfill({declarations:["scroll-snap-type:*","scroll-snap-point-y:*","scroll-snap-coordinate:*","scroll-snap-destination:*"]}).doMatched(r).undoUnmatched(i)}}(window,document); \ No newline at end of file +!function(e,t,n){"use strict";function r(e){return e.replace(/^\s+|\s+$/g,"")}function i(e,t){var n,r=0;if(!e||!t)return!1;for(;n=t[r++];)if(e===n)return!0;return!1}function o(e){return l.test(e)}function s(e){var t,n=0;for(this._rules=[];t=e[n++];)this._rules.push(new a(t))}function a(e){this._rule=e}function u(e){return this instanceof u?(this._options=e,e.keywords||(this._options={keywords:e}),this._promise=[],this._getStylesheets(),this._downloadStylesheets(),this._parseStylesheets(),this._filterCSSByKeywords(),this._buildMediaQueryMap(),this._reportInitialMatches(),void this._addMediaListeners()):new u(e)}var l=RegExp("^"+String({}.valueOf).replace(/[.*+?\^${}()|\[\]\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),c=function(){var e=t.getElementsByTagName("base")[0],n=/^([a-zA-Z:]*\/\/)/;return function(t){var r=!n.test(t)&&!e||t.replace(RegExp.$1,"").split("/")[0]===location.host;return r}}(),f={matchMedia:e.matchMedia&&e.matchMedia("only all").matches,nativeMatchMedia:o(e.matchMedia)},h=function(){function t(e){for(var t,n=0;t=e[n++];)u[t]||i(t,l)||l.push(t)}function n(){if(0===h.readyState||4===h.readyState){var e;(e=l[0])&&r(e),e||s()}}function r(e){f++,h.open("GET",e,!0),h.onreadystatechange=function(){4!=h.readyState||200!=h.status&&304!=h.status||(u[e]=h.responseText,l.shift(),n())},h.send(null)}function o(e){for(var t,n=0,r=0;t=e[n++];)u[t]&&r++;return r===e.length}function s(){for(var e;e=c.shift();)a(e.urls,e.fn)}function a(e,t){for(var n,r=[],i=0;n=e[i++];)r.push(u[n]);t.call(null,r)}var u={},l=[],c=[],f=0,h=function(){var t;try{t=new e.XMLHttpRequest}catch(n){t=new e.ActiveXObject("Microsoft.XMLHTTP")}return t}();return{request:function(e,r){c.push({urls:e,fn:r}),o(e)?s():(t(e),n())},clearCache:function(){u={}},_getRequestCount:function(){return f}}}(),d={_cache:{},clearCache:function(){d._cache={}},parse:function(e,t){function n(){return s(/^\{\s*/)}function i(){return s(/^\}\s*/)}function o(){var t,n=[];for(a(),u(n);"}"!=e.charAt(0)&&(t=w()||S());)n.push(t),u(n);return n}function s(t){var n=t.exec(e);if(n)return e=e.slice(n[0].length),n}function a(){s(/^\s*/)}function u(e){e=e||[];for(var t;t=l();)e.push(t);return e}function l(){if("/"==e[0]&&"*"==e[1]){for(var t=2;"*"!=e[t]||"/"!=e[t+1];)++t;t+=2;var n=e.slice(2,t-2);return e=e.slice(t),a(),{comment:n}}}function c(){var e=s(/^([^{]+)/);if(e)return r(e[0]).split(/\s*,\s*/)}function f(){var e=s(/^(\*?[\-\w]+)\s*/);if(e&&(e=e[0],s(/^:\s*/))){var t=s(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)\s*/);if(t)return t=r(t[0]),s(/^[;\s]*/),{property:e,value:t}}}function h(){for(var e,t=[];e=s(/^(from|to|\d+%|\.\d+%|\d+\.\d+%)\s*/);)t.push(e[1]),s(/^,\s*/);if(t.length)return{values:t,declarations:L()}}function p(){var e=s(/^@([\-\w]+)?keyframes */);if(e){var t=e[1],e=s(/^([\-\w]+)\s*/);if(e){var r=e[1];if(n()){u();for(var o,a=[];o=h();)a.push(o),u();if(i()){var l={name:r,keyframes:a};return t&&(l.vendor=t),l}}}}}function y(){var e=s(/^@supports *([^{]+)/);if(e){var t=r(e[1]);if(n()){u();var a=o();if(i())return{supports:t,rules:a}}}}function m(){var e=s(/^@media *([^{]+)/);if(e){var t=r(e[1]);if(n()){u();var a=o();if(i())return{media:t,rules:a}}}}function v(){var e=s(/^@page */);if(e){var t=c()||[],r=[];if(n()){u();for(var o;o=f()||x();)r.push(o),u();if(i())return{type:"page",selectors:t,declarations:r}}}}function x(){var e=s(/^@([a-z\-]+) */);if(e){var t=e[1];return{type:t,declarations:L()}}}function _(){return E("import")}function g(){return E("charset")}function M(){return E("namespace")}function E(e){var t=s(new RegExp("^@"+e+" *([^;\\n]+);\\s*"));if(t){var n={};return n[e]=r(t[1]),n}}function L(){var e=[];if(n()){u();for(var t;t=f();)e.push(t),u();if(i())return e}}function w(){return p()||m()||y()||_()||g()||M()||v()}function S(){var e=c();if(e)return u(),{selectors:e,declarations:L()}}return t&&d._cache[t]?d._cache[t]:(e=e.replace(/\/\*[\s\S]*?\*\//g,""),d._cache[t]=o())},filter:function(e,t){function n(e,t){if(e||t)return e?e.concat(t):[t]}function i(e){null==e.media&&delete e.media,null==e.supports&&delete e.supports,c.push(e)}function o(e,t){if(t)for(var n=t.length;n--;)if(e.indexOf(t[n])>=0)return!0}function s(e,t){for(var n,i,o,s,a=/\*/,u=0;n=t[u++];)if(i=n.split(":"),o=new RegExp("^"+r(i[0]).replace(a,".*")+"$"),s=new RegExp("^"+r(i[1]).replace(a,".*")+"$"),o.test(e.property)&&s.test(e.value))return!0}function a(e,n,r){if(t.selectors)return o(e.selectors.join(","),t.selectors)?(i({media:n,supports:r,selectors:e.selectors,declarations:e.declarations}),!0):void 0}function u(e,n,r){if(t.declarations)for(var o,a=0;o=e.declarations[a++];)if(s(o,t.declarations))return i({media:n,supports:r,selectors:e.selectors,declarations:e.declarations}),!0}function l(e,t,r){for(var i,o=0;i=e[o++];)i.declarations?a(i,t,r)||u(i,t,r):i.rules&&i.media?l(i.rules,n(t,i.media),r):i.rules&&i.supports&&l(i.rules,t,n(r,i.supports))}var c=[];return l(e),c}},p=function(){function n(){if(o)return o;var e=t.documentElement,n=t.body,r=e.style.fontSize,i=n.style.fontSize,s=t.createElement("div");return e.style.fontSize="1em",n.style.fontSize="1em",n.appendChild(s),s.style.width="1em",s.style.position="absolute",o=s.offsetWidth,n.removeChild(s),n.style.fontSize=i,e.style.fontSize=r,o}function r(t){return e.matchMedia(t)}function i(e){var r,i,o=!1;return s=t.documentElement.clientWidth,a.test(e)&&(r="em"===RegExp.$2?parseFloat(RegExp.$1)*n():parseFloat(RegExp.$1)),u.test(e)&&(i="em"===RegExp.$2?parseFloat(RegExp.$1)*n():parseFloat(RegExp.$1)),r&&i?o=r<=s&&i>=s:(r&&r<=s&&(o=!0),i&&i>=s&&(o=!0)),{matches:o,media:e}}var o,s,a=/\(min\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/,u=/\(max\-width:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/,l={};return{matchMedia:function(e){return f.matchMedia?r(e):i(e)},clearCache:function(){f.nativeMatchMedia||(s=null,l={})}}}(),y=function(){function t(e,t){var n;return function(){clearTimeout(n),n=setTimeout(e,t)}}var n=function(){var e=[];return{add:function(t,n,r){for(var i,o=0;i=e[o++];)if(i.polyfill==t&&i.mql===n&&i.fn===r)return!1;n.addListener(r),e.push({polyfill:t,mql:n,fn:r})},remove:function(t){for(var n,r=0;n=e[r++];)n.polyfill===t&&(n.mql.removeListener(n.fn),e.splice(--r,1))}}}(),r=function(t){function n(){for(var e,n=0;e=t[n++];)e.fn()}return{add:function(r,i){t.length||(e.addEventListener?e.addEventListener("resize",n,!1):e.attachEvent("onresize",n)),t.push({polyfill:r,fn:i})},remove:function(r){for(var i,o=0;i=t[o++];)i.polyfill===r&&t.splice(--o,1);t.length||(e.removeEventListener?e.removeEventListener("resize",n,!1):e.detachEvent&&e.detachEvent("onresize",n))}}}([]);return{removeListeners:function(e){f.nativeMatchMedia?n.remove(e):r.remove(e)},addListeners:function(e,i){function o(){if(f.nativeMatchMedia)for(var o in a)a.hasOwnProperty(o)&&!function(t,r){n.add(e,t,function(){i.call(e,r,t.matches)})}(a[o],o);else{var u=t(function(e,t){return function(){s(e,t)}}(e,a),e._options.debounceTimeout||100);r.add(e,u)}}function s(e,t){var n,r={};p.clearCache();for(n in t)t.hasOwnProperty(n)&&(r[n]=p.matchMedia(n).matches,r[n]!=u[n]&&i.call(e,n,p.matchMedia(n).matches));u=r}var a=e._mediaQueryMap,u={};!function(){for(var e in a)a.hasOwnProperty(e)&&(u[e]=p.matchMedia(e).matches)}(),o()}}}();s.prototype.each=function(e,t){var n,r=0;for(t||(t=this);n=this._rules[r++];)e.call(t,n)},s.prototype.size=function(){return this._rules.length},s.prototype.at=function(e){return this._rules[e]},a.prototype.getDeclaration=function(){for(var e,t={},n=0,r=this._rule.declarations;e=r[n++];)t[e.property]=e.value;return t},a.prototype.getSelectors=function(){return this._rule.selectors.join(", ")},a.prototype.getMedia=function(){return this._rule.media.join(" and ")},u.prototype.doMatched=function(e){return this._doMatched=e,this._resolve(),this},u.prototype.undoUnmatched=function(e){return this._undoUnmatched=e,this._resolve(),this},u.prototype.getCurrentMatches=function(){for(var e,t,n=0,r=[];e=this._filteredRules[n++];)t=e.media&&e.media.join(" and "),t&&!p.matchMedia(t).matches||r.push(e);return new s(r)},u.prototype.destroy=function(){this._undoUnmatched&&(this._undoUnmatched(this.getCurrentMatches()),y.removeListeners(this))},u.prototype._defer=function(e,t){e.call(this)?t.call(this):this._promise.push({condition:e,callback:t})},u.prototype._resolve=function(){for(var e,t=0;e=this._promise[t];)e.condition.call(this)?(this._promise.splice(t,1),e.callback.call(this)):t++},u.prototype._getStylesheets=function(){var e,n,r,o,s,a,u,l=0,f=[];if(this._options.include){for(n=this._options.include;e=n[l++];)if(r=t.getElementById(e)){if("STYLE"===r.nodeName){u={text:r.textContent},f.push(u);continue}if(r.media&&"print"==r.media)continue;if(!c(r.href))continue;u={href:r.href},r.media&&(u.media=r.media),f.push(u)}}else{for(n=this._options.exclude,o=t.getElementsByTagName("link");r=o[l++];)r.rel&&"stylesheet"==r.rel&&"print"!=r.media&&c(r.href)&&!i(r.id,n)&&(u={href:r.href},r.media&&(u.media=r.media),f.push(u));for(a=t.getElementsByTagName("style"),l=0;s=a[l++];)u={text:s.textContent},f.push(u)}return this._stylesheets=f},u.prototype._downloadStylesheets=function(){for(var e,t=this,n=[],r=0;e=this._stylesheets[r++];)n.push(e.href);h.request(n,function(e){for(var n,r=0;n=e[r];)t._stylesheets[r++].text=n;t._resolve()})},u.prototype._parseStylesheets=function(){this._defer(function(){return this._stylesheets&&this._stylesheets.length&&this._stylesheets[0].text},function(){for(var e,t=0;e=this._stylesheets[t++];)e.rules=d.parse(e.text,e.url)})},u.prototype._filterCSSByKeywords=function(){this._defer(function(){return this._stylesheets&&this._stylesheets.length&&this._stylesheets[0].rules},function(){for(var e,t,n=[],r=0;e=this._stylesheets[r++];)t=e.media,t&&"all"!=t&&"screen"!=t?n.push({rules:e.rules,media:e.media}):n=n.concat(e.rules);this._filteredRules=d.filter(n,this._options.keywords)})},u.prototype._buildMediaQueryMap=function(){this._defer(function(){return this._filteredRules},function(){var e,t,n=0;for(this._mediaQueryMap={};t=this._filteredRules[n++];)t.media&&(e=t.media.join(" and "),this._mediaQueryMap[e]=p.matchMedia(e))})},u.prototype._reportInitialMatches=function(){this._defer(function(){return this._filteredRules&&this._doMatched},function(){this._doMatched(this.getCurrentMatches())})},u.prototype._addMediaListeners=function(){this._defer(function(){return this._filteredRules&&this._doMatched&&this._undoUnmatched},function(){y.addListeners(this,function(e,t){for(var n,r=0,i=[],o=[];n=this._filteredRules[r++];)n.media&&n.media.join(" and ")==e&&(t?i:o).push(n);i.length&&this._doMatched(new s(i)),o.length&&this._undoUnmatched(new s(o))})})},u.modules={DownloadManager:h,StyleManager:d,MediaManager:p,EventManager:y},u.constructors={Ruleset:s,Rule:a},e.Polyfill=u}(window,document),function(e,t,n){"use strict";function r(e,n){return(n||t).querySelectorAll(e)}function i(e){var n=new MutationObserver(function(t){t.forEach(function(t){t.addedNodes.length&&e.each(u),[].forEach.call(t.removedNodes,l)})});n.observe(t.body,{subtree:!0,childList:!0}),e.each(u)}function o(e){e.each(function(e){var n=t.querySelectorAll(e.getSelectors());[].forEach.call(n,function(e){l(e)})})}function s(e,n){if("undefined"!=typeof n["scroll-snap-coordinate"])return e.snapLengthUnit=v(n),c(e);var r=e.tagName;"body"!=r.toLowerCase()&&"html"!=r.toLowerCase()||(e=t),e.addEventListener("scroll",A,!1),"undefined"!=typeof n["scroll-snap-destination"]?e.snapLengthUnit=v(n):e.snapLengthUnit=m(n),e.snapElements=[]}function a(e){return function(t){s(t,e)}}function u(e){var n=r(e.getSelectors(),t),i=e.getDeclaration();[].forEach.call(n,a(i))}function l(e){var n=e.tagName;"body"!=n.toLowerCase()&&"html"!=n.toLowerCase()||(e=t),e.removeEventListener("scroll",A,!1),e.snapLengthUnit=null,e.snapElements=[]}function c(e){for(var t=e;e&&e!==document;e=e.parentNode)"undefined"!=typeof e.snapElements&&e.snapElements.push(t)}function f(e,t,n){var r={y:x(t,t.snapLengthUnit.y),x:_(t,t.snapLengthUnit.x)},i=e.scrollTop,o=e.scrollLeft,s={y:i/r.y,x:o/r.x},a={y:k.y/r.y,x:k.x/r.x},u={y:0,x:0};u.y=d(n.y,s.y),u.x=d(n.x,s.x),u.y=p(a.y,s.y,u.y,k.y,i),u.x=p(a.x,s.x,u.x,k.x,o);var l={y:u.y*r.y,x:u.x*r.x};return l.y=y(0,g(e),l.y),l.x=y(0,M(e),l.x),l}function h(e,t,n){for(var r=t.snapElements.length,i=e.scrollTop,o=e.scrollLeft,s=Math.min(n.y,n.x),a={y:x(t,t.snapLengthUnit.y),x:_(t,t.snapLengthUnit.x)},u={y:0,x:0},l=O+s;l=0;l+=s)if(H=t.snapElements[l],u={y:H.offsetTop-e.offsetTop+x(H,H.snapLengthUnit.y),x:H.offsetLeft-e.offsetLeft+_(H,H.snapLengthUnit.x)},H.snapCoords=u,o<=u.x&&o+L(e)>=u.x&&i<=u.y&&i+E(e)>=u.y)return O=l,{y:y(0,g(e),u.y-a.y),x:y(0,M(e),u.x-a.x)};return 1==s&&l===r-1?(O=r-1,{y:y(0,g(e),u.y-a.y),x:y(0,M(e),u.x-a.x)}):s==-1&&0===l?(O=0,{y:y(0,g(e),u.y-a.y),x:y(0,M(e),u.x-a.x)}):{y:y(0,g(e),t.snapElements[O].snapCoords.y-a.y),x:y(0,M(e),t.snapElements[O].snapCoords.x-a.x)}}function d(e,t){return e===-1?Math.floor(t):Math.ceil(t)}function p(e,t,n,r,i){return Math.abs(e-t)>=C&&Math.abs(n-t)>T?Math.round(t):Math.abs(r-i)b?Math.round(t):n}function y(e,t,n){return Math.max(Math.min(n,t),e)}function m(e){var t,n=/repeat\((\d+)(px|vh|vw|%)\)/g,r={y:{value:0,unit:"px"},x:{value:0,unit:"px"}};return"undefined"!=typeof e["scroll-snap-points-y"]&&(t=n.exec(e["scroll-snap-points-y"]),null!==t&&(r.y={value:t[1],unit:t[2]})),"undefined"!=typeof e["scroll-snap-points-x"]&&(t=n.exec(e["scroll-snap-points-x"]),null!==t&&(r.x={value:t[1],unit:t[2]})),r}function v(e){var t,n,r=/(\d+)(px|%) (\d+)(px|%)/g,i={y:{value:0,unit:"px"},x:{value:0,unit:"px"}};return"undefined"!=typeof e["scroll-snap-coordinate"]?t="scroll-snap-coordinate":"undefined"!=typeof e["scroll-snap-destination"]&&(t="scroll-snap-destination"),null!==t&&(n=r.exec(e[t]),null!==n&&(i.y={value:n[1],unit:n[2]},i.x={value:n[3],unit:n[4]})),i}function x(n,r){return"vh"==r.unit?Math.max(t.documentElement.clientHeight,e.innerHeight||1)/100*r.value:"%"==r.unit?E(n)/100*r.value:E(n)/r.value}function _(n,r){return"vw"==r.unit?Math.max(t.documentElement.clientWidth,e.innerWidth||1)/100*r.value:"%"==r.unit?L(n)/100*r.value:L(n)/r.value}function g(e){return e.scrollHeight}function M(e){return e.scrollWidth}function E(e){return e.offsetHeight}function L(e){return e.offsetWidth}function w(n){return n==t||n==e?t.documentElement.scrollTop>0||t.documentElement.scrollLeft>0?t.documentElement:t.querySelector("body"):n}function S(n,r){var i=Math.abs(n-r),o=100/Math.max(t.documentElement.clientHeight,e.innerHeight||1)*i,s=100/U*o;return isNaN(s)?0:Math.max(U/1.5,Math.min(s,U))}var T=1-.18,b=.95,R=5,C=2,N=45,U=768;if(!("scrollSnapType"in t.documentElement.style||"webkitScrollSnapType"in t.documentElement.style||"msScrollSnapType"in t.documentElement.style)){var z,q,$=null,k=null,A=function(e){z=e.target,q=w(z),P&&(cancelAnimationFrame(P)||clearTimeout(P)),$?clearTimeout($):k={y:q.scrollTop,x:q.scrollLeft},$=setTimeout(F,N)},F=function(){if(k.y!=q.scrollTop||k.x!=q.scrollLeft){var e,t={y:k.y-q.scrollTop>0?-1:1,x:k.x-q.scrollLeft>0?-1:1};e="undefined"!=typeof q.snapElements&&q.snapElements.length>0?h(q,z,t):f(q,z,t),z.removeEventListener("scroll",A,!1),W(q,e,function(){z.addEventListener("scroll",A,!1)}),isNaN(e.x||!isNaN(e.y))||(k=e)}},H=null,O=0,j=function(e){return e*e*e},B=function(e,t,n,r){return n>r?t:e+(t-e)*j(n/r)},P=null,W=function(t,n,r){var i={y:t.scrollTop,x:t.scrollLeft},o=Date.now(),s=e.requestAnimationFrame||e.mozRequestAnimationFrame||e.webkitRequestAnimationFrame||function(t){e.setTimeout(t,15)},a=Math.max(S(i.y,n.y),S(i.x,n.x)),u=function(){var e=Date.now()-o;if(isNaN(n.y)||(t.scrollTop=B(i.y,n.y,e,a)),isNaN(n.x)||(t.scrollLeft=B(i.x,n.x,e,a)),e>a){if("function"==typeof r)return r(n)}else P=s(u)};u()};new Polyfill({declarations:["scroll-snap-type:*","scroll-snap-point-y:*","scroll-snap-coordinate:*","scroll-snap-destination:*"]}).doMatched(i).undoUnmatched(o)}}(window,document); \ No newline at end of file diff --git a/dist/scrollsnap-polyfill.js b/dist/scrollsnap-polyfill.js index 36ba632..7f3246c 100644 --- a/dist/scrollsnap-polyfill.js +++ b/dist/scrollsnap-polyfill.js @@ -1 +1 @@ -!function(n,e,t){"use strict";function o(n){n.each(function(n){var t=e.querySelectorAll(n.getSelectors()),o=n.getDeclaration();[].forEach.call(t,function(n){a(n,o)})})}function l(n){n.each(function(n){var t=e.querySelectorAll(n.getSelectors());[].forEach.call(t,function(n){r(n)})})}function a(n,t){if("undefined"!=typeof t["scroll-snap-coordinate"])return n.snapLengthUnit=x(t),i(n);var o=n.tagName;("body"==o.toLowerCase()||"html"==o.toLowerCase())&&(n=e),n.addEventListener("scroll",F,!1),"undefined"!=typeof t["scroll-snap-destination"]?n.snapLengthUnit=x(t):n.snapLengthUnit=p(t),n.snapElements=[]}function r(n){var t=n.tagName;("body"==t.toLowerCase()||"html"==t.toLowerCase())&&(n=e),n.removeEventListener("scroll",F,!1),n.snapLengthUnit=null,n.snapElements=[]}function i(n){for(var e=n;n&&n!==document;n=n.parentNode)"undefined"!=typeof n.snapElements&&n.snapElements.push(e)}function s(n,e,t){var o={y:d(e,e.snapLengthUnit.y),x:m(e,e.snapLengthUnit.x)},l=n.scrollTop,a=n.scrollLeft,r={y:l/o.y,x:a/o.x},i={y:H.y/o.y,x:H.x/o.x},s={y:0,x:0};s.y=c(t.y,r.y),s.x=c(t.x,r.x),s.y=f(i.y,r.y,s.y,H.y,l),s.x=f(i.x,r.x,s.x,H.x,a);var u={y:s.y*o.y,x:s.x*o.x};return u.y=y(0,h(n),u.y),u.x=y(0,v(n),u.x),u}function u(n,e,t){for(var o=e.snapElements.length,l=n.scrollTop,a=n.scrollLeft,r=Math.min(t.y,t.x),i={y:d(e,e.snapLengthUnit.y),x:m(e,e.snapLengthUnit.x)},s={y:0,x:0},u=k+r;o>u&&u>=0;u+=r)if(D=e.snapElements[u],s={y:D.offsetTop-n.offsetTop+d(D,D.snapLengthUnit.y),x:D.offsetLeft-n.offsetLeft+m(D,D.snapLengthUnit.x)},D.snapCoords=s,a<=s.x&&a+g(n)>=s.x&&l<=s.y&&l+L(n)>=s.y)return k=u,{y:y(0,h(n),s.y-i.y),x:y(0,v(n),s.x-i.x)};return 1==r&&u===o-1?(k=o-1,{y:y(0,h(n),s.y-i.y),x:y(0,v(n),s.x-i.x)}):-1==r&&0===u?(k=0,{y:y(0,h(n),s.y-i.y),x:y(0,v(n),s.x-i.x)}):{y:y(0,h(n),e.snapElements[k].snapCoords.y-i.y),x:y(0,v(n),e.snapElements[k].snapCoords.x-i.x)}}function c(n,e){return-1===n?Math.floor(e):Math.ceil(e)}function f(n,e,t,o,l){return Math.abs(n-e)>=b&&Math.abs(t-e)>T?Math.round(e):Math.abs(o-l)w?Math.round(e):t}function y(n,e,t){return Math.max(Math.min(t,e),n)}function p(n){var e,t=/repeat\((\d+)(px|vh|vw|%)\)/g,o={y:{value:"px",unit:0},x:{value:"px",unit:0}};return"undefined"!==n["scroll-snap-points-y"]&&(e=t.exec(n["scroll-snap-points-y"]),null!==e&&(o.y={value:e[1],unit:e[2]})),"undefined"!==n["scroll-snap-points-x"]&&(e=t.exec(n["scroll-snap-points-x"]),null!==e&&(o.x={value:e[1],unit:e[2]})),o}function x(n){var e,t,o=/(\d+)(px|%) (\d+)(px|%)/g,l={y:{value:"px",unit:0},x:{value:"px",unit:0}};return"undefined"!=typeof n["scroll-snap-coordinate"]?e="scroll-snap-coordinate":"undefined"!=typeof n["scroll-snap-destination"]&&(e="scroll-snap-destination"),null!==e&&(t=o.exec(n[e]),null!==t&&(l.y={value:t[1],unit:t[2]},l.x={value:t[3],unit:t[4]})),l}function d(t,o){return"vh"==o.unit?Math.max(e.documentElement.clientHeight,n.innerHeight||1)/100*o.value:"%"==o.unit?L(t)/100*o.value:L(t)/o.value}function m(t,o){return"vw"==o.unit?Math.max(e.documentElement.clientWidth,n.innerWidth||1)/100*o.value:"%"==o.unit?g(t)/100*o.value:g(t)/o.value}function h(n){return n.scrollHeight}function v(n){return n.scrollWidth}function L(n){return n.offsetHeight}function g(n){return n.offsetWidth}function E(t){return t==e||t==n?e.documentElement.scrollTop>0||e.documentElement.scrollLeft>0?e.documentElement:e.querySelector("body"):t}function M(t,o){var l=Math.abs(t-o),a=100/Math.max(e.documentElement.clientHeight,n.innerHeight||1)*l,r=100/S*a;return isNaN(r)?0:Math.max(S/1.5,Math.min(r,S))}var T=1-.18,w=.95,N=5,b=2,U=45,S=768;if(!("scrollSnapType"in e.documentElement.style||"webkitScrollSnapType"in e.documentElement.style||"msScrollSnapType"in e.documentElement.style)){var C,q,A=null,H=null,F=function(n){C=n.target,q=E(C),P&&(cancelAnimationFrame(P)||clearTimeout(P)),A?clearTimeout(A):H={y:q.scrollTop,x:q.scrollLeft},A=setTimeout(W,U)},W=function(){if(H.y!=q.scrollTop||H.x!=q.scrollLeft){var n,e={y:H.y-q.scrollTop>0?-1:1,x:H.x-q.scrollLeft>0?-1:1};n="undefined"!=typeof q.snapElements&&q.snapElements.length>0?u(q,C,e):s(q,C,e),C.removeEventListener("scroll",F,!1),j(q,n,function(){C.addEventListener("scroll",F,!1)}),isNaN(n.x||!isNaN(n.y))||(H=n)}},D=null,k=0,R=function(n){return n*n*n},z=function(n,e,t,o){return t>o?e:n+(e-n)*R(t/o)},P=null,j=function(e,t,o){var l={y:e.scrollTop,x:e.scrollLeft},a=Date.now(),r=n.requestAnimationFrame||n.mozRequestAnimationFrame||n.webkitRequestAnimationFrame||function(e){n.setTimeout(e,15)},i=Math.max(M(l.y,t.y),M(l.x,t.x)),s=function(){var n=Date.now()-a;if(isNaN(t.y)||(e.scrollTop=z(l.y,t.y,n,i)),isNaN(t.x)||(e.scrollLeft=z(l.x,t.x,n,i)),n>i){if("function"==typeof o)return o(t)}else P=r(s)};s()};new Polyfill({declarations:["scroll-snap-type:*","scroll-snap-point-y:*","scroll-snap-coordinate:*","scroll-snap-destination:*"]}).doMatched(o).undoUnmatched(l)}}(window,document); \ No newline at end of file +!function(n,e,t){"use strict";function o(n,t){return(t||e).querySelectorAll(n)}function l(n){var t=new MutationObserver(function(e){e.forEach(function(e){e.addedNodes.length&&n.each(s),[].forEach.call(e.removedNodes,u)})});t.observe(e.body,{subtree:!0,childList:!0}),n.each(s)}function a(n){n.each(function(n){var t=e.querySelectorAll(n.getSelectors());[].forEach.call(t,function(n){u(n)})})}function r(n,t){if("undefined"!=typeof t["scroll-snap-coordinate"])return n.snapLengthUnit=h(t),c(n);var o=n.tagName;"body"!=o.toLowerCase()&&"html"!=o.toLowerCase()||(n=e),n.addEventListener("scroll",k,!1),"undefined"!=typeof t["scroll-snap-destination"]?n.snapLengthUnit=h(t):n.snapLengthUnit=m(t),n.snapElements=[]}function i(n){return function(e){r(e,n)}}function s(n){var t=o(n.getSelectors(),e),l=n.getDeclaration();[].forEach.call(t,i(l))}function u(n){var t=n.tagName;"body"!=t.toLowerCase()&&"html"!=t.toLowerCase()||(n=e),n.removeEventListener("scroll",k,!1),n.snapLengthUnit=null,n.snapElements=[]}function c(n){for(var e=n;n&&n!==document;n=n.parentNode)"undefined"!=typeof n.snapElements&&n.snapElements.push(e)}function f(n,e,t){var o={y:v(e,e.snapLengthUnit.y),x:L(e,e.snapLengthUnit.x)},l=n.scrollTop,a=n.scrollLeft,r={y:l/o.y,x:a/o.x},i={y:D.y/o.y,x:D.x/o.x},s={y:0,x:0};s.y=p(t.y,r.y),s.x=p(t.x,r.x),s.y=x(i.y,r.y,s.y,D.y,l),s.x=x(i.x,r.x,s.x,D.x,a);var u={y:s.y*o.y,x:s.x*o.x};return u.y=d(0,g(n),u.y),u.x=d(0,E(n),u.x),u}function y(n,e,t){for(var o=e.snapElements.length,l=n.scrollTop,a=n.scrollLeft,r=Math.min(t.y,t.x),i={y:v(e,e.snapLengthUnit.y),x:L(e,e.snapLengthUnit.x)},s={y:0,x:0},u=O+r;u=0;u+=r)if(z=e.snapElements[u],s={y:z.offsetTop-n.offsetTop+v(z,z.snapLengthUnit.y),x:z.offsetLeft-n.offsetLeft+L(z,z.snapLengthUnit.x)},z.snapCoords=s,a<=s.x&&a+T(n)>=s.x&&l<=s.y&&l+M(n)>=s.y)return O=u,{y:d(0,g(n),s.y-i.y),x:d(0,E(n),s.x-i.x)};return 1==r&&u===o-1?(O=o-1,{y:d(0,g(n),s.y-i.y),x:d(0,E(n),s.x-i.x)}):r==-1&&0===u?(O=0,{y:d(0,g(n),s.y-i.y),x:d(0,E(n),s.x-i.x)}):{y:d(0,g(n),e.snapElements[O].snapCoords.y-i.y),x:d(0,E(n),e.snapElements[O].snapCoords.x-i.x)}}function p(n,e){return n===-1?Math.floor(e):Math.ceil(e)}function x(n,e,t,o,l){return Math.abs(n-e)>=C&&Math.abs(t-e)>w?Math.round(e):Math.abs(o-l)U?Math.round(e):t}function d(n,e,t){return Math.max(Math.min(t,e),n)}function m(n){var e,t=/repeat\((\d+)(px|vh|vw|%)\)/g,o={y:{value:0,unit:"px"},x:{value:0,unit:"px"}};return"undefined"!=typeof n["scroll-snap-points-y"]&&(e=t.exec(n["scroll-snap-points-y"]),null!==e&&(o.y={value:e[1],unit:e[2]})),"undefined"!=typeof n["scroll-snap-points-x"]&&(e=t.exec(n["scroll-snap-points-x"]),null!==e&&(o.x={value:e[1],unit:e[2]})),o}function h(n){var e,t,o=/(\d+)(px|%) (\d+)(px|%)/g,l={y:{value:0,unit:"px"},x:{value:0,unit:"px"}};return"undefined"!=typeof n["scroll-snap-coordinate"]?e="scroll-snap-coordinate":"undefined"!=typeof n["scroll-snap-destination"]&&(e="scroll-snap-destination"),null!==e&&(t=o.exec(n[e]),null!==t&&(l.y={value:t[1],unit:t[2]},l.x={value:t[3],unit:t[4]})),l}function v(t,o){return"vh"==o.unit?Math.max(e.documentElement.clientHeight,n.innerHeight||1)/100*o.value:"%"==o.unit?M(t)/100*o.value:M(t)/o.value}function L(t,o){return"vw"==o.unit?Math.max(e.documentElement.clientWidth,n.innerWidth||1)/100*o.value:"%"==o.unit?T(t)/100*o.value:T(t)/o.value}function g(n){return n.scrollHeight}function E(n){return n.scrollWidth}function M(n){return n.offsetHeight}function T(n){return n.offsetWidth}function b(t){return t==e||t==n?e.documentElement.scrollTop>0||e.documentElement.scrollLeft>0?e.documentElement:e.querySelector("body"):t}function N(t,o){var l=Math.abs(t-o),a=100/Math.max(e.documentElement.clientHeight,n.innerHeight||1)*l,r=100/A*a;return isNaN(r)?0:Math.max(A/1.5,Math.min(r,A))}var w=1-.18,U=.95,S=5,C=2,q=45,A=768;if(!("scrollSnapType"in e.documentElement.style||"webkitScrollSnapType"in e.documentElement.style||"msScrollSnapType"in e.documentElement.style)){var H,F,W=null,D=null,k=function(n){H=n.target,F=b(H),B&&(cancelAnimationFrame(B)||clearTimeout(B)),W?clearTimeout(W):D={y:F.scrollTop,x:F.scrollLeft},W=setTimeout(R,q)},R=function(){if(D.y!=F.scrollTop||D.x!=F.scrollLeft){var n,e={y:D.y-F.scrollTop>0?-1:1,x:D.x-F.scrollLeft>0?-1:1};n="undefined"!=typeof F.snapElements&&F.snapElements.length>0?y(F,H,e):f(F,H,e),H.removeEventListener("scroll",k,!1),G(F,n,function(){H.addEventListener("scroll",k,!1)}),isNaN(n.x||!isNaN(n.y))||(D=n)}},z=null,O=0,P=function(n){return n*n*n},j=function(n,e,t,o){return t>o?e:n+(e-n)*P(t/o)},B=null,G=function(e,t,o){var l={y:e.scrollTop,x:e.scrollLeft},a=Date.now(),r=n.requestAnimationFrame||n.mozRequestAnimationFrame||n.webkitRequestAnimationFrame||function(e){n.setTimeout(e,15)},i=Math.max(N(l.y,t.y),N(l.x,t.x)),s=function(){var n=Date.now()-a;if(isNaN(t.y)||(e.scrollTop=j(l.y,t.y,n,i)),isNaN(t.x)||(e.scrollLeft=j(l.x,t.x,n,i)),n>i){if("function"==typeof o)return o(t)}else B=r(s)};s()};new Polyfill({declarations:["scroll-snap-type:*","scroll-snap-point-y:*","scroll-snap-coordinate:*","scroll-snap-destination:*"]}).doMatched(l).undoUnmatched(a)}}(window,document); \ No newline at end of file diff --git a/package.json b/package.json index fa3731d..c6a5cbd 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "description": "Polyfill for css scroll snap behaviour", "main": "dist/scrollsnap-polyfill.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "build": "gulp build" }, "repository": { "type": "git", diff --git a/src/scrollsnap-polyfill.js b/src/scrollsnap-polyfill.js index d46f4ad..3d034a3 100644 --- a/src/scrollsnap-polyfill.js +++ b/src/scrollsnap-polyfill.js @@ -58,23 +58,37 @@ return; } + + /** + * qsa is a helper function for quickly working with selecting from the DOM. + * @param {string} sel css selector to match elements against + * @param {context} context DOMNode who's subtree will be searched. Falls back to document if not defined. + */ + function qsa(sel, context) { + return (context || doc).querySelectorAll(sel) + } + /** * doMatched is a callback for Polyfill to fill in the desired behaviour. * @param {array} rules rules found for the polyfill */ function doMatched(rules) { - // iterate over rules - rules.each(function(rule) { - var elements = doc.querySelectorAll(rule.getSelectors()), - declaration = rule.getDeclaration(); + var observer = new MutationObserver(function (mutations) { + mutations.forEach(function (mutation) { + if (mutation.addedNodes.length) { + rules.each(setUpMatchingElements); + } + [].forEach.call(mutation.removedNodes, tearDownElement) + }) + }); - // iterate over elements - [].forEach.call(elements, function(obj) { - // set up the behaviour - setUpElement(obj, declaration); - }); + observer.observe(doc.body, { + subtree: true, + childList: true }); + + rules.each(setUpMatchingElements); } /** @@ -129,6 +143,31 @@ obj.snapElements = []; } + /** + * higher order function that makes setting up elements in a consistent manner easier + * @param {Object} declaration CSS declarations + * @return {function} + */ + function setUpElementFromDeclaration(declaration) { + + /** + * sets up an element for scroll-snap behavior based on earlier declaration + * @param {Object} obj HTML element + */ + return function (element) { + setUpElement(element, declaration) + } + } + + + function setUpMatchingElements(rule) { + var elements = qsa(rule.getSelectors(), doc), + declaration = rule.getDeclaration(); + + // iterate over elements + [].forEach.call(elements, setUpElementFromDeclaration(declaration)); + } + /** * tear down an element. remove all added behaviour. * @param {Object} obj DomElement @@ -412,7 +451,7 @@ result; // parse y value and unit - if (declaration['scroll-snap-points-y'] !== 'undefined') { + if (typeof declaration['scroll-snap-points-y'] !== 'undefined') { result = regex.exec(declaration['scroll-snap-points-y']); // if regexp fails, value is null if (result !== null) { @@ -421,7 +460,7 @@ } // parse x value and unit - if (declaration['scroll-snap-points-x'] !== 'undefined') { + if (typeof declaration['scroll-snap-points-x'] !== 'undefined') { result = regex.exec(declaration['scroll-snap-points-x']); // if regexp fails, value is null if (result !== null) {