Twitter Bootstrap 提醒可以淡入淡出吗?
当我第一次在 Bootstrap 中看到警报时,我认为它们的行为就像模态窗口一样,下拉或淡入,然后在关闭时淡出。但似乎它们总是可见的。我想我可以让它们位于我的应用程序上方的一层并管理显示它们,但我想知道该功能是否内置?
谢谢!
编辑,我到目前为止所拥有的:
<div id="saveAlert" class="alert-message success fade in" data-alert="alert" style="top:0">
<a class="close" href="#">×</a>
<p><strong>Well done!</strong> You successfully read this alert message.</p>
</div>
When I first saw the alerts in Bootstrap I thought they would behave like the modal window does, dropping down or fading in, and then fading out when closed. But it seems like they are always visible. I guess I could have them sit in a layer above my app and manage showing them but I was wondering if the functionality was built in?
thanks!
Edit, what I have so far:
<div id="saveAlert" class="alert-message success fade in" data-alert="alert" style="top:0">
<a class="close" href="#">×</a>
<p><strong>Well done!</strong> You successfully read this alert message.</p>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我强烈不同意前面提到的大多数答案。
简短回答:
有关 3 秒后淡入警报的示例,请参阅此 jsfiddle http://jsfiddle.net/QAz2U/3/
长答案:
虽然引导程序本身并不支持警报淡入淡出,但这里的大多数答案都使用jQuery 淡入淡出函数,它使用 JavaScript 来对元素进行动画处理(淡入淡出)。这样做的一大优点是跨浏览器兼容性。缺点是性能(另请参阅:jQuery 调用 CSS3 淡入淡出动画?) 。
Bootstrap 使用 CSS3 过渡,其性能更好。这对于移动设备很重要:
引导 CSS 淡化警报:
为什么我认为这种性能如此重要?使用旧浏览器和硬件的人可能会因 jQuery.fade() 而出现不稳定的过渡。对于带有现代浏览器的旧硬件来说也是如此。使用 CSS3 过渡,即使使用较旧的硬件,使用现代浏览器的人也会获得流畅的动画,而使用不支持 CSS 过渡的旧浏览器的人将立即看到元素弹出,我认为这比断断续续的动画有更好的用户体验。
我来这里寻找与上面相同的答案:淡入引导警报。在深入研究 Bootstrap 的代码和 CSS 后,答案相当简单。不要将“in”类别添加到您的警报中。当您想要淡入警报时,可以使用 jQuery 添加此内容。
HTML(注意没有 in 类!)
Javascript:
调用上面的函数会添加“in”类,并使用以下命令淡入警报CSS3 转换:-)
另请参阅此 jsfiddle 有关使用超时的示例(感谢 John Lehmann!): http://jsfiddle.net/QAz2U/3/
I strongly disagree with most answers previously mentioned.
Short answer:
See this jsfiddle for an example that fades in alert after 3 seconds http://jsfiddle.net/QAz2U/3/
Long answer:
Although it is true bootstrap doesn't natively support fading in alerts, most answers here use the jQuery fade function, which uses JavaScript to animate (fade) the element. The big advantage of this is cross browser compatibility. The downside is performance (see also: jQuery to call CSS3 fade animation?).
Bootstrap uses CSS3 transitions, which have way better performance. Which is important for mobile devices:
Bootstraps CSS to fade the alert:
Why do I think this performance is so important? People using old browsers and hardware will potentially get a choppy transitions with jQuery.fade(). The same goes for old hardware with modern browsers. Using CSS3 transitions people using modern browsers will get a smooth animation even with older hardware, and people using older browsers that don't support CSS transitions will just instantly see the element pop in, which I think is a better user experience than choppy animations.
I came here looking for the same answer as the above: to fade in a bootstrap alert. After some digging in the code and CSS of Bootstrap the answer is rather straightforward. Don't add the "in" class to your alert. And add this using jQuery when you want to fade in your alert.
HTML (notice there is NO in class!)
Javascript:
Calling the function above function adds the "in" class and fades in the alert using CSS3 transitions :-)
Also see this jsfiddle for an example using a timeout (thanks John Lehmann!): http://jsfiddle.net/QAz2U/3/
我使用的东西是这样的:
在你的模板中,有一个警报区域
,然后是一个用于显示警报的 jQuery 函数
The thing I use is this:
In your template an alert area
Then an jQuery function for showing an alert
您可以使用 jquery 淡入框。使用'hide'类内置的bootstraps有效地在div元素上设置display:none:
然后使用jquery中的fadeIn函数,如下所示:
还有为fadeIn函数指定持续时间,例如:
$("#saveAlert").fadeIn(400);
有关使用 fadeIn 函数的完整详细信息可以在官方 jQuery 文档站点上找到:http://api.jquery.com/fadeIn/
顺便说一句,如果您不使用 jquery,您可以将“隐藏”类添加到您自己的 CSS 文件中,或者将其添加到您的 div 中:
然后,您的 div 基本上将默认设置为隐藏,并且然后 jQuery 将执行 fadeIn 操作,强制显示 div。
You can fade-in a box using jquery. Use bootstraps built in 'hide' class to effectively set display:none on the div element:
and then use the fadeIn function in jquery, like so:
There are also specify a duration for the fadeIn function, e.g:
$("#saveAlert").fadeIn(400);
Full details on using the fadeIn function can be found on the official jQuery documentation site: http://api.jquery.com/fadeIn/
Just a sidenote as well, if you arent using jquery, you can either add the 'hide' class to your own CSS file, or just add this to your div:
Your div will then basically be set to hidden as default, and then jQuery will perform the fadeIn action, forcing the div to be displayed.
对于 2.3 及更高版本,只需添加:
$(".alert").fadeOut(3000 );
bootstrap:
适用于所有浏览器。
For 2.3 and above, just add:
$(".alert").fadeOut(3000 );
bootstrap:
Works in all browsers.
将
hide
类添加到alert-message
。然后在 jQuery 脚本导入后添加以下代码:如果要处理多条消息,此代码将按升序隐藏它们:
Add
hide
class toalert-message
. Then put the following code after your jQuery script import:If you want handle multiple messages, this code will hide them in ascending order:
目前的答案都不适合我。我正在使用 Bootstrap 3。
我喜欢 Rob Vermeer 所做的事情并从他的回复开始。
对于淡入然后淡出效果,我只是使用编写了以下函数并使用 jQuery:
Html 在我的页面上将警报添加到:
Javascript 函数以显示和关闭警报。
然后,要显示和关闭警报,只需调用如下函数:
作为旁注,我将 div.alert-messages 的样式固定在顶部:
None of the current answers worked for me. I'm using Bootstrap 3.
I liked what Rob Vermeer was doing and started from his response.
For a fade in and then fade out effect, I just used wrote the following function and used jQuery:
Html on my page to add the alert(s) to:
Javascript function to show and dismiss the alert.
Then, to show and dismiss the alert, just call the function like this:
As a side note, I styled the div.alert-messages fixed on top:
我用这种方法在 3 秒后关闭淡出警报。希望它会有用。
I got this way to close fading my Alert after 3 seconds. Hope it will be useful.
我不同意 Bootstrap 使用
淡入
的方式(如他们的文档中所示 - http://v4-alpha.getbootstrap.com/components/alerts/),我的建议是避免类名fade
和in
,并避免一般情况下的这种模式(目前在该问题的评分最高的答案中可以看到)。(1) 语义是错误的——转换是暂时的,但类名仍然存在。那么为什么我们要把我们的类命名为
fade
和fade in
呢?它应该是faded
和faded-in
,这样当开发人员阅读标记时,就可以清楚地看到这些元素是faded
或faded -在
。 Bootstrap 团队已经取消了hide
为hidden
,为什么fade
有什么不同呢?(2) 在单个转换中使用 2 个类
fade
和in
会污染类空间。而且,fade
和in
之间的相互关联尚不清楚。in
类看起来像是一个完全独立的类,就像alert
和alert-success
一样。最好的解决方案是在元素淡出时使用
faded
,并在元素淡入时使用faded-in
替换该类。提醒 Faded
So回答问题。我认为警报标记、样式和逻辑应该按以下方式编写。 注意:如果您使用的是普通 JavaScript,请随意替换 jQuery 逻辑。
HTML
CSS
JQuery
I don't agree with the way that Bootstrap uses
fade in
(as seen in their documentation - http://v4-alpha.getbootstrap.com/components/alerts/), and my suggestion is to avoid the class namesfade
andin
, and to avoid that pattern in general (which is currently seen in the top-rated answer to this question).(1) The semantics are wrong - transitions are temporary, but the class names live on. So why should we name our classes
fade
andfade in
? It should befaded
andfaded-in
, so that when developers read the markup, it's clear that those elements werefaded
orfaded-in
. The Bootstrap team has already done away withhide
forhidden
, why isfade
any different?(2) Using 2 classes
fade
andin
for a single transition pollutes the class space. And, it's not clear thatfade
andin
are associated with one another. Thein
class looks like a completely independent class, likealert
andalert-success
.The best solution is to use
faded
when the element has been faded out, and to replace that class withfaded-in
when the element has been faded in.Alert Faded
So to answer the question. I think the alert markup, style, and logic should be written in the following manner. Note: Feel free to replace the jQuery logic, if you're using vanilla javascript.
HTML
CSS
JQuery
当然,是的。在您的项目中使用这个简单的文件: https://gist.github.com/3851727
首先添加 HTML像这样:
然后使用:
您可以将所有引导警报类型(例如
error
、success
和warning
传递给type 属性作为选项。
Of course, Yes. Use this simple file in your project: https://gist.github.com/3851727
First add you HTML like this:
and then use:
You can pass all bootstrap alert types such as
error
,success
andwarning
totype
property as options.这是非常重要的问题,我正在努力通过替换当前消息并添加新消息来完成(显示/隐藏)消息。
下面是工作示例:
希望它能帮助其他人!
This is very important question and I was struggling to get it done (show/hide) message by replacing current and add new message.
Below is working example:
Hope it will help others!