[英]double click jquery mobile
i have code to download image: 我有下载图片的代码:
<a href="$img" class="download-img" download><img src="$img"></a>
and i have function to check double click from mobile devices: 并且我具有检查来自移动设备的双击的功能:
var touchtime = 0;
$('.download-img').on('click', function() {
if(touchtime == 0) {
//set first click
touchtime = new Date().getTime();
} else {
//compare first click to this click and see if they occurred within double click threshold
if(((new Date().getTime())-touchtime) < 800) {
//double click occurred
alert("double clicked");
touchtime = 0;
} else {
//not a double click so set as a new first click
touchtime = new Date().getTime();
}
}
});
how i can join this 2 part of codes, to make download image from double click, but not single 我如何才能加入代码的这2部分,以使双击下载图片,但不能单点击
If you capture the event argument, you can call e.preventDefault()
when it's a single click. 如果捕获事件参数,则
e.preventDefault()
单击一下即可调用e.preventDefault()
。
$('.download-img').on('click', function(e) {
// ^ capture the event
if(touchtime == 0) {
//set first click
touchtime = new Date().getTime();
e.preventDefault();
} else {
...
}
});
You could also use the dblclick
event instead of rolling your own double click logic. 您也可以使用
dblclick
事件,而不是滚动自己的双击逻辑。
Here is what I came up with. 这是我想出的。 Though definitely check into that
dblclick
event MrCode mentioned in his answer. 虽然肯定检查成
dblclick
在他的回答中提到的事件MrCode。
$('.download-img').on('click', function(e){
var $link = $(this);
//if the time now minus the time stored on the link, or 0 if there is not one, is less than 800, it's valid
if ( Date.now() - ($link.data('touchtime') || 0) < 800 ) {
console.log('double click');
} else {
//time did not exist, or it exceeded the 800 threshold, set a new one
$link.data('touchtime', Date.now());
//prevent the click
e.preventDefault();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.