首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检测一个元素是否已经悬停了5秒

问检测一个元素是否已经悬停了5秒
EN

Stack Overflow用户
提问于 2013-11-13 17:38:01
回答 1查看 886关注 0票数 0

我有一个hover事件,它删除一个小按钮,并在它悬停时用一个完整的菜单栏替换它。

我想做的是,如果鼠标没有在菜单栏(this.element)上悬停5秒(每次发生hover事件时,计时器重置),则会发生相反的情况。

代码语言:javascript
复制
this.button.on('hover', function(e){

    t.admin_bar.show('slide', { 'direction': t.options.position }, 500);
    t.button.hide('slide', { 'direction': t.options.position }, 500);

});

更新

在迄今所提供的意见和答复的帮助下,我提出了这一点。然而,即使mouseenter和mouseleave事件正常工作,clearTimout()似乎并不是因为管理员栏仍然被隐藏。

代码语言:javascript
复制
_create_events : function(){

    var t = this;   // This object

    t.admin_bar = t.element

    /** Capture when the mouse is hovered over the 'Show admin bar' button */
    t.button.on('hover', function(e){

        t._show_admin_bar();
        t._set_timeout();   // Start a new timeout (to hide the admin bar if it's not hovered on)

    });

    /** Capture when the mouse is hovered over the admin bar */
    t.admin_bar.on('mouseenter', function(e){

        clearTimeout(t.timer);  // Clear the existing timeout

    });

    /** Capture when the mouse leaves the admin bar */
    t.admin_bar.on('mouseleave', function(e){

        t._set_timeout();   // Restart the timout

    });

}, // _create_events

_set_timeout : function(){

    var t = this;   // This object

    t.timer = setTimeout(function(){
        t._hide_admin_bar();
    }, 5000);

}, // _timeout

_show_admin_bar : function(){

    this.admin_bar.show('slide', { 'direction': this.options.position }, 500);  // Show the admin bar
    this.button.hide('slide', { 'direction': this.options.position }, 500);     // Hide the 'Show admin bar' button

}, // _show_bar

_hide_admin_bar : function(){

    this.admin_bar.hide('slide', { 'direction': this.options.position }, 500);  // Hide the admin bar
    this.button.show('slide', { 'direction': this.options.position }, 500);     // Show the 'Show admin bar' button

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-13 17:45:21

你可以试试类似..。

代码语言:javascript
复制
var timer;
function hide(){
    //Hide your menu bar
}
function show(){
    //Show your menu bar
}

$("#buttonID").hover(
  function() {
    clearTimeout(timer);
    show();
  }, function() {
    timer = setTimeout(function(){hide()},5000);
  }
);

更新:

问题在于您调用.on("hover" function... --这是一个绑定鼠标和鼠标离开的处理程序,在我看来是令人困惑的。我更喜欢只使用鼠标,或者使用悬浮()函数。所以我把你的代码改为

代码语言:javascript
复制
t.button.on('mouseenter', function (e) {
    t._show_admin_bar();
    t._set_timeout(); // Start a new timeout (to hide the admin bar if it's not hovered on)
 });

http://jsfiddle.net/KdStd/1/

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19960618

复制
相关文章

相似问题

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