使用指针事件时,在智能手机上无法找到适当的事件来触发基于手指的触摸操作(在Chrome Android和Chrome Devtools中进行了测试)。
我需要的是:如果您通过保持手指按下并在屏幕上移动时“穿过”元素,则需要“悬停”事件。
也就是说,将手指放在元素外面,通过它移动,并且只有在完全穿过元素后才将手指抬起。
我附加了一个代码片段以清楚说明:我不需要蓝色元素的事件,我只需要片段中红色元素的相应“进入/离开”事件。示例JS代码将为鼠标触发,但在移动设备上不会触发任何console.infos。
我需要的是:如果您通过保持手指按下并在屏幕上移动时“穿过”元素,则需要“悬停”事件。
也就是说,将手指放在元素外面,通过它移动,并且只有在完全穿过元素后才将手指抬起。
我附加了一个代码片段以清楚说明:我不需要蓝色元素的事件,我只需要片段中红色元素的相应“进入/离开”事件。示例JS代码将为鼠标触发,但在移动设备上不会触发任何console.infos。
var elem = document.querySelector(".element");
elem.addEventListener("pointerover", function() {
console.clear();
console.info("pointerover triggered");
});
elem.addEventListener("pointerenter", function() {
console.clear();
console.info("pointerenter triggered");
});
elem.addEventListener("pointerleave", function() {
console.clear();
console.info("pointerleave triggered");
});
.outer {
width: 100px;
height: 100px;
border: 3px solid grey;
font-size: 12px;
color: white;
text-align:center;
touch-action: none;
}
.start {
position: relative;
top:0px;
left:0px;
width: 100px;
height: 20px;
background-color: blue;
}
.element {
position: relative;
top: 20px;
left: 0px;
width: 100px;
height: 20px;
background-color: red;
}
.end {
position: relative;
top: 40px;
right: 0;
width: 100px;
height: 20px;
background-color: blue;
}
<div class="outer">
<div class="start">Start touch here</div>
<div class="element">Move over here</div>
<div class="end">End touch here</div>
</div>
pointermove
也不会触发pointerover
。 - Dynalonpointerover
在移动设备上也不起作用。恐怕您需要手动实现这样的逻辑。 - Roberto Zvjerković