forked from Amphiluke/floating-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.floatingscroll.es6.min.js
6 lines (6 loc) · 3.59 KB
/
jquery.floatingscroll.es6.min.js
1
2
3
4
5
6
/*!
floating-scroll v3.2.0
https://amphiluke.github.io/floating-scroll/
(c) 2023 Amphiluke
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("jquery")):"function"==typeof define&&define.amd?define(["jquery"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jQuery)}(this,(function(e){"use strict";const t="horizontal",i="vertical";let n={init(e,i){let n=this;n.orientationProps=(e=>{let i=e===t;return{ORIENTATION:e,SIZE:i?"width":"height",X_SIZE:i?"height":"width",OFFSET_SIZE:i?"offsetWidth":"offsetHeight",OFFSET_X_SIZE:i?"offsetHeight":"offsetWidth",CLIENT_SIZE:i?"clientWidth":"clientHeight",CLIENT_X_SIZE:i?"clientHeight":"clientWidth",INNER_X_SIZE:i?"innerHeight":"innerWidth",SCROLL_SIZE:i?"scrollWidth":"scrollHeight",SCROLL_POS:i?"scrollLeft":"scrollTop",START:i?"left":"top",X_START:i?"top":"left",X_END:i?"bottom":"right"}})(i);let o=e.closest(".fl-scrolls-body");o.length&&(n.scrollBody=o),n.container=e[0],n.visible=!0,n.initWidget(),n.updateAPI(),n.addEventHandlers(),n.skipSyncContainer=n.skipSyncWidget=!1},initWidget(){let t=this;const{ORIENTATION:i,SIZE:n,SCROLL_SIZE:o}=t.orientationProps;let l=t.widget=e(`<div class="fl-scrolls" data-orientation="${i}"></div>`);e("<div></div>").appendTo(l)[n](t.container[o]),l.appendTo(t.container)},addEventHandlers(){let t=this;(t.eventHandlers=[{$el:e(window),handlers:{"destroyDetached.fscroll"({namespace:e}){"fscroll"===e&&t.destroyDetachedAPI()}}},{$el:t.scrollBody||e(window),handlers:{scroll(){t.updateAPI()},resize(){t.updateAPI()}}},{$el:t.widget,handlers:{scroll(){t.visible&&!t.skipSyncContainer&&t.syncContainer(),t.skipSyncContainer=!1}}},{$el:e(t.container),handlers:{scroll(){t.skipSyncWidget||t.syncWidget(),t.skipSyncWidget=!1},focusin(){setTimeout((()=>{t.widget&&t.syncWidget()}),0)},"update.fscroll"({namespace:e}){"fscroll"===e&&t.updateAPI()},"destroy.fscroll"({namespace:e}){"fscroll"===e&&t.destroyAPI()}}}]).forEach((({$el:e,handlers:t})=>e.bind(t)))},checkVisibility(){let e=this,{widget:t,container:i,scrollBody:n}=e;const{SCROLL_SIZE:o,OFFSET_SIZE:l,X_START:s,X_END:r,INNER_X_SIZE:d,CLIENT_X_SIZE:c}=e.orientationProps;let a=t[0][o]<=t[0][l];if(!a){let e=i.getBoundingClientRect(),t=n?n[0].getBoundingClientRect()[r]:window[d]||document.documentElement[c];a=e[r]<=t||e[s]>t}e.visible===a&&(e.visible=!a,t.toggleClass("fl-scrolls-hidden"))},syncContainer(){let e=this;const{SCROLL_POS:t}=e.orientationProps;let i=e.widget[0][t];e.container[t]!==i&&(e.skipSyncWidget=!0,e.container[t]=i)},syncWidget(){let e=this;const{SCROLL_POS:t}=e.orientationProps;let i=e.container[t];e.widget[0][t]!==i&&(e.skipSyncContainer=!0,e.widget[0][t]=i)},updateAPI(){let t=this;const{SIZE:i,X_SIZE:n,OFFSET_X_SIZE:o,CLIENT_SIZE:l,CLIENT_X_SIZE:s,SCROLL_SIZE:r,START:d}=t.orientationProps;let{widget:c,container:a,scrollBody:h}=t,S=a[l],f=a[r];c[i](S),h||c.css(d,`${a.getBoundingClientRect()[d]}px`),e("div",c)[i](f),f>S&&c[n](c[0][o]-c[0][s]+1),t.syncWidget(),t.checkVisibility()},destroyAPI(){let e=this;e.eventHandlers.forEach((({$el:e,handlers:t})=>e.unbind(t))),e.widget.remove(),e.eventHandlers=e.widget=e.container=e.scrollBody=null},destroyDetachedAPI(){e.contains(document.body,this.container)||this.destroyAPI()}};e.fn.floatingScroll=function(o="init",l={}){if("init"===o){let{orientation:o=t}=l;if(o!==t&&o!==i)throw new Error(`Scrollbar orientation should be either “${t}” or “${i}”`);this.each(((t,i)=>Object.create(n).init(e(i),o)))}else Object.prototype.hasOwnProperty.call(n,`${o}API`)&&this.trigger(`${o}.fscroll`);return this},e((()=>{e("body [data-fl-scrolls]").each(((t,i)=>{let n=e(i);n.floatingScroll("init",n.data("flScrolls")||{})}))}))}));