UNPKG

lm-mask

Version:

* 作者:niuguimin * 邮箱:15275151030@163.com * 版本:**`1.0.0`**

63 lines (43 loc) 2.3 kB
export const helper = (function (bodyCls) { // let requestAnimationFrame = // window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame // || window.msRequestAnimationFrame; // let cancelAnimationFrame = // window.cancelAnimationFrame || window.mozCancelAnimationFrame || window.webkitCancelAnimationFrame // || window.msCancelAnimationFrame; // let scrollTop = 0; // let animationId = undefined; return { afterOpen() { if (document.querySelectorAll('.enter-animation').length > 0) return; document.body.classList.add(bodyCls); // let clientWidth = document.body.clientWidth || document.documentElement.clientWidth; // let moveWidth = clientWidth / 2; // // 在弹出层显示之前,记录当前的滚动位置 // scrollTop = document.body.scrollTop || document.documentElement.scrollTop; // // 使body脱离文档流 // document.body.classList.add(bodyCls); // // 把脱离文档流的body拉上去!否则页面会回到顶部! // document.body.style.top = -scrollTop + 'px'; // // body fixed时 在ipad pro上居中 // document.body.style.marginLeft = -moveWidth + 'px'; }, beforeClose() { //mask隐藏时恢复底层滚动,只有在最后一层mask隐藏时处理 if (document.querySelectorAll('.enter-animation').length !== 1) return; document.body.classList.remove(bodyCls); // document.body.style.top = ''; // document.body.style.marginLeft = ''; // //document.body.scrollTop = document.documentElement.scrollTop = scrollTop; // // bugfix - 环境:58App | IOS 点击关闭会抖动问题 // // 备注:但是加了延时后偶尔还会产生小的抖动 // this.cancelAnimation(); // animationId = requestAnimationFrame(() => { // document.body.scrollTop = document.documentElement.scrollTop = scrollTop; // }); }, cancelAnimation() { //animationId && cancelAnimationFrame(this.animationId); } }; })('mask-show');