简体   繁体   English

jQuery-一段时间后链接仅“起作用”

[英]jQuery - link working *only* after some time

I have a link: 我有一个链接:

<a href="#" id="link">Here's my link</a>

This is not a normal clickable link, it's coded in jQuery like this: 这不是一个普通的可点击链接,它在jQuery中编码如下:

 $("#link").hover(function(e) { 
    e.preventDefault();   
    $("#tv").stop().animate({marginLeft: "50px"});
    $("#tv img)").animate({opacity: 1});
})  

So after hovering unclickable link there's change of #tv's margin and opacity. 因此,将鼠标悬停在不可点击的链接之后,就会改变#tv的边距和不透明度。

Is there any way of making this work only after the user hovers the link area with pointer for more than two seconds? 有没有让用户将鼠标悬停与指针超过两秒钟,链接区域仅这项工作的方法吗?

Because now everything happens in real time. 因为现在所有事情都是实时发生的。

I know there's delay() , but it doesn't work because it just delays the animation and in this case I don't want any action if the pointer is over for less than two seconds. 我知道这里有delay() ,但是它不起作用,因为它只是延迟了动画,在这种情况下,如果指针停留不到两秒钟,我就不希望任何动作。

Possible without a loop? 可能没有循环?

你所追求的是hoverIntent

var animateTimeout;

$("#link").hover(function() {
  if (animateTimeout != null) { 
    clearTimeout(animateTimeout); 
  }
  animateTimeout = setTimeout(animate, 2000);
}, function() {
  clearTimeout(animateTimeout);
});

function animate() {
  //do animation
}

You just need a setTimeout() to delay the code, along with a clearTimeout() to clear it if the user leaves the link within 2 seconds. 您只需要一个setTimeout()来延迟代码,并且如果用户在2秒内离开链接,则需要clearTimeout()来清除它。

Example: http://jsfiddle.net/mNWEq/2/ 示例: http //jsfiddle.net/mNWEq/2/

$("#link").hover(function(e) {
    e.preventDefault();
    $.data(this).timeout = setTimeout(function() {
        $("#tv").stop().animate({marginLeft: "50px"});
        $("#tv img)").animate({opacity: 1});
    }, 2000);
}, function(e) {
    clearTimeout($.data(this,'timeout'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM