简体   繁体   English

模态打开时禁用触摸滚动

[英]Disable touch scrolling when modal is open

I'm using full-screen modals on my site on mobile. 我在移动设备上的网站上使用全屏模式。 The problem is that touching the modal will cause the body page to move even though it's overflow:hidden; 问题在于,触摸模态将导致正文页面移动,即使它已溢出:

Here's what bootstrap says about that: 这是有关引导的内容:

Support for overflow: hidden on the element is quite limited in iOS and Android. 支持溢出:在iOS和Android中,隐藏在元素上的功能非常有限。 To that end, when you scroll past the top or bottom of a modal in either of those devices' browsers, the content will begin to scroll. 为此,当您在任一设备的浏览器中滚动经过某个模式的顶部或底部时,内容将开始滚动。

What can I do to prevent that? 我该怎么做才能防止这种情况?

Setting the body to position:fixed causes the scroll to jump to the top in an ugly way. 将主体设置为position:fixed会使滚动条以难看的方式跳到顶部。 What's the best solution if anyone came up with one? 如果有人想出最好的解决方案?

I had the same question myself before. 我自己之前也有过同样的问题。

What I did is changed the body to overflow:hidden and fixed height equal to modal height and remembered document scroll position. 我所做的就是将主体更改为overflow:hidden且固定的height等于模式height并记住文档的滚动位置。 After closing the modal, I've applied remembered scroll position back and height to auto . 关闭模态后,我将回想起的滚动位置和height应用于auto

It is so complicated because I needed my modals to be bigger than window and I wanted to be able to scroll the modal. 它是如此复杂,因为我需要模态大于窗口,并且希望能够滚动模态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM