我的网站顶部有一个关闭按钮的警报。这是用于关闭按钮的jQuery:
/* CLOSE ALERT BAR WITH BUTTON */
$('a.ctp-alert').click(function () {
$("div.ctp-alert").slideUp();
});
它本身运行良好,但我也试图添加另一个脚本,这将在5000毫秒后将这个DIV向上滑动。这是我正在使用的jQuery:
/* FADE OUT ALERT BAR AUTOMATICALLY */
$("div.ctp-alert").delay(5000).slideUp(500);
这两个脚本都是自己工作的,但是当同时使用这两个脚本时,关闭按钮会停止工作。看起来这两个人互相干扰。
关于我的任何想法都可以让它们一起工作?提前谢谢。
答案 0 :(得分:1)
由于您无法使用.delay()
取消定时器,因此您无法在此使用$.delay
。
而是用setTimeout写一下(有点难看)
var timers = {};
$('a.ctp-alert').click(function () {
$("div.ctp-alert").slideUp();
clearTimeout(timers.hider);
timers.hider = null;
});
timers.hider = setTimeout(function() {
$("div.ctp-alert").slideUp(500);
timers.hider = null;
}, 5000);