想要在按下按钮时终止事件



我有一个要求,我想杀死相同的事件(touchstart(,同时按住按钮一段时间(对于iOS(。

$("#btn").on("touchstart", function(evt){
currBtn = $(this);
timeoutId = setTimeout(()=>{
changeOpacity(currBtn);
}, 1500);
return false;
});

我做了一个解决的解决方案。您只需更改touchstart事件的mousedown事件:

function changeOpacity() {
$("#btn").css("opacity",0.3);
}
function addEvent() {
//add the event
$( "#btn" ).on("mousedown",onTouchStarts);
}
function removesEvent() {
//removes the event
$( "#btn" ).off("mousedown",onTouchStarts);
}
function onTouchStarts(evt){
console.log("mousedown");
currBtn = $(this);
timeoutId = setTimeout(()=>{
changeOpacity(currBtn);
removesEvent();
}, 1500);
return false;
}
addEvent();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn">Button</button>

使用preventDefault()这会抑制通常会触发的任何默认行为

evt.preventDefault();

最新更新