jQuery悬停()不适用于绝对定位的元素和动画
我有一些看起来像这样的 html:
<a href="#" class="move"><span class="text">add</span><span class="icon-arrow"></span></a>
我在锚标记上注册了一个 jquery 事件:
$('a.move').hover(
function (event) {
$(this).children('span.text').toggle();
$(this).animate({right: '5px'}, 'fast');
},
function (event) {
$(this).children('span.text').toggle();
$(this).animate({right: '0px'}, 'fast');
}
);
当我将鼠标悬停在锚标记上时,它会显示 span.text 并将锚点向右移动 5px。
现在,由于我不想陷入的复杂性,我必须设置position:relative; 并绝对定位图标和文本,使图标出现在左侧,文本出现在右侧。
问题:
当我将鼠标悬停在锚标记上时,图标向右移动,并且鼠标最终位于文本(出现的)上方。 不幸的是,如果我将鼠标从图标移动到文本,并且动画开始疯狂循环,则会调用“out”函数。 我不明白是什么导致“out”事件触发,因为鼠标永远不会离开锚标记。
谢谢!
I have some html that looks like this:
<a href="#" class="move"><span class="text">add</span><span class="icon-arrow"></span></a>
And I have a jquery event registered on the anchor tag:
$('a.move').hover(
function (event) {
$(this).children('span.text').toggle();
$(this).animate({right: '5px'}, 'fast');
},
function (event) {
$(this).children('span.text').toggle();
$(this).animate({right: '0px'}, 'fast');
}
);
When I mouse over the anchor tag, it displays the span.text and moves the anchor 5px to the right.
Now, due to complications that I don't feel like getting into, I have to set position: relative; on the container and absolutely position the icon and the text so that the icon appears on the left and the text on the right.
THE PROBLEM:
When I mouse over the anchor tag, the icon moves right, and the mouse ends up over top of the text (which appears). Unfortunately, the 'out' function gets called if I move my mouse from the icon to the text and the animation starts looping like crazy. I don't understand what's causing the "out" event to fire, as the mouse is never leaving the anchor tag.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用“mouseenter”和“mouseleave”事件代替悬停,当子元素妨碍时,这些事件不会触发:
Instead of hover you can use the "mouseenter" and "mouseleave" events, which do not fire when child elements get in the way: