Twitter Bootstrap 提醒可以淡入淡出吗?

发布于 2024-12-08 21:30:51 字数 437 浏览 1 评论 0原文

当我第一次在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

因为看清所以看轻 2024-12-15 21:30:51

我强烈不同意前面提到的大多数答案。

简短回答:

省略“in”类并使用 jQuery 添加它以使其淡入。

有关 3 秒后淡入警报的示例,请参阅此 jsfiddle http://jsfiddle.net/QAz2U/3/

长答案:

虽然引导程序本身并不支持警报淡入淡出,但这里的大多数答案都使用jQuery 淡入淡出函数,它使用 JavaScript 来对元素进行动画处理(淡入淡出)。这样做的一大优点是跨浏览器兼容性。缺点是性能(另请参阅:jQuery 调用 CSS3 淡入淡出动画?) 。

Bootstrap 使用 CSS3 过渡,其性能更好。这对于移动设备很重要:

引导 CSS 淡化警报:

.fade {
  opacity: 0;
  -webkit-transition: opacity 0.15s linear;
  -moz-transition: opacity 0.15s linear;
  -o-transition: opacity 0.15s linear;
  transition: opacity 0.15s linear;
}
.fade.in {
  opacity: 1;
}

为什么我认为这种性能如此重要?使用旧浏览器和硬件的人可能会因 jQuery.fade() 而出现不稳定的过渡。对于带有现代浏览器的旧硬件来说也是如此。使用 CSS3 过渡,即使使用较旧的硬件,使用现代浏览器的人也会获得流畅的动画,而使用不支持 CSS 过渡的旧浏览器的人将立即看到元素弹出,我认为这比断断续续的动画有更好的用户体验。

我来这里寻找与上面相同的答案:淡入引导警报。在深入研究 Bootstrap 的代码和 CSS 后,答案相当简单。不要将“in”类别添加到您的警报中。当您想要淡入警报时,可以使用 jQuery 添加此内容。

HTML(注意没有 in 类!)

<div id="myAlert" class="alert success fade" data-alert="alert">
  <!-- rest of alert code goes here -->
</div>

Javascript:

function showAlert(){
  $("#myAlert").addClass("in")
}

调用上面的函数会添加“in”类,并使用以下命令淡入警报CSS3 转换:-)

另请参阅此 jsfiddle 有关使用超时的示例(感谢 John Lehmann!): http://jsfiddle.net/QAz2U/3/

I strongly disagree with most answers previously mentioned.

Short answer:

Omit the "in" class and add it using jQuery to fade it in.

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:

.fade {
  opacity: 0;
  -webkit-transition: opacity 0.15s linear;
  -moz-transition: opacity 0.15s linear;
  -o-transition: opacity 0.15s linear;
  transition: opacity 0.15s linear;
}
.fade.in {
  opacity: 1;
}

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!)

<div id="myAlert" class="alert success fade" data-alert="alert">
  <!-- rest of alert code goes here -->
</div>

Javascript:

function showAlert(){
  $("#myAlert").addClass("in")
}

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/

小霸王臭丫头 2024-12-15 21:30:51

我使用的东西是这样的:

在你的模板中,有一个警报区域

<div id="alert-area"></div>

,然后是一个用于显示警报的 jQuery 函数

function newAlert (type, message) {
    $("#alert-area").append($("<div class='alert-message " + type + " fade in' data-alert><p> " + message + " </p></div>"));
    $(".alert-message").delay(2000).fadeOut("slow", function () { $(this).remove(); });
}
newAlert('success', 'Oh yeah!');

The thing I use is this:

In your template an alert area

<div id="alert-area"></div>

Then an jQuery function for showing an alert

function newAlert (type, message) {
    $("#alert-area").append($("<div class='alert-message " + type + " fade in' data-alert><p> " + message + " </p></div>"));
    $(".alert-message").delay(2000).fadeOut("slow", function () { $(this).remove(); });
}
newAlert('success', 'Oh yeah!');
是伱的 2024-12-15 21:30:51

您可以使用 jquery 淡入框。使用'hide'类内置的bootstraps有效地在div元素上设置display:none:

<div id="saveAlert" class="alert alert-success hide" data-alert="alert" style="top:0">
            <a class="close" href="#">×</a>
            <p><strong>Well done!</strong> You successfully read this alert message.</p>
        </div>

然后使用jquery中的fadeIn函数,如下所示:

$("#saveAlert").fadeIn();

还有为fadeIn函数指定持续时间,例如:
$("#saveAlert").fadeIn(400);

有关使用 fadeIn 函数的完整详细信息可以在官方 jQuery 文档站点上找到:http://api.jquery.com/fadeIn/

顺便说一句,如果您不使用 jquery,您可以将“隐藏”类添加到您自己的 CSS 文件中,或者将其添加到您的 div 中:

 <div style="display:none;" id="saveAlert">

然后,您的 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:

<div id="saveAlert" class="alert alert-success hide" data-alert="alert" style="top:0">
            <a class="close" href="#">×</a>
            <p><strong>Well done!</strong> You successfully read this alert message.</p>
        </div>

