Files
2023-03-10 14:47:04 +08:00

1 line
15 KiB
JavaScript

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.StuckJs=e():t.StuckJs=e()}(this,function(){return function(t){var e={};function i(n){if(e[n])return e[n].exports;var r=e[n]={i:n,l:!1,exports:{}};return t[n].call(r.exports,r,r.exports,i),r.l=!0,r.exports}return i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)i.d(n,r,function(e){return t[e]}.bind(null,r));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=4)}([function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.noop=function(){},e.stableSort=function(t,e){return t.map(function(t,e){return{item:t,index:e}}).sort(function(t,i){var n=e(t.item,i.item);return 0!==n?n:t.index-i.index}).map(function(t){return t.item})}},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=i(0),r=function(){function t(e,i,r){var s=this;void 0===i&&(i=!0),void 0===r&&(r=n.noop),this.$$shouldPlacehold=!0,this.original=e,this.onUpdate="function"==typeof r?r:n.noop,this.initialComputedStyles=window.getComputedStyle(this.original),this.initiallyHidden="none"===this.initialComputedStyles.display,this.initiallyHidden&&this.execWhileStucking(function(){s.initialComputedStyles=window.getComputedStyle(s.original)}),this.element=t.createPlaceholderElement(),this.applyInitialStyles(),t.wrap(this.original,this.element),this.cachedRect=this.updateRect(),i&&(this.observer=t.createObserver(this.original,function(){return s.update()}))}return Object.defineProperty(t.prototype,"shouldPlacehold",{get:function(){return!this.initiallyHidden&&this.$$shouldPlacehold},set:function(t){this.shouldPlacehold!==t&&(this.$$shouldPlacehold=t,this.update(!0))},enumerable:!0,configurable:!0}),t.prototype.update=function(t){void 0===t&&(t=!1),this.shouldPlacehold?this.applyStyles(t):this.removeStyles(),this.onUpdate()},t.prototype.updateRect=function(){var t=this;return this.cachedRect=this.element.getBoundingClientRect(),this.initiallyHidden&&this.execWhileStucking(function(){t.cachedRect=t.element.getBoundingClientRect()}),this.cachedRect},t.prototype.destroy=function(){this.observer&&(this.observer.disconnect(),delete this.observer),t.unwrap(this.original),delete this.element,delete this.original,delete this.cachedRect,delete this.onUpdate},t.prototype.execWhileStucking=function(t){var e=this.original.dataset.stuck;this.original.dataset.stuck="true",t(),this.original.dataset.stuck=e},t.prototype.applyInitialStyles=function(){this.initialComputedStyles&&!this.initiallyHidden&&(this.element.style.margin=this.initialComputedStyles.margin,this.element.style.minWidth=this.initialComputedStyles.minWidth,this.element.style.minHeight=this.initialComputedStyles.minHeight,this.element.style.width=this.initialComputedStyles.width,this.element.style.height=this.initialComputedStyles.height)},t.prototype.applyStyles=function(t){if(void 0===t&&(t=!1),this.original&&this.element){var e=this.original.getBoundingClientRect(),i=e.width,n=e.height,r=i!==this.cachedRect.width,s=n!==this.cachedRect.height;(t||r||s)&&((t||r)&&(this.element.style.width=i+"px"),(t||s)&&(this.element.style.height=n+"px"),this.updateRect())}},t.prototype.removeStyles=function(){this.original&&this.element&&(this.element.style.width="",this.element.style.height="")},t.createObserver=function(t,e){if(!t)throw new TypeError("[Stuck.js] Could not create mutation observer on targetNode "+String(t)+". This should be HTMLElement");var i=function(t){var e=t.type;return"childList"===e||"attributes"===e},n=new MutationObserver(function(t){t.some(i)&&e()});return n.observe(t,{attributes:!0,attributeFilter:["style","class"],childList:!0,subtree:!0}),n},t.unwrap=function(t){var e=t.parentNode;if(e instanceof HTMLElement){e.insertAdjacentElement("beforebegin",t);var i=e.parentNode;i instanceof HTMLElement&&i.removeChild(e)}return t},t.wrap=function(t,e){return t.parentNode!==e&&(t.insertAdjacentElement("beforebegin",e),e.appendChild(t)),e},t.createPlaceholderElement=function(t){return void 0===t&&(t="div"),document.createElement(t)},t}();e.default=r},function(t,e,i){"use strict";var n=this&&this.__assign||function(){return(n=Object.assign||function(t){for(var e,i=1,n=arguments.length;i<n;i++)for(var r in e=arguments[i])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0});var r=i(3),s=i(1),o=i(0),c=function(t){var e=t.getBoundingClientRect().bottom+window.pageYOffset,i=window.getComputedStyle(t).paddingBottom;return e-(null!==i?parseInt(i,10):0)},a=function(){function t(t,e,i,c){if(void 0===e&&(e={observe:!0}),void 0===i&&(i=!0),void 0===c&&(c=o.noop),this.marginTop=0,this.isStickToBottom=!1,!t)throw new Error("[Stuck-js] Invalid element given");this.$$manager=r.getStickyManagerInstance(window).register(this),this.element=t,this.rect=this.element.getBoundingClientRect(),this.options=n({marginTop:0},e),this.marginTop=this.options.marginTop||0,this.setWrapperFromSelectorOrElement(this.options.wrapper),this.placeholder=new s.default(this.element,this.options.observe||!0,c||this.$$manager.bulkUpdate),this.element.dataset.stuck="",i&&this.$$manager.activate(),this.placeholder.shouldPlacehold=this.isSticky}return Object.defineProperty(t.prototype,"isSticky",{get:function(){return null!==this.element&&"fixed"===this.element.style.position},set:function(t){this.placeholder&&(this.placeholder.shouldPlacehold=t),this.element.dataset.stuck=t?t.toString():"",this.element.style.position=t?"fixed":"",this.element.style.top=t?this.top+"px":"",this.element.style.left=t?this.placeholder.updateRect().left+"px":"",t&&this.computePositionTopFromRect()},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"top",{get:function(){return this.$$additionalTop||0===this.$$additionalTop?this.$$additionalTop:this.marginTop},set:function(t){this.$$additionalTop=t,this.element.style.top=t?t+"px":this.marginTop+"px"},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"wrapper",{get:function(){return this.$$wrapper},enumerable:!0,configurable:!0}),t.prototype.setWrapperFromSelectorOrElement=function(t){if(!(document.body instanceof HTMLElement))throw new TypeError("[Stuck.js] document.body is not HTMLElement in this environment");var e=(this.placeholder&&this.placeholder.element||this.element).parentElement;this.$$wrapper=function(t){for(var e=[],i=1;i<arguments.length;i++)e[i-1]=arguments[i];if(t&&t instanceof HTMLElement)return t;var n=[t&&document.querySelector(t)].concat(e).find(function(t){return!!t&&t instanceof HTMLElement});if(n instanceof HTMLElement)return n;throw new TypeError("[Stuck-js] Could not find HTMLElement")}(t,e,document.body),this.floor=c(this.$$wrapper),this.options.wrapper=this.$$wrapper},t.prototype.destroy=function(){this.isSticky=!1,this.placeholder.destroy(),this.$$manager.unregister(this),delete this.placeholder,delete this.element,delete this.options},t.prototype.computePositionTopFromRect=function(t){void 0===t&&(t=this.element.getBoundingClientRect()),this.rect=t,this.floor=c(this.wrapper);var e=(this.floor||0)-window.pageYOffset;return this.rect.bottom>=e&&!this.isStickToBottom?(this.top=e-this.rect.height,void(this.isStickToBottom=!0)):this.isStickToBottom?this.rect.top>=this.marginTop?(this.top=this.marginTop,void(this.isStickToBottom=!1)):void(this.rect.top<this.marginTop&&(this.top=e-this.rect.height)):void 0},t.prototype.update=function(){var t=this.placeholder.element.getBoundingClientRect();if(!this.isSticky&&this.marginTop>t.top)this.isSticky=!0;else if(this.isSticky){if(t.top>=this.marginTop)return void(this.isSticky=!1);this.rect=this.element.getBoundingClientRect(),this.rect.left!==t.left&&(this.element.style.left=t.left+"px"),this.computePositionTopFromRect(this.rect)}},t}();e.default=a},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function t(t){this.$$stickies=[],this.$$activated=!1,this.$$bulkUpdateRequestId=null,this.$$window=t,this.bulkUpdate=this.bulkUpdate.bind(this),this.bulkPlaceholderUpdate=this.bulkPlaceholderUpdate.bind(this)}return t.getInstance=function(e){return this.$$instance||(this.$$instance=new t(e)),this.$$instance},t.prototype.register=function(t){return this.$$stickies=this.$$stickies.concat([t]),this},t.prototype.unregister=function(t){return this.$$stickies=this.$$stickies.filter(function(e){return e!==t}),this.$$stickies.length<1&&this.deactivate(),this},t.prototype.bulkUpdate=function(){var t=this;return this.$$bulkUpdateRequestId&&this.$$window.cancelAnimationFrame(this.$$bulkUpdateRequestId),this.$$bulkUpdateRequestId=this.$$window.requestAnimationFrame(function(){t.$$stickies.forEach(function(t){return t.update()})}),this},t.prototype.destroyAll=function(){return this.$$stickies.forEach(function(t){return t.destroy()}),this.$$stickies=[],this.deactivate(),this},t.prototype.activate=function(){return!this.$$activated&&this.$$stickies.length>0&&(this.$$window.addEventListener("scroll",this.bulkUpdate),this.$$window.addEventListener("resize",this.bulkPlaceholderUpdate),this.$$activated=!0),this.bulkUpdate(),this},t.prototype.deactivate=function(){return this.$$activated&&(this.$$window.removeEventListener("scroll",this.bulkUpdate),this.$$window.removeEventListener("resize",this.bulkPlaceholderUpdate),this.$$activated=!1),this},t.prototype.bulkPlaceholderUpdate=function(){var t=this;this.$$bulkUpdateRequestId&&this.$$window.cancelAnimationFrame(this.$$bulkUpdateRequestId),this.$$bulkUpdateRequestId=this.$$window.requestAnimationFrame(function(){t.$$stickies.forEach(function(t){t.placeholder.update(),t.update()})})},t}();e.getStickyManagerInstance=function(t){return n.getInstance(t)}},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=i(1);e.Placeholder=n.default;var r=i(2);e.Sticky=r.default;var s=i(5);e.Stuck=s.default,e.default=s.default},function(t,e,i){"use strict";var n=this&&this.__assign||function(){return(n=Object.assign||function(t){for(var e,i=1,n=arguments.length;i<n;i++)for(var r in e=arguments[i])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)},r=this&&this.__rest||function(t,e){var i={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(i[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(t);r<n.length;r++)e.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(t,n[r])&&(i[n[r]]=t[n[r]])}return i};Object.defineProperty(e,"__esModule",{value:!0});var s=i(6),o=i(2),c=function(){function t(t,e,i){void 0===t&&(t=[]),void 0===e&&(e={observe:!0}),void 0===i&&(i=!0),this.$$instances=[],this.$$manager=s.getStuckManagerInstance(window).register(this),this.$$defaultOptions=e,this.create(t,i)}return t.prototype.create=function(t,e){var i=this;void 0===e&&(e=!0);var n=(Array.isArray(t)?t:[t]).reduce(function(t,n){return t.concat(i.register(n,e))},[]);return 0===n.length?[]:(this.$$manager.update(),n)},t.prototype.register=function(t,e){var i,s=this;void 0===e&&(e=!0);var c=t.selector,a=t.element,l=r(t,["selector","element"]),u=this.$$manager.stickyElements,h=function(t){if(t.element){var e=t.element;if(e instanceof HTMLElement)return[e];if(Array.isArray(e)||"object"==typeof e)return Array.from(e)}if(t.selector)return Array.from(document.querySelectorAll(t.selector)).filter(function(t){return t instanceof HTMLElement});throw new Error("[Stuck.js] No selector, element nor elements in setting")}({selector:c,element:a}).filter(function(t){return!u.includes(t)}).map(function(t){return new o.default(t,n({},s.$$defaultOptions,l),!1,function(){s.$$manager.update()})});return(i=this.$$manager).addStickies.apply(i,[e].concat(h)),this.$$instances=this.$$instances.concat(h),h},Object.defineProperty(t.prototype,"stickies",{get:function(){return this.$$instances},enumerable:!0,configurable:!0}),t.prototype.destroy=function(){this.$$manager.unregister(this),this.$$instances=[]},t}();e.default=c},function(t,e,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=i(3),r=i(0),s=function(){function t(t){this.$$stucks=[],this.$$stickies=[],this.$$stackingStickies=[],this.$$window=t}return t.getInstance=function(e){return this.$$instance||(this.$$instance=new t(e)),this.$$instance},t.prototype.register=function(t){return this.$$stucks=this.$$stucks.concat([t]),this},t.prototype.unregister=function(t){return this.destroyStickies.apply(this,t.stickies),this.$$stucks=this.$$stucks.filter(function(e){return e!==t}),this},Object.defineProperty(t.prototype,"stickies",{get:function(){return this.$$stickies},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"stickyElements",{get:function(){return this.$$stickies.map(function(t){return t.element})},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"stackingStickies",{get:function(){return this.$$stackingStickies},enumerable:!0,configurable:!0}),t.prototype.addStickies=function(t){for(var e=[],i=1;i<arguments.length;i++)e[i-1]=arguments[i];return this.$$stickies=this.$$stickies.concat(e),t&&(this.$$stackingStickies=this.$$stackingStickies.concat(e)),n.getStickyManagerInstance(this.$$window).activate(),this},t.prototype.destroyStickies=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return t.forEach(function(t){return t.destroy()}),this.$$stickies=this.$$stickies.filter(function(e){return!t.includes(e)}),this.$$stackingStickies=this.$$stackingStickies.filter(function(e){return!t.includes(e)}),this.$$stackingStickies.length>0&&this.update(),this},t.prototype.update=function(){return this.$$stackingStickies=this.stackingStickies.filter(function(t,e,i){return i.indexOf(t)===e}).map(function(t){return{instance:t,rect:t.placeholder.updateRect()}}).sort(function(t,e){var i=t.rect,n=e.rect;return i.top-n.top}).reduce(function(t,e){var i=t.instances,n=t.ceiling,r=e.instance;return r.marginTop=r.options.marginTop+n,{instances:i.concat([r]),ceiling:r.rect.height+r.marginTop}},{instances:[],ceiling:0}).instances,n.getStickyManagerInstance(this.$$window).bulkUpdate(),this.$$stickies=r.stableSort(this.stickies,function(t,e){return t.placeholder.cachedRect.top-e.placeholder.cachedRect.top}),this},t}();e.getStuckManagerInstance=function(t){return s.getInstance(t)}}])});