问题:
凡蓝<div>
我们有overflow:hidden
.现在我想以自定义的同步*方式滚动这些 div,而不管在白色容器中的什么位置 <div>
我滚动。我的想法是我可以创建一个绝对定位的透明 <div>
作为白色容器的直接 child ,并给它一个溢出的 child :
其中蓝色容器的 z-index 比原来的两个文本容器高:
.container {
width: 100vw;
height: 100vh;
z-index: 10;
position: absolute;
overflow-y: scroll;
}
所以最终的结果看起来是这样的:
现在我希望能够滚动覆盖容器但捕获底层元素中的其他鼠标事件(如文本选择)。
我的目标是在滚动覆盖容器时使用 JavaScript 手动滚动底层容器。
问题:
鉴于有 no way使用 css 属性有选择地禁用指针事件 pointer-events
, 是否有任何其他方法可以仅启用覆盖元素的滚动事件,同时将其他指针事件传递给底层元素?
背景:
*我正在尝试实现的与 Perforce P4Merge 使用他们的 diff 工具所做的类似。他们有一个垂直滚动条用于 2 个代码块,我假设滚动高度大于两个代码块中的任何一个。在某些情况下,滚动事件会滚动两个代码块,有时只是其中一个,而在其他情况下,它们会以不同的速度滚动(取决于添加和删除的内容)。
更新:
原始实现是用 React 编写的,在该代码中我不需要 margin-left: -18px;
在 scrollable-container
显示滚动条。不知道为什么。此外,如果您愿意,这里有一个代码笔:codepen snippet
body {
overflow-y: hidden;
}
.app {
overflow-y: hidden;
position: relative;
display: flex;
flex-direction: row;
z-index: 0;
}
.scrollable-container {
width: 100vw;
height: 100vh;
z-index: 10;
margin-left: -18px;
position: absolute;
overflow-y: scroll;
}
.scrollable-content {
width: 500px;
height: 1600px;
}
.non-scrollable-container {
flex: 1;
height: 100vh;
overflow-y: hidden;
}
.bridge {
width: 40px;
background: linear-gradient(white, black);
cursor: ew-resize;
height: 100vh;
}
#original {
background: linear-gradient(red, yellow);
height: 2100px;
}
#modified {
background: linear-gradient(blue, green);
height: 1600px;
}
<div class="app">
<div class="scrollable-container">
<div class="scrollable-content"></div>
</div>
<div class="non-scrollable-container">
<div id="original" class="codeBlock">
Content I want to select
</div>
</div>
<div class="bridge"></div>
<div class="non-scrollable-container">
<div id="modified" class="codeBlock">
Content I want to select
</div>
</div>
</div>
最佳答案
这个问题已经很老了,但是万一有人在这里寻找类似的东西,我找到了解决方案。 我使用 Java 脚本事件监听器暂时禁用 mousedown 上的指针事件,并重新启用其父级鼠标弹起时的指针事件
function addlistener() {
var scrollable = document.getElementsByClassName("scrollable-container")[0];
scrollable.addEventListener('mousedown', function() {
this.style.pointerEvents = "none";
document.elementFromPoint(event.clientX, event.clientY).click();
}, false);
document.getElementsByClassName("app")[0].addEventListener('mouseup', function(e) {
scrollable.style.pointerEvents = "all";
}, false);
}
body {
overflow-y: hidden;
}
.app {
overflow-y: hidden;
position: relative;
display: flex;
flex-direction: row;
z-index: 0;
}
.scrollable-container {
width: 100vw;
height: 100vh;
margin-left: -18px;
position: absolute;
overflow-y: scroll;
z-index: 10;
}
.scrollable-content {
width: 500px;
height: 1600px;
}
.non-scrollable-container {
flex: 1;
height: 100vh;
overflow-y: hidden;
}
.bridge {
width: 40px;
background: linear-gradient(white, black);
cursor: ew-resize;
height: 100vh;
}
#original {
background: linear-gradient(red, yellow);
height: 2100px;
}
#modified {
background: linear-gradient(blue, green);
height: 1600px;
}
<body onload="addlistener()">
<div class="app">
<div class="scrollable-container">
<div class="scrollable-content"></div>
</div>
<div class="non-scrollable-container">
<div id="original" class="codeBlock">
Content I want to select
</div>
</div>
<div class="bridge"></div>
<div class="non-scrollable-container">
<div id="modified" class="codeBlock">
Content I want to select
</div>
</div>
</div>
</body>
关于javascript - 禁用除覆盖 div 上的滚动之外的所有指针事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50315332/