and then use the fadeIn function in jquery, like so:

$("#saveAlert").fadeIn();

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:

 <div style="display:none;" id="saveAlert">

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.

如此安好 2024-12-15 21:30:51

对于 2.3 及更高版本,只需添加:

$(".alert").fadeOut(3000 );

bootstrap:

<div class="alert success fade in" data-alert="alert" >
    <a class="close" data-dismiss="alert" href="#">×</a>
    // code 
</div>

适用于所有浏览器。

For 2.3 and above, just add:

$(".alert").fadeOut(3000 );

bootstrap:

<div class="alert success fade in" data-alert="alert" >
    <a class="close" data-dismiss="alert" href="#">×</a>
    // code 
</div>

Works in all browsers.

回眸一遍 2024-12-15 21:30:51

hide类添加到alert-message。然后在 jQuery 脚本导入后添加以下代码:

$(document).ready( function(){
    $(".alert-message").animate({ 'height':'toggle','opacity':'toggle'});
    window.setTimeout( function(){
        $(".alert-message").slideUp();
    }, 2500);
});

如果要处理多条消息,此代码将按升序隐藏它们:

$(document).ready( function(){
   var hide_delay = 2500;  // starting timeout before first message is hidden
   var hide_next = 800;   // time in mS to wait before hiding next message
   $(".alert-message").slideDown().each( function(index,el) {
      window.setTimeout( function(){
         $(el).slideUp();  // hide the message
      }, hide_delay + hide_next*index);
   });
});

Add hide class to alert-message. Then put the following code after your jQuery script import:

$(document).ready( function(){
    $(".alert-message").animate({ 'height':'toggle','opacity':'toggle'});
    window.setTimeout( function(){
        $(".alert-message").slideUp();
    }, 2500);
});

If you want handle multiple messages, this code will hide them in ascending order:

$(document).ready( function(){
   var hide_delay = 2500;  // starting timeout before first message is hidden
   var hide_next = 800;   // time in mS to wait before hiding next message
   $(".alert-message").slideDown().each( function(index,el) {
      window.setTimeout( function(){
         $(el).slideUp();  // hide the message
      }, hide_delay + hide_next*index);
   });
});
魔法唧唧 2024-12-15 21:30:51

目前的答案都不适合我。我正在使用 Bootstrap 3。

我喜欢 Rob Vermeer 所做的事情并从他的回复开始。

对于淡入然后淡出效果,我只是使用编写了以下函数并使用 jQuery:

Html 在我的页面上将警报添加到:

        <div class="alert-messages text-center">
        </div>

Javascript 函数以显示和关闭警报。

function showAndDismissAlert(type, message) {
    var htmlAlert = '<div class="alert alert-' + type + '">' + message + '</div>';

    // Prepend so that alert is on top, could also append if we want new alerts to show below instead of on top.
    $(".alert-messages").prepend(htmlAlert);

    // Since we are prepending, take the first alert and tell it to fade in and then fade out.
    // Note: if we were appending, then should use last() instead of first()
    $(".alert-messages .alert").first().hide().fadeIn(200).delay(2000).fadeOut(1000, function () { $(this).remove(); });
}

然后,要显示和关闭警报,只需调用如下函数:

    showAndDismissAlert('success', 'Saved Successfully!');
    showAndDismissAlert('danger', 'Error Encountered');
    showAndDismissAlert('info', 'Message Received');

作为旁注,我将 div.alert-messages 的样式固定在顶部:

    <style>
    div.alert-messages {
        position: fixed;
        top: 50px;
        left: 25%;
        right: 25%;
        z-index: 7000;
    }
    </style>

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:

        <div class="alert-messages text-center">
        </div>

Javascript function to show and dismiss the alert.

function showAndDismissAlert(type, message) {
    var htmlAlert = '<div class="alert alert-' + type + '">' + message + '</div>';

    // Prepend so that alert is on top, could also append if we want new alerts to show below instead of on top.
    $(".alert-messages").prepend(htmlAlert);

    // Since we are prepending, take the first alert and tell it to fade in and then fade out.
    // Note: if we were appending, then should use last() instead of first()
    $(".alert-messages .alert").first().hide().fadeIn(200).delay(2000).fadeOut(1000, function () { $(this).remove(); });
}

Then, to show and dismiss the alert, just call the function like this:

    showAndDismissAlert('success', 'Saved Successfully!');
    showAndDismissAlert('danger', 'Error Encountered');
    showAndDismissAlert('info', 'Message Received');

As a side note, I styled the div.alert-messages fixed on top:

    <style>
    div.alert-messages {
        position: fixed;
        top: 50px;
        left: 25%;
        right: 25%;
        z-index: 7000;
    }
    </style>
苍暮颜 2024-12-15 21:30:51

我用这种方法在 3 秒后关闭淡出警报。希望它会有用。

    setTimeout(function(){
    $('.alert').fadeTo("slow", 0.1, function(){
        $('.alert').alert('close')
    });     
    }, 3000)

