iOS 15 已经发布,新版本的 Safari 也随之发布,带来了无处不在的下拉刷新功能。不管你喜欢与否,单页应用程序不太喜欢这样。
以下是在 iPhone 版 Chrome 上禁用它的方法:
知道如何在 iOS 15 的 Safari 中执行相同操作吗?
CSS
overscroll-behavior-y: contain
没有任何效果。
适用于我们用例的非常粗略的解决方案是为 body 元素设置
overflow: hidden;
,但随后您需要为所有内容提供一个溢出的容器元素,否则滚动会被阻止。
<body>
<div id="container"> Content </div>
</body>
body {
overflow: hidden;
}
#container {
height: 100vh;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
}
2022 年,这对我来说效果很好:
body {
overflow: hidden;
}
我通过将目标元素的 CSS 属性 touch-action 设置为 none 来禁用此行为。
touch-action:none;
https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action
这是棘手的部分:
/* prevent pull-to-refresh for Safari 16+ */
@media screen and (pointer: coarse) {
@supports (-webkit-backdrop-filter: blur(1px)) and (overscroll-behavior-y: none) {
html {
min-height: 100.3%;
overscroll-behavior-y: none;
}
}
}
/* prevent pull-to-refresh for Safari 9~15 */
@media screen and (pointer: coarse) {
@supports (-webkit-backdrop-filter: blur(1px)) and (not (overscroll-behavior-y: none)) {
html {
height: 100%;
overflow: hidden;
}
body {
margin: 0px;
max-height: 100%; /* or `height: calc(100% - 16px);` if body has default margin */
overflow: auto;
-webkit-overflow-scrolling: touch;
}
/* in this case to disable pinch-zoom, set `touch-action: pan-x pan-y;` on `body` instead of `html` */
}
}
/* prevent pull-to-refresh for Chrome 63+ */
body{
overscroll-behavior-y: none;
}
顺便说一句如果您想禁用捏合缩放,请使用此:
/* prevent pinch-zoom for Chrome 36+, Safari 13+ */
html {
touch-action: pan-x pan-y;
min-height: 100%; /* prevent pinch-zoom at page bottom */
}
这个:
// prevent pinch-zoom for iOS Safari 9~12
if (window.GestureEvent && !('touchAction' in document.documentElement.style)) {
document.documentElement.addEventListener('gesturestart', (e)=>{e.preventDefault()}, {passive: false, capture:true});
}
还有这个:
<!-- prevent pinch-zoom for Chrome / old Safari -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
iNoBounce 会一直工作,直到您使用多于一根手指的手势。 假设你用两根手指下拉网页(例如),然后你就会得到刷新。
目前,唯一对我有用的方法(但它禁用缩放、滚动和其他触摸功能)是使用 event.preventDefault() 侦听“touchmove”并将被动设置为 false。
2013 年的库 iNoBounce (https://github.com/lazd/iNoBounce) 实际上在 iOS 15 上仍然表现得很好。
直接复制文档中的示例确实禁用了拉动刷新。