2012-07-09 122 views
0
 $("#theDiv").hover(function(){ 
      $(this).animate({top: "300px", left: "400px", width: "50px" , height: "50px" },"Fast") 
     }); 

     $("#theDiv").click(function(){ 
      $(this).stop(); 
     }); 

代码如上。我试图停止悬停功能,当我点击它,但它不起作用。或者根本不可能?通过点击功能停止悬停功能

回答

0
$("#theDiv").on({ 
    mouseenter: function() { 
     $(this).animate({top: "300px", left: "400px", width: "50px" , height: "50px" },"Fast"); 
    }, 
    click: function() { 
     $(this).off('mouseenter').stop(true); 
    } 
}); 

FIDDLE

+0

我建议这个答案的变化,用'的setTimeout()'函数来重新启用了mouseenter,让用户有足够的时间移动他们的鼠标远离元素并避免重新触发悬停。 – Ohgodwhy 2012-07-09 02:58:39

+0

问题是“如何停止悬停功能”,你确定OP会希望它在设定的时间后重新开始吗? – adeneo 2012-07-09 02:59:57

+0

我在生活中唯一确定的两件事就是死亡和税收。我只是在想,如果能够对动画进行可重新渲染,将会很好,因为位于视口中间的元素可能会成为入侵的一部分。我的意思不外乎,对不起。 – Ohgodwhy 2012-07-09 03:03:12

1
$("#theDiv").mouseenter(function(){ 
    $(this).animate({ 
     top: "300px", 
     left: "400px", 
     width: "50px" , 
     height: "50px" 
    } , "fast"); 
}).click(function(){ 
    $(this).stop(true, false); 
}); 

.stop([clearQueue] [, jumpToEnd])

+0

这不起作用,点击后您将移动鼠标的第二个触发器。 – Ohgodwhy 2012-07-09 02:57:10