Jquery 悬停与 setTimeout
我有一个水平导航菜单,我想在用户的鼠标停留在按钮上 1 秒钟时显示工具提示。或者换句话说,我希望提示出现时有一个延迟。当用户将鼠标移开时,工具提示应立即消失。 Stumbleupon 的工具栏是我希望其发挥作用的一个示例。
javascript:
$("a.btn").hover(
function() {
var tip = $(this).parent().children(".tip-wrapper");
setTimeout(function{
tip.show();
}, 1000)
},
function {
var tip = $(this).parent().children(".tip-wrapper");
tip.hide();
}
);
html:
<th title="">
<a href="#" class="btn">
<span class="person">Firstname Lastname</span>
</a>
<div class="tip-wrapper">
<div class="tip-border">
<div class="tip">
tool tips go here
</div>
</div>
</div>
</th>
我看过很多教程,但不明白为什么我的教程不起作用。
I have a horizontal navigation menu and I want to show a tooltip when the user's mouse rests on the button for 1 second. Or in other words, I want there to be a delay for when the tip appears. The tooltip should disappear immediately when the user moves the mouse away. Stumbleupon's toolbar is an example of how I want this to function.
javascript:
$("a.btn").hover(
function() {
var tip = $(this).parent().children(".tip-wrapper");
setTimeout(function{
tip.show();
}, 1000)
},
function {
var tip = $(this).parent().children(".tip-wrapper");
tip.hide();
}
);
html:
<th title="">
<a href="#" class="btn">
<span class="person">Firstname Lastname</span>
</a>
<div class="tip-wrapper">
<div class="tip-border">
<div class="tip">
tool tips go here
</div>
</div>
</div>
</th>
I've looked at many tutorials and can't figure out why mine doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果鼠标在超时发生之前移开,您将立即
hide()
它,然后在超时运行后show()
它。相反,您应该使用
hoverIntent
插件,该插件为你做这个。If the mouse moves away before the timeout happens, you'll
hide()
it immediately, thenshow()
it after the timeout runs.Instead, you should use the
hoverIntent
plugin, which does this for you.你有一些语法错误:
function {
应该是function() {
(同样适用于setTimeout(function{
=>;setTimeout (函数(){
);我建议使用一个引用超时函数的变量。这样,如果用户未将元素悬停至少一秒,您可以停止工具提示的显示。 :
另外,您应该在开始时隐藏工具提示。 是一个实时工作示例。
只要您已经在项目中使用 jquery,我建议您利用它并使用它的动画功能。这样,您的代码就变成:
ps:使用
.siblings()
而不是.parent().children()
you have some syntax errors :
function {
should befunction() {
(same goes forsetTimeout(function{
=>setTimeout(function(){
);I suggest using a variable that refers to your timeout function. That way, you can stop the appearance of the tooltip if the user does not hover the element at least one second. :
Also, you should hide your tooltips at the beginning. is a live working sample.
As long as you're already using jquery in your project, I suggest you take advantage of it and use it's animation function instead. This way, your code becomes :
p.s.: use
.siblings()
instead of.parent().children()
首先,您的脚本中存在一些语法错误(如 gion_13 所示)。
其次,为了确保如果用户在超时完成之前将鼠标移开,工具提示不会错误显示,您需要使用一个变量来存储超时,以便您可以在
hover
中清除它 的handlerOut
参数。工作小提琴。
First, there were a few syntax errors in your script (as gion_13) pointed out.
Second, to make sure the tooltip doesn't show erroneously if the user moves the mouse away before the timeout is complete, you'll want to use a variable to store your timeout so that you can clear it in
hover
'shandlerOut
parameter.Working Fiddle.