将鼠标悬停在任何段落上,附加带有小消息的 div,将鼠标悬停,它会淡出,这是对的吗?

发布于 2024-10-17 22:13:20 字数 601 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-10-24 22:13:20

只需使用类而不是 id:

$('p').hover(function() {

    $(this).append('<span class="both">BOOKMARK THIS</span>')
        $('.both').animate({opacity: 1.0}) 

}, function(){
        $('.both').fadeOut(600, function(){
            $(this).remove()
        })
});

http://jsfiddle.net/yzXxH/

Just use class instead of id:

$('p').hover(function() {

    $(this).append('<span class="both">BOOKMARK THIS</span>')
        $('.both').animate({opacity: 1.0}) 

}, function(){
        $('.both').fadeOut(600, function(){
            $(this).remove()
        })
});

http://jsfiddle.net/yzXxH/

半城柳色半声笛 2024-10-24 22:13:20

我可能会稍微改变一下:

$('p').hover(function() {

    $('<span class="both">BOOKMARK THIS</span>')
        .appendTo(this)
        .animate({opacity: 1.0}) 

}, function(){

    var both = $(this).find('span.both');
    both.fadeOut(600, function(){
        both.remove()
    });

});

我使用 class 而不是 id 的原因是,当您离开一个段落并进入下一个段落时,您将暂时有两个 spans ——一个在旧段落中淡出,一个添加到新段落中。两个元素具有相同的 id 是无效的并且充满危险。

如果我回到同一段,我可能也会提前取消动画:

$('p').hover(function() {

    $(this).find('span.both').stop().remove(); // Stop and remove it if it's there
    $('<span class="both">BOOKMARK THIS</span>')
        .appendTo(this)
        .animate({opacity: 1.0}) 

}, function(){

    var both = $(this).find('span.both');
    both.fadeOut(600, function(){
        both.remove()
    });

});

I'd probably change it a little:

$('p').hover(function() {

    $('<span class="both">BOOKMARK THIS</span>')
        .appendTo(this)
        .animate({opacity: 1.0}) 

}, function(){

    var both = $(this).find('span.both');
    both.fadeOut(600, function(){
        both.remove()
    });

});

The reason I used a class instead of an id is that when you leave one paragraph and enter the next, you're going to temporarily have two of these spans — the one that's fading out on the old paragraph and the one added to the new one. Having two elements with the same id is invalid and fraught with peril.

I'd probably also cancel the animation early if I come back to the same paragraph:

$('p').hover(function() {

    $(this).find('span.both').stop().remove(); // Stop and remove it if it's there
    $('<span class="both">BOOKMARK THIS</span>')
        .appendTo(this)
        .animate({opacity: 1.0}) 

}, function(){

    var both = $(this).find('span.both');
    both.fadeOut(600, function(){
        both.remove()
    });

});
冬天旳寂寞 2024-10-24 22:13:20

在我看来,你不需要不断地添加和删除 dom。只需将跨度默认设置为 style="display:none;" ,那么

$('p').hover(function() {
        $(this).find('#both').animate({opacity: 1.0}) 

}, function(){
        $(this).find('#both').fadeOut(600)
});

这将无法完全正常工作,您可能必须扰乱默认样式中的不透明度才能获得您想要的效果。不过,不需要操纵 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;", then

$('p').hover(function() {
        $(this).find('#both').animate({opacity: 1.0}) 

}, function(){
        $(this).find('#both').fadeOut(600)
});

That 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文