2011-06-16 115 views
0

我有一个选项卡式的脚本,它具有切换悬停标签。 因为链接靠得很近,所以我希望在悬停功能被触发前有一个延迟,所以当用户“接触”其余的链接时,内容切换的速度并不快。jQuery setTimeout悬停链接?

也许用setTmeout?这是我的剧本,我该怎么做?

$(".HotelPanel_content").hide(); //Hide all content 
$("ul.HotelPanelNav li:first").addClass("active").show(); //Activate first tab 
$(".HotelPanel_content:first").show(); //Show first tab content 


$("ul.HotelPanelNav li").hover(function() { 
    $("ul.HotelPanelNav li").removeClass("active"); //Remove any "active" class 
    $(this).addClass("active"); //Add "active" class to selected tab 
    $(".HotelPanel_content").hide(); //Hide all tab content 
    var activeTab = $(this).find("a").attr("id"); //Find the rel attribute value to identify the active tab + content 
    $(activeTab).slideDown("slow"); //Fade in the active content 
    return false; 
}); 

}); 

回答

1

试试这个:

var thisCache; 
var hoverTimeout; 
$("ul.HotelPanelNav li").mouseenter(function() { 
    thisCache = $(this); 
    hoverTimeout = setTimeout(function(){ 
     $("ul.HotelPanelNav li").removeClass("active"); //Remove any "active" class 
     thisCache.addClass("active"); //Add "active" class to selected tab 
     $(".HotelPanel_content").hide(); //Hide all tab content 
     var activeTab = thisCache.find("a").attr("id"); //Find the rel attribute value to identify the active tab + content 
     $(activeTab).slideDown("slow"); //Fade in the active content 
     },300) 
    }); 
$("ul.HotelPanelNav li").mouseleave(function(){ 
    clearTimeout(hoverTimeout) 
}) 
+0

是Litek, 您的解决方案对我来说最合适。 这就是它 Thx为您的想法,你们所有人。 – Grashopper 2011-06-16 16:03:49

0

您可以将.delay()添加到任何jquery动画方法。这个方法需要一个毫秒的时间,所以你可以在动画上做到这一点。

$(activeTab).delay(5000).slideDown("slow"); 

我相信你也可能会推迟功能

$("ul.HotelPanelNav li").delay(5000).hover(function() { 

编辑

您也可以使用.timeout功能

window.setTimeout(function() { 
    callYourFunction(); 
}, 1000); 

编辑2

这对我来说做工精细

<script type="text/javascript" src="jquery-1.4.2.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#somelink").hover(function(){ 
     window.setTimeout(function() { 
     //put your code here 
     }, 1000); 
    }); 
}); 

</script> 
</head> 
<body> 
<div id="flash"> 
</div> 
<div id="somelink"> 
    Hello this is a div 
</div> 
+0

你好,谢谢你,但我真正需要的是在链接悬停后延迟功能(切换到下一个内容) 因为我的问题是: 1.链接如下其他 链接一个 链路B 路段C 2.内容面板低于已embeeded链接链路(一个单独的导航) 问题: 选择看(例如)链接,用户必须导航到内容如下所述,但是上一路下来,他徘徊在链接b + c 因此,我需要延迟整个悬停行动 – Grashopper 2011-06-16 08:43:38

+0

在这种情况下,您将需要使用setTimeout函数。看到这篇文章https://developer.mozilla.org/en/DOM/window.setTimeout – 2011-06-16 08:55:21

+0

嗯,仍然无法使它的工作。你可以在我的代码上面显示它吗?我需要当鼠标悬停在链接上,延迟动作“显示下一个内容” – Grashopper 2011-06-16 11:08:45

0

也许你可以这样来做

$("ul.HotelPanelNav li").hover(function() { 
    settTimeout('yourfunction($this);', time) 
}); 

function yourfunction(element){ 
    $("ul.HotelPanelNav li").removeClass("active"); //Remove any "active" class 
    $(this).addClass("active"); //Add "active" class to selected tab 
    $(".HotelPanel_content").hide(); //Hide all tab content 
    var activeTab = $(this).find("a").attr("id"); //Find the rel attribute value to identify the active tab + content 
    $(activeTab).slideDown("slow"); //Fade in the active content 
    return false; 
}