2014-01-11 81 views
1

我有通知出现在屏幕的左上角。他们在5秒钟的延迟时间内被移除。如果用户徘徊在一个以上,它应该被删除。防止元素被移除,如果鼠标上移

这里是我的jQuery代码

$alert.on('mouseover', function() { 
    clearTimeout(growl_remove); 
}); 
growl_remove = setTimeout(function() { 
    return $alert.alert("close"); 
}, 5000); 

下面是HTML

<div class="growl col-xs-12 col-sm-12 col-md-3 alert alert-danger" style="position: fixed; margin: 0px; z-index: 9999; top: 26.846393585205078px; right: 20px;"> 
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> 
    <span class="glyphicon glyphicon-ban-circle"></span>&nbsp;Invalid Username or Passowrd 
</div> 

现在,这似乎工作,但它停止从被删除不是一个用鼠标的最后警告超过它。

这里是一个的jsfiddle:http://jsfiddle.net/KU5gy/

+1

听起来像一个封闭的问题。请分享HTML,也许把一个jsFiddle放在一起。 – BenM

回答

1

能帮助?

var AlertController = function(){ 
    var _timeoutId = null; 
    var _resetTimeout = function() { 
    clearTimeour(_timeoutId); 
    _timeoutId = setTimeout(function() { $alert.alert("close"); }, 5000); 
    } 
    var _init(){ 
     $alert.on('mouseover', resetTimeout); 
    } 
    return { 
     showNotification: function(){ 
     $alert.alert("show"); 
     _init(); 
     } 
    } 
} 

,并调用它像这样:

//call it when your notification is to be shown 
function onNotification(){ 
    new AlertController().showNotification(); 
} 
+0

我不知道为什么,但我得到'未捕获的SyntaxError:意外的标记(''上线var'init(){' –

+0

@ RobertE.McIntosh:我没有测试代码,但提供错误的行可能应该是'var _init = function(){$ alert.on(....)}' – surfmuggle

0

这似乎解决了我的问题

var bootstrap_growl_remove = []; 

$alert.on('mouseover', function() { 
    clearTimeout(bootstrap_growl_remove[$alert.index()]); 
}).on('mouseleave', function() { 
    bootstrap_growl_remove[$alert.index()] = setTimeout(function() { 
    return $alert.alert("close"); 
    }, options.delay); 
}); 

bootstrap_growl_remove[$alert.index()] = setTimeout(function() { 
    return $alert.alert("close"); 
}, options.delay); 
相关问题