I got this way to close fading my Alert after 3 seconds. Hope it will be useful.

    setTimeout(function(){
    $('.alert').fadeTo("slow", 0.1, function(){
        $('.alert').alert('close')
    });     
    }, 3000)
水波映月 2024-12-15 21:30:51

我不同意 Bootstrap 使用淡入的方式(如他们的文档中所示 - http://v4-alpha.getbootstrap.com/components/alerts/),我的建议是避免类名 fadein,并避免一般情况下的这种模式(目前在该问题的评分最高的答案中可以看到)。

(1) 语义是错误的——转换是暂时的,但类名仍然存在。那么为什么我们要把我们的类命名为 fadefade in 呢?它应该是 fadedfaded-in,这样当开发人员阅读标记时,就可以清楚地看到这些元素是 fadedfaded -在。 Bootstrap 团队已经取消了 hidehidden,为什么 fade 有什么不同呢?

(2) 在单个转换中使用 2 个类 fadein 会污染类空间。而且,fadein 之间的相互关联尚不清楚。 in 类看起来像是一个完全独立的类,就像 alertalert-success 一样。

最好的解决方案是在元素淡出时使用 faded,并在元素淡入时使用 faded-in 替换该类。

提醒 Faded

So回答问题。我认为警报标记、样式和逻辑应该按以下方式编写。 注意:如果您使用的是普通 JavaScript,请随意替换 jQuery 逻辑

HTML

<div id="saveAlert" class="alert alert-success">
  <a class="close" href="#">×</a>
  <p><strong>Well done!</strong> You successfully read this alert message.</p>
</div>

CSS

.faded {
  opacity: 0;
  transition: opacity 1s;
}

JQuery

$('#saveAlert .close').on('click', function () {

  $("#saveAlert")
    .addClass('faded');

});

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 names fade and in, 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 and fade in? It should be faded and faded-in, so that when developers read the markup, it's clear that those elements were faded or faded-in. The Bootstrap team has already done away with hide for hidden, why is fade any different?

(2) Using 2 classes fade and in for a single transition pollutes the class space. And, it's not clear that fade and in are associated with one another. The in class looks like a completely independent class, like alert and alert-success.

The best solution is to use faded when the element has been faded out, and to replace that class with faded-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

<div id="saveAlert" class="alert alert-success">
  <a class="close" href="#">×</a>
  <p><strong>Well done!</strong> You successfully read this alert message.</p>
</div>

CSS

.faded {
  opacity: 0;
  transition: opacity 1s;
}

JQuery

$('#saveAlert .close').on('click', function () {

  $("#saveAlert")
    .addClass('faded');

});
苏佲洛 2024-12-15 21:30:51

当然,是的。在您的项目中使用这个简单的文件: https://gist.github.com/3851727

首先添加 HTML像这样:

<div id="messagebox" class="alert hide"></div>

然后使用:

$("#messagebox").message({text: "Hello world!", type: "error"});

您可以将所有引导警报类型(例如 errorsuccesswarning 传递给 type 属性作为选项。

Of course, Yes. Use this simple file in your project: https://gist.github.com/3851727

First add you HTML like this:

<div id="messagebox" class="alert hide"></div>

and then use:

$("#messagebox").message({text: "Hello world!", type: "error"});

You can pass all bootstrap alert types such as error, success and warning to type property as options.

萌面超妹 2024-12-15 21:30:51

这是非常重要的问题,我正在努力通过替换当前消息并添加新消息来完成(显示/隐藏)消息。

下面是工作示例:

function showAndDismissAlert(type, message) {

  var htmlAlert = '<div class="alert alert-' + type + '">' + message + '</div>';
  $(".alert-messages").prepend(htmlAlert);
  $(".alert-messages .alert").hide().fadeIn(600).delay(2000).fadeOut(1000, function() {
    $(this).remove();
  });

}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="alert-messages"></div>

<div class="buttons">
  <button type="button" name="button" onclick="showAndDismissAlert('success', 'Saved Successfully!')">Button1</button>
  <button type="button" name="button" onclick="showAndDismissAlert('danger', 'Error Encountered')">Button2</button>
  <button type="button" name="button" onclick="showAndDismissAlert('info', 'Message Received')">Button3</button>
</div>

希望它能帮助其他人!

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:

function showAndDismissAlert(type, message) {

  var htmlAlert = '<div class="alert alert-' + type + '">' + message + '</div>';
  $(".alert-messages").prepend(htmlAlert);
  $(".alert-messages .alert").hide().fadeIn(600).delay(2000).fadeOut(1000, function() {
    $(this).remove();
  });

}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="alert-messages"></div>

<div class="buttons">
  <button type="button" name="button" onclick="showAndDismissAlert('success', 'Saved Successfully!')">Button1</button>
  <button type="button" name="button" onclick="showAndDismissAlert('danger', 'Error Encountered')">Button2</button>
  <button type="button" name="button" onclick="showAndDismissAlert('info', 'Message Received')">Button3</button>
</div>

Hope it will help others!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文