简体   繁体   English

jQuery fadeOut()仅可使用一次

[英]jQuery fadeOut() Only Works Once

I am using jQuery to fade out a "notification" bubble I built. 我正在使用jQuery淡出我构建的“通知”气泡。 The first time the function is called it fades out just fine, but the second time the "notification" is appended to the body, but just sits there and doesn't fade out. 第一次调用该函数时,它会逐渐消失,但是第二次将“通知”附加到主体上,但是只是坐在那里而不会消失。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is the Javascript that is being called. 这是被调用的Javascript。

    if (pointsCalculated = 1) {
    $('body').append('<div id="progress">' + pointsCalculated + ' point added to current points...</div>');
}
else {
    $('body').append('<div id="progress">' + pointsCalculated + ' points added to current points...</div>');
}

//Reset calculator after adding to tracker
calcReset();

    $("#progress").fadeOut(2000);

Try removing the element after the fadeOut: 尝试在fadeOut之后删除元素:

$("#progress").fadeOut(2000, function() { $(this).remove(); });

More info: 更多信息:

.remove() 。去掉()
.fadeOut() 。淡出()

Javascript can´t find more then one element with an ID, and its already faded out when you want to run it again. Javascript找不到一个ID多个的元素,而当您想再次运行它时,它已经淡出。

you can change the id to an class and then find all .progress that is visible and not animated to start the fadout on that item, and when its done you can remove it so you dont have to many .progress 您可以将id更改为一个类,然后找到所有可见且未设置动画的.progress来启动该项目上的淡入淡出,完成该操作后,您可以将其删除,因此不必进行太多.progress

$('body').append('<div class="progress">' + pointsCalculated + ' ' + (pointsCalculated === 1 ? 'point' : 'points') + ' added to current points...</div>');

//Reset calculator after adding to tracker
calcReset();

$(".progress:visible:not(:animated)").fadeOut(2000, function() { $(this).remove(); });

remember in javascript if you want to look if a variable is a value you will need to use at least two "=" else you will set the value to that variable. 记住在javascript中,如果要查看变量是否为值,则需要至少使用两个“ =”,否则将值设置为该变量。

每次添加后,您都需要将fadeOut事件重新绑定到#progress元素。

You must recreating your appended elements fadeOut binding when you add the element back to the DOM. 将元素添加回DOM时,必须重新创建附加的元素fadeOut绑定。 You can create this binding immediately after the append and this should now work. 您可以在追加之后立即创建此绑定,并且现在应该可以使用。

You code doesn't show you REMOVING "progress", so you're likely adding a new one each time. 您的代码并未显示您正在删除“进度”,因此您可能每次都添加一个新代码。 Since IDs much be unique, your code finds two and fails. 由于ID非常唯一,因此您的代码将找到两个并失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM