如何在 Jquery 中检测 div 上的长按? [英] How to detect a long press on a div in Jquery?
本文介绍了如何在 Jquery 中检测 div 上的长按?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当用户在 div 上按下 2 秒
时,我想执行一些功能.
I would like to execute some function when the user presses for 2 seconds
on a div.
有可能吗?
这是我检测div点击的代码
Here is my code to detect the click on the div
$('div').mousedown(function() {
});
推荐答案
只需同时观察 mousedown
和 mouseup
并计算差异.这是一个例子.
Just watch both mousedown
and mouseup
and calculate the difference. Here's an example.
(function() {
// how many milliseconds is a long press?
var longpress = 3000;
// holds the start time
var start;
jQuery( "#pressme" ).on( 'mousedown', function( e ) {
start = new Date().getTime();
} );
jQuery( "#pressme" ).on( 'mouseleave', function( e ) {
start = 0;
} );
jQuery( "#pressme" ).on( 'mouseup', function( e ) {
if ( new Date().getTime() >= ( start + longpress ) ) {
alert('long press!');
} else {
alert('short press!');
}
} );
}());
这篇关于如何在 Jquery 中检测 div 上的长按?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文