显示通知直到超时除非悬停(jQuery)
Show notification until timeout unless hovered (jQuery)
我正试图建立一个通知系统,我从UI/UX开始,我开始建立一个概念证明,但我正在努力调试我的超时。任何帮助都会很棒的。
JavaScript: //Remove notification after 5 seconds
var timer;
new function(){
timer = setTimeout(function(){
removeNotification($notification);
}, 5000);
}
//If any notification is hovered then start the timer again
jQuery("#notificationList").hover(function (){
clearTimeout(timer);
});
new Function() {..}
->以这种方式声明函数会导致函数不被编译,并且可能比其他声明函数的方式慢。
所以,有一个生活,像下面。
//Remove notification after 5 seconds
var timer = false;
(function(){
timer = setTimeout(function(){
removeNotification($notification);
}, 5000);
}());
和变化:
//If any notification is hovered then start the timer again
jQuery("#notificationList").hover(function (){
clearTimeout(timer);
});
:
//If any notification is hovered then start the timer again
jQuery("#notificationList").hover(function () {
removeNotification($notification);
timer && clearTimeout(timer);
});
因为clearTimeout
不会运行,如果计时器最初没有设置。希望对你有帮助。
你的全部代码(因为我不能分叉你的笔):
//Notification markup
var notificationMarkup = '<li class="notification"><a href="#"><span class="image-wrapper"><img src="http://www.guestsofdave.com/images/scarlett%20johansson.jpg"/></span><span class="message-wrapper"><span class="message"><span class="info"><strong>Scarlett</strong> has replied to your <strong>forum post</strong>.</span><span class="meta"><i class="application forum"></i> <span class="notification-date">1 Hour Ago</span></span></span></span></a><span class="close-db-notification"></span></li>';
//CSS animation end hook
var animationEnd = "webkitAnimationEnd oanimationend msAnimationEnd animationend";
//Remove notification function
function removeNotification($notification) {
$notification.addClass("animated zoomOut");
$notification.animate({
height: 0
}, 150, function() {
$notification.remove();
});
}
//New notification
jQuery(".new-notification").click(function() {
//Gets the last notification added
var $notification = jQuery("#notificationList .notification").last();
//Append the notification markup into the list
jQuery("#notificationList").append(notificationMarkup);
//Animate the notifications height from 0 to 90px and then animate it in
$notification.animate({
height: 90
}, 150, function() {
jQuery(this).css('visibility', 'visible');
jQuery(this).addClass("animated zoomIn");
});
//Remove animation classes when animation ends
$notification.on(animationEnd, function() {
jQuery(this).removeClass("animated zoomIn")
});
//Remove notification after 5 seconds
var timer = false;
(function() {
timer = setTimeout(function() {
removeNotification($notification);
}, 5000);
}());
//If any notification is hovered then start the timer again
jQuery("#notificationList").hover(function() {
removeNotification($notification);
timer && clearTimeout(timer);
});
});
//Remove notification on click
jQuery(document).on("click", ".close-db-notification", function() {
var $notification = jQuery(this).closest(".notification");
removeNotification($notification);
});
相关文章:
- css(或jQuery)悬停时淡入淡出
- jquery快速悬停问题
- jQuery:当屏幕大小改变时,如何更改默认图像和悬停图像
- jQuery悬停在没有鼠标悬停的情况下启动
- 如何使jQuery中的悬停函数单独应用于数据库映像
- 为什么jquery悬停在jsfiddle中可以工作,而在我的html布局中却不能
- jquery悬停和while循环
- 在悬停Jquery/cs3时通过翻转动画更改文本
- 悬停在悬停jquery/javascript中
- 更改元素父元素的背景颜色时停止在
上闪烁鼠标悬停?jQuery/JS. - 在悬停jQuery上保持下拉列表打开
- 使用.on()关闭悬停jquery
- 如何设置属性='数据fancybox组'在滑动悬停jquery
- Wordpress不接受.悬停(jquery)
- 我如何添加文本到悬停jQuery元素
- Img SRC路径给第一个子只在悬停jquery
- 我如何跟踪一个:悬停jquery源代码
- 显示通知直到超时除非悬停(jQuery)
- 改变悬停jquery的一部分Href
- 改变背景位置的悬停- jquery