在移动 Safari 上禁用滚动和弹跳效果
我正在开发一个基于浏览器的应用程序,目前我正在为 iPad 的移动 Safari 浏览器开发和设计样式。
我正在寻找两件事:如何禁用不需要垂直滚动的页面?如何禁用弹性反弹效果?
I'm working on a browser based app, currently I'm developing and styling for the mobile Safari browser for iPad.
I'm looking for two things: How can I disable vertical scrolling for pages that don't require it? And how can I disable the elastic bounce effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
这个答案不再是最先进的,除非您正在为非常旧的 iOS 设备进行开发...请参阅其他解决方案
2011 年答案:对于在 iOS Safari 中运行的 web/html 应用程序,您需要一些东西对于
iOS 5,您可能需要考虑以下因素: document.ontouchmove 和滚动iOS 5
2014 年 9 月更新:
更彻底的方法可以在这里找到:https://github.com/luster-io/prevent -过度滚动。有关此内容以及大量有用的 Web 应用程序建议,请参阅
http://www.luster.io/blog/9-29-14-mobile-web-checklist.html2016 年 3 月更新:最后链接不再有效 - 请参阅https://web.archive.org/web/20151103001838/http://www.luster.io/blog/9-29-14-mobile-web-checklist.html 用于存档版本反而。感谢@falsarella 指出了这一点。
This answer is no longer state of the art, unless you are developing for a very old iOS device... Please see other solutions
2011 answer: For a web/html app running inside iOS Safari you want something like
For iOS 5 you may want to take the following into account: document.ontouchmove and scrolling on iOS 5
Update September 2014:
A more thorough approach can be found here: https://github.com/luster-io/prevent-overscroll. For that and a whole lot of useful webapp advice, see
http://www.luster.io/blog/9-29-14-mobile-web-checklist.htmlUpdate March 2016: That last link is no longer active - see https://web.archive.org/web/20151103001838/http://www.luster.io/blog/9-29-14-mobile-web-checklist.html for the archived version instead. Thanks @falsarella for pointing that out.
您还可以将 body/html 的位置更改为固定:
You can also change the position of the body/html to fixed:
为了防止在现代移动浏览器上滚动,您需要添加被动:false。在找到这个解决方案之前,我一直在费尽心思让它发挥作用。我只在互联网上的另一处发现了这一点。
To prevent scrolling on modern mobile browsers you need to add the passive: false. I had been pulling my hair out getting this to work until I found this solution. I have only found this mentioned in one other place on the internet.
iOS 16 现在支持 css
overscroll-behavior
。 iOS 16 设备,为了防止弹性反弹效果,请将以下 CSS 添加到 html 根请注意,提供的解决方案仅在内容大于视口时禁用弹性反弹效果。
如果您还想完全禁用 iOS 设备上主页的滚动,请使用
css
overscroll-behavior
is now supported in iOS 16. If you are targeting > iOS 16 devices, to prevent elastic bounce effect, add the following CSS to the html rootPlease note, the solution provided only disables elastic bounce effect when content is larger than viewport.
If you also want to completely disable scrolling in main page on iOS devices, use
您可以使用此 jQuery 代码片段来执行此操作:
这将阻止垂直滚动以及页面上发生的任何反弹效果。
You can use this jQuery code snippet to do this:
This will block the vertical scrolling and also any bounce back effect occurring on your pages.
在容器上,您可以在元素内部设置反弹效果
源:http://www.kylejlarson.com/blog/2011/fixed-elements-and-scrolling-divs-in-ios-5/
On container you can set bounce effect inside element
Source: http://www.kylejlarson.com/blog/2011/fixed-elements-and-scrolling-divs-in-ios-5/
我知道这有点偏离滑雪道,但我一直在使用 Swiffy 将 Flash 转换为交互式 HTML5 游戏,并遇到了相同的滚动问题,但没有找到有效的解决方案。
我遇到的问题是 Swiffy 阶段占据了整个屏幕,因此一旦加载,文档 touchmove 事件就永远不会被触发。
如果我尝试将相同的事件添加到 Swiffy 容器中,它会在舞台加载后立即被替换。
最后,我通过将 touchmove 事件应用于舞台内的每个 DIV 来解决它(相当混乱)。由于这些 div 也在不断变化,我需要不断检查它们。
这是我的解决方案,看起来效果很好。我希望这对尝试找到与我相同的解决方案的其他人有所帮助。
I know this is slightly off-piste but I've been using Swiffy to convert Flash into an interactive HTML5 game and came across the same scrolling issue but found no solutions that worked.
The problem I had was that the Swiffy stage was taking up the whole screen, so as soon as it had loaded, the document touchmove event was never triggered.
If I tried to add the same event to the Swiffy container, it was replaced as soon as the stage had loaded.
In the end I solved it (rather messily) by applying the touchmove event to every DIV within the stage. As these divs were also ever-changing, I needed to keep checking them.
This was my solution, which seems to work well. I hope it's helpful for anyone else trying to find the same solution as me.
删除ipad safari的代码:禁用滚动和弹跳效果
如果文档中有canvas标签,有时会影响Canvas内对象的可用性(例如:对象的移动);所以添加下面的代码来修复它。
Code to To remove ipad safari: disable scrolling, and bounce effect
If you have canvas tag inside document, sometime it will affect the usability of object inside Canvas(example: movement of object); so add below code to fix it.
这些解决方案都不适合我。我就是这样做的。
none of the solutions works for me. This is how I do it.
尝试这个 JS 解决方案:
防止默认的 Safari 滚动和弹跳手势,而不分离触摸事件侦听器。
Try this JS sollutuion:
Prevents default Safari scrolling and bounce gestures without detaching your touch event listeners.
改进的答案@Ben Bos和@Tim评论
此CSS将有助于防止CSS重新渲染的滚动和性能问题,因为位置改变/没有宽度和高度的小滞后
improved answer @Ben Bos and commented by @Tim
This css will help prevent scrolling and performance issue with css re-render because position changed / little lagging without width and height
这个 CSS 解决方案对我有用(仅限 iOS,我发现它最烦人):
This CSS solution worked for me (iOS-only, where I found it most annoying):
对于那些使用 MyScript Web 应用程序并且正在努力解决正文滚动/拖动问题(在 iPad 和平板电脑上)而不是实际编写的人:
已修复对我来说。
For those who are using MyScript the Web App and are struggling with the body scrolling/dragging (on iPad and Tablets) instead of actually writing:
<body touch-action="none" unresolved>
That fixed it for me.
在 iPhone 上测试。只需在目标元素容器上使用此 css,它就会改变滚动行为,当手指离开屏幕时滚动行为会停止。
https://developer.mozilla.org/en -US/docs/Web/CSS/-webkit-overflow-scrolling
Tested in iphone. Just use this css on target element container and it will change the scrolling behaviour, which stops when finger leaves the screen.
https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-overflow-scrolling
您可以使用 js 来防止滚动:
而不仅仅是在打开/关闭模式时运行/停止
toggleScroll
函数。像这样
toggleScroll(true) /toggleScroll(false)
(这仅适用于 iOS,在 Android 上不起作用)
You can use js for prevent scroll:
And than just run/stop
toggleScroll
func when you opnen/close modal.Like this
toggleScroll(true) / toggleScroll(false)
(This is only for iOS, on Android not working)
尝试这个切换
webkitOverflowScrolling
样式的 JS 解决方案。这里的技巧是,这种风格关闭后,移动版 Safari 会转为普通滚动并防止过度弹跳——可惜的是,它无法取消正在进行的拖动。这个复杂的解决方案还跟踪onscroll
,因为顶部的弹跳会使scrollTop
为可跟踪的负值。该解决方案在 iOS 12.1.1 上进行了测试,有一个缺点:在加速滚动时,由于重置样式可能不会立即取消它,仍然会发生单次过度弹跳。Try this JS solution that toggles
webkitOverflowScrolling
style. The trick here is that this style is off, mobile Safari goes to ordinary scrolling and prevents over-bounce — alas, it is not able to cancel ongoing drag. This complex solution also tracksonscroll
as bounce over the top makesscrollTop
negative that may be tracked. This solution was tested on iOS 12.1.1 and has single drawback: while accelerating the scroll single over-bounce still happens as resetting the style may not cancel it immediately.考虑以下架构:
这个 css 可以工作:
Consider the following architecture:
this css will work:
使用 JQuery
Using JQuery
对于那些不想摆脱弹跳而只是想知道弹跳何时停止的人(例如开始计算屏幕距离),您可以执行以下操作(容器是溢出的容器元素):
For those of you who don't want to get rid of the bouncing but just to know when it stops (for example to start some calculation of screen distances), you can do the following (container is the overflowing container element):
禁用 safari 弹跳滚动效果:
Disable safari bounce scrolling effect:
当带有滚动的菜单打开并且位于滚动高度的顶部或底部时,我在抓取后台的 html 元素时遇到了问题。我尝试了很多事情。将 html 位置设置为
fixed
是我最接近锁定屏幕的方法,但在 PWA 中,它导致底部出现白色区域,我无法修复。最后我找到了一个对我有用的解决方案
I had an issue with grabbing the html element in the background, when a menu with scroll was open and either at the top or at the bottom at the scroll height. I tried lots of things. Setting html position to
fixed
was the closest I got to lock the screen, but in the PWA it resulted in a white area at the bottom, that I couldn't fix.Finally I've found a solution, that worked for me ????:
Because it only seems to be an issue on iOS, I have targeted the devices from iPhone X to 12 Pro Max:
This is preventing any kind of scroll, touch or grab in the html or body elements, and scroll is still working in the menu or where else specified. Cheers.
这就是我解决它的方法:
然后你只需在你想要/不想要能够滚动或具有滚动行为的代码部分中切换布尔值
This is how I solved it:
Then you just have to toggle the boolean in the section of code that you want/don't-want to be able to scroll or have the scroll behaviour
与愤怒的猕猴桃类似,我使用高度而不是位置来让它工作:
Similar to angry kiwi I got it to work using height rather than position:
解决方案经过测试,适用于 iOS 12.x
这是我遇到的问题:
当我滚动图库时,主体总是自行滚动(人类滑动并不是真正水平的),这使得我的图库毫无用处。
这是我在画廊开始滚动时所做的事情
以及当我的画廊结束滚动时所做的事情...
希望这会有所帮助〜
Solution tested, works on iOS 12.x
This is problem I was encountering :
While I scrolling my gallery, the body always scrolling itself (human swipe aren't really horizontal), that makes my gallery useless.
Here's what I did while my gallery start scrolling
And when my gallery end its scrolling...
Hope this helps~