将鼠标悬停在任何段落上,附加带有小消息的 div,将鼠标悬停,它会淡出,这是对的吗?
http://jsfiddle.net/nicktheandroid/3AraQ/
当 P 悬停在上方时,#both 是附加到该段落并居中。当我将鼠标悬停在新段落上时,它会在第一个 P 上淡出,并在现在悬停的 P 上淡入。这是最好的方法吗?稍后我将使用它来允许人们单击书签图像,然后当他们将鼠标悬停在 P 上时,它将执行我下面的代码所做的操作,然后当他们单击该 P 时,它将为该段落创建一个书签,但我真的只需要以下代码的帮助。谢谢!
$('p').hover(function() {
$(this).append('<span id="both">BOOKMARK THIS</span>')
$('#both').animate({opacity: 1.0})
}, function(){
$('#both').fadeOut(600, function(){
$(this).remove()
})
});
它工作不顺利,它只是不对......
http://jsfiddle.net/nicktheandroid/3AraQ/
When a P is hovered over, #both is appended to that paragraph and centered. When i hover off and then onto a new paragraph, it fades out on the first P and fades in on the now hovered P. is this the best way to do it? Later on i'll use this to allow people to click a bookmark image, then when they hover a P it will do what my code below does, then when they click on that P it will create a bookmark to that paragraph, but I really just need help with the code below. THANKS!
$('p').hover(function() {
$(this).append('<span id="both">BOOKMARK THIS</span>')
$('#both').animate({opacity: 1.0})
}, function(){
$('#both').fadeOut(600, function(){
$(this).remove()
})
});
it's not working smoothly, it's just not right....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用类而不是 id:
http://jsfiddle.net/yzXxH/
Just use class instead of id:
http://jsfiddle.net/yzXxH/
我可能会稍微改变一下:
我使用
class
而不是id
的原因是,当您离开一个段落并进入下一个段落时,您将暂时有两个span
s ——一个在旧段落中淡出,一个添加到新段落中。两个元素具有相同的id
是无效的并且充满危险。如果我回到同一段,我可能也会提前取消动画:
I'd probably change it a little:
The reason I used a
class
instead of anid
is that when you leave one paragraph and enter the next, you're going to temporarily have two of thesespan
s — the one that's fading out on the old paragraph and the one added to the new one. Having two elements with the sameid
is invalid and fraught with peril.I'd probably also cancel the animation early if I come back to the same paragraph:
在我看来,你不需要不断地添加和删除 dom。只需将跨度默认设置为
style="display:none;"
,那么这将无法完全正常工作,您可能必须扰乱默认样式中的不透明度才能获得您想要的效果。不过,不需要操纵 dom。
It doesn't look to me like you have a need to keep adidng and removing to the dom. Just put the span in by default with
style="display:none;"
, thenThat won't work exactly, you may have to mess with opacity in the default style to get what you want. Still, no need to manipulate the dom.