首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery中的Longpress / longclick事件支持/插件

jQuery中的Longpress / longclick事件支持/插件
EN

Stack Overflow用户
提问于 2012-08-27 22:51:07
回答 3查看 23.5K关注 0票数 8

我正在做一个需要鼠标悬停菜单的网站。从可访问性的角度来看,我不建议使用鼠标悬停菜单,但使用jQuery实现起来相当容易。

问题:我们还需要支持触摸屏设备(平板电脑)。在这样的设备上,您没有鼠标,因此mouseover事件不起作用。我希望jQuery有一个长按事件,但它没有。我确实找到了一个使用谷歌的jQuery longclick plugin,但它是针对jQuery 1.4的,所以我并不热衷于使用它。此外,jQuery插件网站目前正在维护中,所以这并不是很有帮助。

那么问题来了:有没有一个优雅的jQuery 1.7 / 1.8插件来支持长按/长点击事件?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-30 00:30:54

事实证明,对于jQuery 1.4和jQuery 1.8,您可以只使用现有的longclick plugin

代码语言:javascript
复制
$("#area").mousedown(function(){
    $("#result").html("Waiting for it...");
});
$("#area").longclick(500, function(){
    $("#result").html("You longclicked. Nice!");
});
$("#area").click(function(){
    $("#result").html("You clicked. Bummer.");
});
代码语言:javascript
复制
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="http://rawgit.com/pisi/Longclick/master/jquery.longclick-min.js"></script>
    
<p id="area">Click me!</p>
<p id="result">You didn't click yet.</p>

票数 7
EN

Stack Overflow用户

发布于 2012-08-28 00:30:25

您可以在各种鼠标事件期间使用setTimeout进行延迟检查。整合jQuery的$.data()来存储跨事件的超时(在每个元素上)应该可以帮助您完成这一切。下面是一个例子:

HTML:

代码语言:javascript
复制
<ul id="menu">
    <li><a href="#" onclick="return false;" class="test"></a></li>
    <li><a href="#" onclick="return false;" class="test"></a></li>
</ul>

JS:

代码语言:javascript
复制
var mousepress_time = 1000;  // Maybe hardcode this value in setTimeout
var orig_message = "Click Here";  // Remove this line
var held_message = "Down";  // Remove this line

$(document).ready(function () {
    $(".test")
        .html(orig_message)  // Remove this line
        .on("mousedown", function () {
            console.log("#########mousedown");  // Remove this line
            var $this = $(this);
            $(this).data("checkdown", setTimeout(function () {
                // Add your code to run
                $this.html(held_message);  // Remove this line
            }, mousepress_time));
        }).on("mouseup", function () {
            clearTimeout($(this).data("checkdown"));
            console.log("#######mouseup");  // Remove this line
            $(this).html(orig_message);  // Remove this line
        }).on("mouseout", function () {
            clearTimeout($(this).data("checkdown"));
            console.log("#######mouseout");  // Remove this line
            $(this).html(orig_message);  // Remove this line
        });
});

演示http://jsfiddle.net/7jKYa/10/

还有更多的事情要做,因为你也加入了悬停,但在大多数情况下,我认为这是你想要的。

如果需要,它可以很容易地转换为插件,否则我认为它可以单独工作。不过,我希望这能有所帮助!

票数 5
EN

Stack Overflow用户

发布于 2015-03-25 20:50:12

你可以给它计时。

代码语言:javascript
复制
function onImageMouseDown(e){
    var d = new Date();
    md_time = d.getTime(); // Milliseconds since 1 Apr 1970
}

function onImageMouseUp(e){
    var d = new Date();
    var long_click = (d.getTime()-md_time)>1000;
    if (long_click){
        // Click lasted longer than 1s (1000ms)
    }else{
        // Click lasted less than 1s
    }
    md_time = 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12144374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档