在 Mac 上防止在 Safari 中拖动滚动
Prevent drag scrolling in Safari on Mac
看看this codepen
角落里有红色方块。
如果您单击(使用鼠标或触摸板)此方块并将其向下拖动 - 在 Safari 中 Mac .parent
元素将向下滚动。
即使 .parent 有 overflow-y: hidden;
在所有其他浏览器中不会发生滚动。
如何防止在 safari 上滚动?
作为一个粗略的解决方案,我可以听滚动并在它不为零时覆盖 scrollTop,但也许有更优雅的解决方案?
使用css你可以给出指针事件:none;以避免在单击它时发生任何事件。
这绝对是一个有趣的发现!你会认为这是不可能的,因为你将 overflow-y
设置为 none
。尽管如此,您可以使用此 JavScript 阻止此行为并禁止在父级内部单击:
const parent = document.querySelector('.parent');
parent.addEventListener('mousedown', e => e.preventDefault());
通过将 preventDefault()
添加到 mousedown
上触发的事件,您可以防止浏览器评估与该点击相关的任何进一步事件,包括拖动。这是实际操作:
const parent = document.querySelector('.parent');
parent.addEventListener('mousedown', e => e.preventDefault());
.parent {
width: 200px;
height: 200px;
border: 1px dashed black;
overflow-x: auto;
overflow-y: hidden;
}
.child {
width: 200%;
height: 200%;
background: linear-gradient(to right bottom, red, tomato), linear-gradient(to bottom, white, black);
background-size: 50px 50px, 100% 100%;
background-repeat: no-repeat;
background-position: center;
}
<div class="parent">
<div class="child"></div>
</div>
您可以添加中介div。然后,为其设置宽度、高度、溢出属性。
.parent {
width: 200px;
height: 200px;
border: 1px dashed black;
overflow-x: auto;
overflow-y: hidden;
}
/** intermediary div */
.parent > div {
width: 200%;
height: 100%;
overflow: hidden;
}
.child {
width: 100%; /** this should be 100% **/
height: 200%;
background: linear-gradient(to right bottom, red, tomato), linear-gradient(to bottom, white, black);
background-size: 50px 50px, 100% 100%;
background-repeat: no-repeat;
background-position: center;
}
<div class="parent">
<div>
<div class="child"></div>
</div>
</div>
或将 pointer-events:none
添加到父级。
.parent {
/* This prevents dragging */
pointer-events: none;
width: 200px;
height: 200px;
border: 1px dashed black;
overflow-x: auto;
overflow-y: hidden;
}
.child {
width: 200%;
height: 200%;
background: linear-gradient(to right bottom, red, tomato), linear-gradient(to bottom, white, black);
background-size: 50px 50px, 100% 100%;
background-repeat: no-repeat;
background-position: center;
}
<div class="parent">
<div class="child"></div>
</div>
看看this codepen
角落里有红色方块。
如果您单击(使用鼠标或触摸板)此方块并将其向下拖动 - 在 Safari 中 Mac .parent
元素将向下滚动。
即使 .parent 有 overflow-y: hidden;
在所有其他浏览器中不会发生滚动。
如何防止在 safari 上滚动?
作为一个粗略的解决方案,我可以听滚动并在它不为零时覆盖 scrollTop,但也许有更优雅的解决方案?
使用css你可以给出指针事件:none;以避免在单击它时发生任何事件。
这绝对是一个有趣的发现!你会认为这是不可能的,因为你将 overflow-y
设置为 none
。尽管如此,您可以使用此 JavScript 阻止此行为并禁止在父级内部单击:
const parent = document.querySelector('.parent');
parent.addEventListener('mousedown', e => e.preventDefault());
通过将 preventDefault()
添加到 mousedown
上触发的事件,您可以防止浏览器评估与该点击相关的任何进一步事件,包括拖动。这是实际操作:
const parent = document.querySelector('.parent');
parent.addEventListener('mousedown', e => e.preventDefault());
.parent {
width: 200px;
height: 200px;
border: 1px dashed black;
overflow-x: auto;
overflow-y: hidden;
}
.child {
width: 200%;
height: 200%;
background: linear-gradient(to right bottom, red, tomato), linear-gradient(to bottom, white, black);
background-size: 50px 50px, 100% 100%;
background-repeat: no-repeat;
background-position: center;
}
<div class="parent">
<div class="child"></div>
</div>
您可以添加中介div。然后,为其设置宽度、高度、溢出属性。
.parent {
width: 200px;
height: 200px;
border: 1px dashed black;
overflow-x: auto;
overflow-y: hidden;
}
/** intermediary div */
.parent > div {
width: 200%;
height: 100%;
overflow: hidden;
}
.child {
width: 100%; /** this should be 100% **/
height: 200%;
background: linear-gradient(to right bottom, red, tomato), linear-gradient(to bottom, white, black);
background-size: 50px 50px, 100% 100%;
background-repeat: no-repeat;
background-position: center;
}
<div class="parent">
<div>
<div class="child"></div>
</div>
</div>
或将 pointer-events:none
添加到父级。
.parent {
/* This prevents dragging */
pointer-events: none;
width: 200px;
height: 200px;
border: 1px dashed black;
overflow-x: auto;
overflow-y: hidden;
}
.child {
width: 200%;
height: 200%;
background: linear-gradient(to right bottom, red, tomato), linear-gradient(to bottom, white, black);
background-size: 50px 50px, 100% 100%;
background-repeat: no-repeat;
background-position: center;
}
<div class="parent">
<div class="child"></div>
</div>