javascript - Locking the body scroll, blocks target element scrolling
问题描述
I am using the following library for locking scrolling of the body of page and only allow scrolling on my modal if it is open.
https://www.npmjs.com/package/body-scroll-lock
My modal AddItemModal
is a portal so inside index.html
file I have this.
<body>
<div id="root"></div>
<div id="add-item-modal"></div>
</body>
NavigationIcons component (where modal is opened)
const NavigationIcons = (props) => {
let targetElement = document.getElementById("add-item-modal");
useEffect(() => {
...
return () => clearAllBodyScrollLocks();
}, []);
const renderAddItemModal = () => {
if (props.addItem) {
disableBodyScroll(targetElement);
return <AddItemModal />;
}
};
return (
<div className="main-4" onClick={() => props.openAddItemModal()}>
{renderAddItemModal()}
</div>
);
}
AddItemModalHeader component (where modal is closed)
const AddItemModalHeader = (props) => {
let targetElement = document.getElementById("add-item-modal");
return (
<div className="modal-header">
add item
<div className="close" onClick={(e) => {
props.handleModalClose(e);
enableBodyScroll(targetElement);
}}>
close
</div>
</div>
)
}
In documentation it is written that targetElement
must be the the modal that I want to display. But since the application is rendered inside root
div and modal displayed in add-item-modal
, shouldn't it be the root
instead and apply to it the provided functions such as enableBodyScroll
, disableBodyScroll
, clearAllBodyScrollLocks
. I did try with both root
and add-item-modal
and in both cases the scrolling does not work on the modal when I test from my iOS device.
The example provided there does not make this clear to me. Could someone explain what am I doing wrong here?
UPDATE
Others that might have this issue on iOS devices please see the following well-explained answer from Github thread https://github.com/ionic-team/ionic-v1/issues/155#issuecomment-411110252
解决方案
一些已知的问题body-scroll-lock
是
- 不适用于 Android 网络视图
- 在带有鼠标滚轮的 PC 上不起作用
- 如果您触摸某处而不是 targetElement,则在 iOS 上不起作用
- 必须通过 targetElement,即使没有必要
请知道我与 没有任何联系
tua-body-scroll-lock
。
tua-body-scroll-lock
具有相同的功能body-scroll-lock
。喜欢
disableBodyScroll
别名lock
enableBodyScroll
别名unlock
clearAllBodyScrollLocks
别名clearBodyLocks
我用一个例子做了一个小提琴tua-body-scroll-lock
你的代码应该像
import {lock, unlock, clearBodyLocks} from 'tua-body-scroll-lock';
const NavigationIcons = (props) => {
...
useEffect(() => {
...
return () => clearBodyLocks();
}, []);
const renderAddItemModal = () => {
if (props.addItem) {
lock(targetElement);
return <AddItemModal />;
}
};
...
}
const AddItemModalHeader = (props) => {
return (
<div className="modal-header">
add item
<div className="close" onClick={(e) => {
props.handleModalClose(e);
unlock(targetElement);
}}>
close
</div>
</div>
)
}
推荐阅读
- node.js - 使用exceljs将excel文件导出到客户端
- python - 如何从 cmd 获取 python.exe 路径?
- java - Java中的变量初始化
- css - TailwindCSS Flexbox 导航栏链接未内联显示
- vue.js - 如何将 json 文件导入和读取到 Vue / Gridsome main.js
- seal - 旋转密文向量时出现错误,
- docker - kubernetes kubectl 是否与镜像一起运行创建部署 yaml 文件
- jquery - 在 laravel 6 中使用 jquery
- amazon-web-services - 如何使用应用程序和通过 3G/LTE 连接的物联网设备设置云
- gentics-mesh - 如何在 Gentics Mesh 中设置新节点的默认权限?