tua-body-scroll-lock
Advanced tools
Comparing version 1.0.0 to 1.1.0-0
@@ -1,3 +0,3 @@ | ||
declare const lock: (targetElement?: HTMLElement | undefined) => void; | ||
declare const unlock: (targetElement?: HTMLElement | undefined) => void; | ||
declare const lock: (targetElements?: HTMLElement[] | undefined) => void; | ||
declare const unlock: (targetElements?: HTMLElement[] | undefined) => void; | ||
export { lock, unlock }; |
/** | ||
* tua-body-scroll-lock v1.0.0 | ||
* (c) 2019 Evinma, BuptStEve | ||
* tua-body-scroll-lock v1.1.0-0 | ||
* (c) 2020 Evinma, BuptStEve | ||
* @license MIT | ||
@@ -110,27 +110,32 @@ */ | ||
}; | ||
const checkTargetElement = (targetElement) => { | ||
if (targetElement) | ||
const checkTargetElement = (targetElements) => { | ||
if (targetElements) | ||
return; | ||
if (targetElement === null) | ||
if (targetElements === null) | ||
return; | ||
console.warn(`If scrolling is also required in the floating layer, ` + | ||
`the target element must be provided.`); | ||
`the targetElements must be provided.`); | ||
}; | ||
const lock = (targetElement) => { | ||
const lock = (targetElements) => { | ||
if (isServer()) | ||
return; | ||
checkTargetElement(targetElement); | ||
checkTargetElement(targetElements); | ||
if (detectOS().ios) { | ||
// iOS | ||
if (targetElement && lockedElements.indexOf(targetElement) === -1) { | ||
targetElement.ontouchstart = (event) => { | ||
initialClientY = event.targetTouches[0].clientY; | ||
initialClientX = event.targetTouches[0].clientX; | ||
}; | ||
targetElement.ontouchmove = (event) => { | ||
if (event.targetTouches.length !== 1) | ||
return; | ||
handleScroll(event, targetElement); | ||
}; | ||
lockedElements.push(targetElement); | ||
if (targetElements) { | ||
const elementArray = Array.isArray(targetElements) ? targetElements : [targetElements]; | ||
elementArray.forEach((element) => { | ||
if (element && lockedElements.indexOf(element) === -1) { | ||
element.ontouchstart = (event) => { | ||
initialClientY = event.targetTouches[0].clientY; | ||
initialClientX = event.targetTouches[0].clientX; | ||
}; | ||
element.ontouchmove = (event) => { | ||
if (event.targetTouches.length !== 1) | ||
return; | ||
handleScroll(event, element); | ||
}; | ||
lockedElements.push(element); | ||
} | ||
}); | ||
} | ||
@@ -149,6 +154,6 @@ if (!documentListenerAdded) { | ||
}; | ||
const unlock = (targetElement) => { | ||
const unlock = (targetElements) => { | ||
if (isServer()) | ||
return; | ||
checkTargetElement(targetElement); | ||
checkTargetElement(targetElements); | ||
lockedNum -= 1; | ||
@@ -163,9 +168,12 @@ if (lockedNum > 0) | ||
// iOS | ||
if (targetElement) { | ||
const index = lockedElements.indexOf(targetElement); | ||
if (index !== -1) { | ||
targetElement.ontouchmove = null; | ||
targetElement.ontouchstart = null; | ||
lockedElements.splice(index, 1); | ||
} | ||
if (targetElements) { | ||
const elementArray = Array.isArray(targetElements) ? targetElements : [targetElements]; | ||
elementArray.forEach((element) => { | ||
const index = lockedElements.indexOf(element); | ||
if (index !== -1) { | ||
element.ontouchmove = null; | ||
element.ontouchstart = null; | ||
lockedElements.splice(index, 1); | ||
} | ||
}); | ||
} | ||
@@ -172,0 +180,0 @@ if (documentListenerAdded) { |
@@ -1,1 +0,1 @@ | ||
const t=()=>"undefined"==typeof window,e=t=>document.querySelector(t),o=t=>{t=t||navigator.userAgent;const e=/(iPad).*OS\s([\d_]+)/.test(t);return{ios:!e&&/(iPhone\sOS)\s([\d_]+)/.test(t)||e,android:/(Android);?[\s\/]+([\d.]+)?/.test(t)}};let n=0,i=0,s=0,l=null,r=!1;const d=[],c=function(e){if(t())return!1;if(!e)throw new Error("options must be provided");let o=!1;const n={get passive(){o=!0}},i=()=>{};window.addEventListener("__TUA_BSL_TEST_PASSIVE__",i,n),window.removeEventListener("__TUA_BSL_TEST_PASSIVE__",i,n);const{capture:s}=e;return o?e:void 0!==s&&s}({passive:!1}),h=t=>{t.cancelable&&t.preventDefault()},a=a=>{t()||(o().ios?(a&&-1===d.indexOf(a)&&(a.ontouchstart=t=>{i=t.targetTouches[0].clientY,s=t.targetTouches[0].clientX},a.ontouchmove=t=>{1===t.targetTouches.length&&((t,e)=>{if(e){const{scrollTop:o,scrollLeft:n,scrollWidth:l,scrollHeight:r,clientWidth:d,clientHeight:c}=e,a=t.targetTouches[0].clientX-s,u=t.targetTouches[0].clientY-i,f=Math.abs(u)>Math.abs(a),g=u>0&&0===o,p=a>0&&0===n,v=a<0&&n+d+1>=l,y=u<0&&o+c+1>=r;if(f&&(g||y)||!f&&(p||v))return h(t)}t.stopPropagation()})(t,a)},d.push(a)),r||(document.addEventListener("touchmove",h,c),r=!0)):n<=0&&(l=o().android?(()=>{const t=e("html"),o=e("body"),n=t.scrollTop||o.scrollTop,i=Object.assign({},t.style),s=Object.assign({},o.style);return t.style.height="100%",t.style.overflow="hidden",o.style.top=`-${n}px`,o.style.width="100%",o.style.height="auto",o.style.position="fixed",o.style.overflow="hidden",()=>{t.style.height=i.height||"",t.style.overflow=i.overflow||"",["top","width","height","overflow","position"].forEach(t=>{o.style[t]=s[t]||""}),window.scrollTo(0,n)}})():(()=>{const t=e("body"),o=Object.assign({},t.style),n=window.innerWidth-document.body.clientWidth;return t.style.overflow="hidden",t.style.boxSizing="border-box",t.style.paddingRight=`${n}px`,()=>{["overflow","boxSizing","paddingRight"].forEach(e=>{t.style[e]=o[e]||""})}})()),n+=1)},u=e=>{if(!(t()||(n-=1)>0))if(o().ios||"function"!=typeof l){if(e){const t=d.indexOf(e);-1!==t&&(e.ontouchmove=null,e.ontouchstart=null,d.splice(t,1))}r&&(document.removeEventListener("touchmove",h,c),r=!1)}else l()};export{a as lock,u as unlock}; | ||
const e=()=>"undefined"==typeof window,t=e=>document.querySelector(e),o=e=>{e=e||navigator.userAgent;const t=/(iPad).*OS\s([\d_]+)/.test(e);return{ios:!t&&/(iPhone\sOS)\s([\d_]+)/.test(e)||t,android:/(Android);?[\s\/]+([\d.]+)?/.test(e)}};let i=0,n=0,s=0,r=null,l=!1;const c=[],d=function(t){if(e())return!1;if(!t)throw new Error("options must be provided");let o=!1;const i={get passive(){o=!0}},n=()=>{};window.addEventListener("__TUA_BSL_TEST_PASSIVE__",n,i),window.removeEventListener("__TUA_BSL_TEST_PASSIVE__",n,i);const{capture:s}=t;return o?t:void 0!==s&&s}({passive:!1}),h=e=>{e.cancelable&&e.preventDefault()},a=a=>{if(!e()){if(o().ios){if(a){(Array.isArray(a)?a:[a]).forEach(e=>{e&&-1===c.indexOf(e)&&(e.ontouchstart=e=>{n=e.targetTouches[0].clientY,s=e.targetTouches[0].clientX},e.ontouchmove=t=>{1===t.targetTouches.length&&((e,t)=>{if(t){const{scrollTop:o,scrollLeft:i,scrollWidth:r,scrollHeight:l,clientWidth:c,clientHeight:d}=t,a=e.targetTouches[0].clientX-s,u=e.targetTouches[0].clientY-n,f=Math.abs(u)>Math.abs(a),y=u>0&&0===o,g=a>0&&0===i,p=a<0&&i+c+1>=r,v=u<0&&o+d+1>=l;if(f&&(y||v)||!f&&(g||p))return h(e)}e.stopPropagation()})(t,e)},c.push(e))})}l||(document.addEventListener("touchmove",h,d),l=!0)}else i<=0&&(r=o().android?(()=>{const e=t("html"),o=t("body"),i=e.scrollTop||o.scrollTop,n=Object.assign({},e.style),s=Object.assign({},o.style);return e.style.height="100%",e.style.overflow="hidden",o.style.top=`-${i}px`,o.style.width="100%",o.style.height="auto",o.style.position="fixed",o.style.overflow="hidden",()=>{e.style.height=n.height||"",e.style.overflow=n.overflow||"",["top","width","height","overflow","position"].forEach(e=>{o.style[e]=s[e]||""}),window.scrollTo(0,i)}})():(()=>{const e=t("body"),o=Object.assign({},e.style),i=window.innerWidth-document.body.clientWidth;return e.style.overflow="hidden",e.style.boxSizing="border-box",e.style.paddingRight=`${i}px`,()=>{["overflow","boxSizing","paddingRight"].forEach(t=>{e.style[t]=o[t]||""})}})());i+=1}},u=t=>{if(!(e()||(i-=1)>0))if(o().ios||"function"!=typeof r){if(t){(Array.isArray(t)?t:[t]).forEach(e=>{const t=c.indexOf(e);-1!==t&&(e.ontouchmove=null,e.ontouchstart=null,c.splice(t,1))})}l&&(document.removeEventListener("touchmove",h,d),l=!1)}else r()};export{a as lock,u as unlock}; |
/** | ||
* tua-body-scroll-lock v1.0.0 | ||
* (c) 2019 Evinma, BuptStEve | ||
* tua-body-scroll-lock v1.1.0-0 | ||
* (c) 2020 Evinma, BuptStEve | ||
* @license MIT | ||
@@ -130,27 +130,32 @@ */ | ||
var checkTargetElement = function checkTargetElement(targetElement) { | ||
if (targetElement) return; | ||
if (targetElement === null) return; | ||
var checkTargetElement = function checkTargetElement(targetElements) { | ||
if (targetElements) return; | ||
if (targetElements === null) return; | ||
if (process.env.NODE_ENV === 'production') return; | ||
console.warn("If scrolling is also required in the floating layer, " + "the target element must be provided."); | ||
console.warn("If scrolling is also required in the floating layer, " + "the targetElements must be provided."); | ||
}; | ||
var lock = function lock(targetElement) { | ||
var lock = function lock(targetElements) { | ||
if (isServer()) return; | ||
checkTargetElement(targetElement); | ||
checkTargetElement(targetElements); | ||
if (detectOS().ios) { | ||
// iOS | ||
if (targetElement && lockedElements.indexOf(targetElement) === -1) { | ||
targetElement.ontouchstart = function (event) { | ||
initialClientY = event.targetTouches[0].clientY; | ||
initialClientX = event.targetTouches[0].clientX; | ||
}; | ||
if (targetElements) { | ||
var elementArray = Array.isArray(targetElements) ? targetElements : [targetElements]; | ||
elementArray.forEach(function (element) { | ||
if (element && lockedElements.indexOf(element) === -1) { | ||
element.ontouchstart = function (event) { | ||
initialClientY = event.targetTouches[0].clientY; | ||
initialClientX = event.targetTouches[0].clientX; | ||
}; | ||
targetElement.ontouchmove = function (event) { | ||
if (event.targetTouches.length !== 1) return; | ||
handleScroll(event, targetElement); | ||
}; | ||
element.ontouchmove = function (event) { | ||
if (event.targetTouches.length !== 1) return; | ||
handleScroll(event, element); | ||
}; | ||
lockedElements.push(targetElement); | ||
lockedElements.push(element); | ||
} | ||
}); | ||
} | ||
@@ -169,5 +174,5 @@ | ||
var unlock = function unlock(targetElement) { | ||
var unlock = function unlock(targetElements) { | ||
if (isServer()) return; | ||
checkTargetElement(targetElement); | ||
checkTargetElement(targetElements); | ||
lockedNum -= 1; | ||
@@ -182,10 +187,13 @@ if (lockedNum > 0) return; | ||
if (targetElement) { | ||
var index = lockedElements.indexOf(targetElement); | ||
if (targetElements) { | ||
var elementArray = Array.isArray(targetElements) ? targetElements : [targetElements]; | ||
elementArray.forEach(function (element) { | ||
var index = lockedElements.indexOf(element); | ||
if (index !== -1) { | ||
targetElement.ontouchmove = null; | ||
targetElement.ontouchstart = null; | ||
lockedElements.splice(index, 1); | ||
} | ||
if (index !== -1) { | ||
element.ontouchmove = null; | ||
element.ontouchstart = null; | ||
lockedElements.splice(index, 1); | ||
} | ||
}); | ||
} | ||
@@ -192,0 +200,0 @@ |
/** | ||
* tua-body-scroll-lock v1.0.0 | ||
* (c) 2019 Evinma, BuptStEve | ||
* tua-body-scroll-lock v1.1.0-0 | ||
* (c) 2020 Evinma, BuptStEve | ||
* @license MIT | ||
@@ -136,26 +136,31 @@ */ | ||
var checkTargetElement = function checkTargetElement(targetElement) { | ||
if (targetElement) return; | ||
if (targetElement === null) return; | ||
console.warn("If scrolling is also required in the floating layer, " + "the target element must be provided."); | ||
var checkTargetElement = function checkTargetElement(targetElements) { | ||
if (targetElements) return; | ||
if (targetElements === null) return; | ||
console.warn("If scrolling is also required in the floating layer, " + "the targetElements must be provided."); | ||
}; | ||
var lock = function lock(targetElement) { | ||
var lock = function lock(targetElements) { | ||
if (isServer()) return; | ||
checkTargetElement(targetElement); | ||
checkTargetElement(targetElements); | ||
if (detectOS().ios) { | ||
// iOS | ||
if (targetElement && lockedElements.indexOf(targetElement) === -1) { | ||
targetElement.ontouchstart = function (event) { | ||
initialClientY = event.targetTouches[0].clientY; | ||
initialClientX = event.targetTouches[0].clientX; | ||
}; | ||
if (targetElements) { | ||
var elementArray = Array.isArray(targetElements) ? targetElements : [targetElements]; | ||
elementArray.forEach(function (element) { | ||
if (element && lockedElements.indexOf(element) === -1) { | ||
element.ontouchstart = function (event) { | ||
initialClientY = event.targetTouches[0].clientY; | ||
initialClientX = event.targetTouches[0].clientX; | ||
}; | ||
targetElement.ontouchmove = function (event) { | ||
if (event.targetTouches.length !== 1) return; | ||
handleScroll(event, targetElement); | ||
}; | ||
element.ontouchmove = function (event) { | ||
if (event.targetTouches.length !== 1) return; | ||
handleScroll(event, element); | ||
}; | ||
lockedElements.push(targetElement); | ||
lockedElements.push(element); | ||
} | ||
}); | ||
} | ||
@@ -174,5 +179,5 @@ | ||
var unlock = function unlock(targetElement) { | ||
var unlock = function unlock(targetElements) { | ||
if (isServer()) return; | ||
checkTargetElement(targetElement); | ||
checkTargetElement(targetElements); | ||
lockedNum -= 1; | ||
@@ -187,10 +192,13 @@ if (lockedNum > 0) return; | ||
if (targetElement) { | ||
var index = lockedElements.indexOf(targetElement); | ||
if (targetElements) { | ||
var elementArray = Array.isArray(targetElements) ? targetElements : [targetElements]; | ||
elementArray.forEach(function (element) { | ||
var index = lockedElements.indexOf(element); | ||
if (index !== -1) { | ||
targetElement.ontouchmove = null; | ||
targetElement.ontouchstart = null; | ||
lockedElements.splice(index, 1); | ||
} | ||
if (index !== -1) { | ||
element.ontouchmove = null; | ||
element.ontouchstart = null; | ||
lockedElements.splice(index, 1); | ||
} | ||
}); | ||
} | ||
@@ -197,0 +205,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).bodyScrollLock={})}(this,function(e){"use strict";var t=function(){return"undefined"==typeof window},o=function(e){return document.querySelector(e)},n=function(e){e=e||navigator.userAgent;var t=/(iPad).*OS\s([\d_]+)/.test(e);return{ios:!t&&/(iPhone\sOS)\s([\d_]+)/.test(e)||t,android:/(Android);?[\s\/]+([\d.]+)?/.test(e)}};var i=0,r=0,s=0,c=null,l=!1,d=[],u=function(e){if(t())return!1;if(!e)throw new Error("options must be provided");var o=!1,n={get passive(){o=!0}},i=function(){};window.addEventListener("__TUA_BSL_TEST_PASSIVE__",i,n),window.removeEventListener("__TUA_BSL_TEST_PASSIVE__",i,n);var r=e.capture;return o?e:void 0!==r&&r}({passive:!1}),f=function(e){e.cancelable&&e.preventDefault()};e.lock=function(e){var a,h,v,p,y;t()||(n().ios?(e&&-1===d.indexOf(e)&&(e.ontouchstart=function(e){r=e.targetTouches[0].clientY,s=e.targetTouches[0].clientX},e.ontouchmove=function(t){1===t.targetTouches.length&&function(e,t){if(t){var o=t.scrollTop,n=t.scrollLeft,i=t.scrollWidth,c=t.scrollHeight,l=t.clientWidth,d=t.clientHeight,u=e.targetTouches[0].clientX-s,a=e.targetTouches[0].clientY-r,h=Math.abs(a)>Math.abs(u);if(h&&(a>0&&0===o||a<0&&o+d+1>=c)||!h&&(u>0&&0===n||u<0&&n+l+1>=i))return f(e)}e.stopPropagation()}(t,e)},d.push(e)),l||(document.addEventListener("touchmove",f,u),l=!0)):i<=0&&(c=n().android?(a=o("html"),h=o("body"),v=a.scrollTop||h.scrollTop,p=Object.assign({},a.style),y=Object.assign({},h.style),a.style.height="100%",a.style.overflow="hidden",h.style.top="-".concat(v,"px"),h.style.width="100%",h.style.height="auto",h.style.position="fixed",h.style.overflow="hidden",function(){a.style.height=p.height||"",a.style.overflow=p.overflow||"",["top","width","height","overflow","position"].forEach(function(e){h.style[e]=y[e]||""}),window.scrollTo(0,v)}):function(){var e=o("body"),t=Object.assign({},e.style),n=window.innerWidth-document.body.clientWidth;return e.style.overflow="hidden",e.style.boxSizing="border-box",e.style.paddingRight="".concat(n,"px"),function(){["overflow","boxSizing","paddingRight"].forEach(function(o){e.style[o]=t[o]||""})}}()),i+=1)},e.unlock=function(e){if(!(t()||(i-=1)>0))if(n().ios||"function"!=typeof c){if(e){var o=d.indexOf(e);-1!==o&&(e.ontouchmove=null,e.ontouchstart=null,d.splice(o,1))}l&&(document.removeEventListener("touchmove",f,u),l=!1)}else c()},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).bodyScrollLock={})}(this,function(e){"use strict";var t=function(){return"undefined"==typeof window},o=function(e){return document.querySelector(e)},n=function(e){e=e||navigator.userAgent;var t=/(iPad).*OS\s([\d_]+)/.test(e);return{ios:!t&&/(iPhone\sOS)\s([\d_]+)/.test(e)||t,android:/(Android);?[\s\/]+([\d.]+)?/.test(e)}};var i=0,r=0,s=0,c=null,l=!1,d=[],u=function(e){if(t())return!1;if(!e)throw new Error("options must be provided");var o=!1,n={get passive(){o=!0}},i=function(){};window.addEventListener("__TUA_BSL_TEST_PASSIVE__",i,n),window.removeEventListener("__TUA_BSL_TEST_PASSIVE__",i,n);var r=e.capture;return o?e:void 0!==r&&r}({passive:!1}),f=function(e){e.cancelable&&e.preventDefault()};e.lock=function(e){if(!t()){if(n().ios){if(e)(Array.isArray(e)?e:[e]).forEach(function(e){e&&-1===d.indexOf(e)&&(e.ontouchstart=function(e){r=e.targetTouches[0].clientY,s=e.targetTouches[0].clientX},e.ontouchmove=function(t){1===t.targetTouches.length&&function(e,t){if(t){var o=t.scrollTop,n=t.scrollLeft,i=t.scrollWidth,c=t.scrollHeight,l=t.clientWidth,d=t.clientHeight,u=e.targetTouches[0].clientX-s,a=e.targetTouches[0].clientY-r,h=Math.abs(a)>Math.abs(u);if(h&&(a>0&&0===o||a<0&&o+d+1>=c)||!h&&(u>0&&0===n||u<0&&n+l+1>=i))return f(e)}e.stopPropagation()}(t,e)},d.push(e))});l||(document.addEventListener("touchmove",f,u),l=!0)}else i<=0&&(c=n().android?(a=o("html"),h=o("body"),v=a.scrollTop||h.scrollTop,y=Object.assign({},a.style),p=Object.assign({},h.style),a.style.height="100%",a.style.overflow="hidden",h.style.top="-".concat(v,"px"),h.style.width="100%",h.style.height="auto",h.style.position="fixed",h.style.overflow="hidden",function(){a.style.height=y.height||"",a.style.overflow=y.overflow||"",["top","width","height","overflow","position"].forEach(function(e){h.style[e]=p[e]||""}),window.scrollTo(0,v)}):function(){var e=o("body"),t=Object.assign({},e.style),n=window.innerWidth-document.body.clientWidth;return e.style.overflow="hidden",e.style.boxSizing="border-box",e.style.paddingRight="".concat(n,"px"),function(){["overflow","boxSizing","paddingRight"].forEach(function(o){e.style[o]=t[o]||""})}}());var a,h,v,y,p;i+=1}},e.unlock=function(e){if(!(t()||(i-=1)>0))if(n().ios||"function"!=typeof c){if(e)(Array.isArray(e)?e:[e]).forEach(function(e){var t=d.indexOf(e);-1!==t&&(e.ontouchmove=null,e.ontouchstart=null,d.splice(t,1))});l&&(document.removeEventListener("touchmove",f,u),l=!1)}else c()},Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "tua-body-scroll-lock", | ||
"version": "1.0.0", | ||
"version": "1.1.0-0", | ||
"description": "🔐Body scroll locking that just works with everything", | ||
@@ -5,0 +5,0 @@ "main": "dist/tua-bsl.umd.js", |
@@ -134,15 +134,17 @@ # tua-body-scroll-lock | ||
### TargetElement needs scrolling(iOS only) | ||
In some scenarios, when scrolling is prohibited, some elements still need to scroll, at this point, pass the targetElement. | ||
### TargetElements needs scrolling(iOS only) | ||
In some scenarios, when scrolling is prohibited, some elements still need to scroll, at this point, pass the targetElements. | ||
```js | ||
import { lock, unlock } from 'tua-body-scroll-lock' | ||
const elementOne = document.querySelector('#elementOne') | ||
const elementTwo = document.querySelector('#elementTwo') | ||
const targetElement = document.querySelector('#someElementId') | ||
const targetElements = [elementOne, elementTwo] | ||
lock(targetElement) | ||
unlock(targetElement) | ||
lock(targetElements) | ||
unlock(targetElements) | ||
``` | ||
> The `targetElement` is not required on the PC and Android. | ||
> The `targetElements` is not required on the PC and Android. | ||
@@ -149,0 +151,0 @@ ## Demo |
@@ -101,5 +101,5 @@ import { | ||
const checkTargetElement = (targetElement?: HTMLElement) => { | ||
if (targetElement) return | ||
if (targetElement === null) return | ||
const checkTargetElement = (targetElements?: Array<HTMLElement>) => { | ||
if (targetElements) return | ||
if (targetElements === null) return | ||
if (process.env.NODE_ENV === 'production') return | ||
@@ -109,26 +109,32 @@ | ||
`If scrolling is also required in the floating layer, ` + | ||
`the target element must be provided.` | ||
`the targetElements must be provided.` | ||
) | ||
} | ||
const lock = (targetElement?: HTMLElement) => { | ||
const lock = (targetElements?: Array<HTMLElement>) => { | ||
if (isServer()) return | ||
checkTargetElement(targetElement) | ||
checkTargetElement(targetElements) | ||
if (detectOS().ios) { | ||
// iOS | ||
if (targetElement && lockedElements.indexOf(targetElement) === -1) { | ||
targetElement.ontouchstart = (event) => { | ||
initialClientY = event.targetTouches[0].clientY | ||
initialClientX = event.targetTouches[0].clientX | ||
} | ||
if (targetElements) { | ||
const elementArray = Array.isArray(targetElements) ? targetElements : [targetElements] | ||
targetElement.ontouchmove = (event) => { | ||
if (event.targetTouches.length !== 1) return | ||
elementArray.forEach((element: HTMLElement) => { | ||
if (element && lockedElements.indexOf(element) === -1) { | ||
element.ontouchstart = (event) => { | ||
initialClientY = event.targetTouches[0].clientY | ||
initialClientX = event.targetTouches[0].clientX | ||
} | ||
handleScroll(event, targetElement) | ||
} | ||
element.ontouchmove = (event) => { | ||
if (event.targetTouches.length !== 1) return | ||
lockedElements.push(targetElement) | ||
handleScroll(event, element) | ||
} | ||
lockedElements.push(element) | ||
} | ||
}) | ||
} | ||
@@ -149,6 +155,6 @@ | ||
const unlock = (targetElement?: HTMLElement) => { | ||
const unlock = (targetElements?: Array<HTMLElement>) => { | ||
if (isServer()) return | ||
checkTargetElement(targetElement) | ||
checkTargetElement(targetElements) | ||
lockedNum -= 1 | ||
@@ -166,10 +172,15 @@ | ||
// iOS | ||
if (targetElement) { | ||
const index = lockedElements.indexOf(targetElement) | ||
if (targetElements) { | ||
const elementArray = Array.isArray(targetElements) ? targetElements : [targetElements] | ||
if (index !== -1) { | ||
targetElement.ontouchmove = null | ||
targetElement.ontouchstart = null | ||
lockedElements.splice(index, 1) | ||
} | ||
elementArray.forEach((element: HTMLElement) => { | ||
const index = lockedElements.indexOf(element) | ||
if (index !== -1) { | ||
element.ontouchmove = null | ||
element.ontouchstart = null | ||
lockedElements.splice(index, 1) | ||
} | ||
}) | ||
} | ||
@@ -176,0 +187,0 @@ |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
48706
759
173
2