jquery鼠标悬停效果bug,mouseover事件总是在mouseout时触发几次
我有一个简单的画廊网格,其中有一个嵌套的跨度,用于显示标题,我想在鼠标悬停时向上滑动,并在鼠标移开时隐藏。
一切正常,除非鼠标向下移动到标题所在的位置和/或从图块底部悬停在图块之外,然后悬停效果会不受控制地重复几次。
起初我认为这可能是因为跨度包含在作为悬停触发器的锚点内,但将其移出也不起作用。
有什么想法吗?
演示在这里: http://www.winterealm.com/gallery/
标记:
<div class="gallery_container">
<ul>
<li><a href=""><img src="assets/img/artistisch.jpg" alt="aaa"/><span class="title">Category A</span></a></li>
<li><a href=""><img src="assets/img/attraktiv.jpg" alt="bbb"/><span class="title">Category B</span></a></li>
<li><a href=""><img src="assets/img/historisch.jpg" alt="ccc"/><span class="title">Category C</span></a></li>
<li><a href=""><img src="assets/img/popart.jpg" alt="ddd"/><span class="title">Category D</span></a></li>
<li><a href=""><img src="assets/img/portrait.jpg" alt="eee"/><span class="title">Category E</span></a></li>
<li><a href=""><img src="assets/img/sketch.jpg" alt="fff"/><span class="title">Category F</span></a></li>
</ul>
</div>
这是 jquery
$(document).ready(function(){
$('.gallery_container a').mouseover(function(){
$(this).children('.title').animate({
opacity: 100,
bottom: 0
},200);
});
$('.gallery_container a').mouseout(function(){
$(this).children('.title').animate({
opacity: 0,
bottom: -30
},200);
});
});
I have a simple gallery grid with a nested span that shows the title, which I want to slide up on mouse over, and hide on mouse out.
Everything works fine, except whenever the mouse moves down to where the title is and/or hovers out of the tile from the bottom of the tile, then the hover effect repeats a few times uncontrollably.
At first I thought it might be because the span is contained within the anchor which is the hover trigger, but moving it out didn't work either.
Any ideas ?
The demo is here : http://www.winterealm.com/gallery/
Markup:
<div class="gallery_container">
<ul>
<li><a href=""><img src="assets/img/artistisch.jpg" alt="aaa"/><span class="title">Category A</span></a></li>
<li><a href=""><img src="assets/img/attraktiv.jpg" alt="bbb"/><span class="title">Category B</span></a></li>
<li><a href=""><img src="assets/img/historisch.jpg" alt="ccc"/><span class="title">Category C</span></a></li>
<li><a href=""><img src="assets/img/popart.jpg" alt="ddd"/><span class="title">Category D</span></a></li>
<li><a href=""><img src="assets/img/portrait.jpg" alt="eee"/><span class="title">Category E</span></a></li>
<li><a href=""><img src="assets/img/sketch.jpg" alt="fff"/><span class="title">Category F</span></a></li>
</ul>
</div>
Here's the jquery
$(document).ready(function(){
$('.gallery_container a').mouseover(function(){
$(this).children('.title').animate({
opacity: 100,
bottom: 0
},200);
});
$('.gallery_container a').mouseout(function(){
$(this).children('.title').animate({
opacity: 0,
bottom: -30
},200);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的问题是,每次鼠标移到元素或子元素上时,都会触发 mouseover。尝试改用 mouseenter 和 mouseleave 事件。
The problem here is that mouseover fires every time the mouse moves over the element or child elements. Try using the mouseenter and mouseleave events instead.
试试这个。
您可以在这里观看现场演示。 - http://jsfiddle.net/8Hd7s/
Try this.
You can see a live demo here. - http://jsfiddle.net/8Hd7s/
因此,您可能想要实现一个非常简单的锁定机制,如下所示:
它不是严格的竞争条件明智的,但它不需要如此。
So you may want to implement a really simple locking mechanism, as in:
it's not airtight race-condition-wise, but it doesn't need to be.