简体   繁体   English

jQuery和Bootstrap向上滑动警报

[英]jQuery & Bootstrap Slide Up Alert

I have an alert: 我有一个警报:

<div class="alert alert-success fade show" id='success-alert' role="alert">
        {{ message }}
      </div>

I have a button: 我有一个按钮:

<input name="submit" value="Submit" class="btn btn-primary" id="submit-id-submit" type="submit" method="post">

When the button is clicked, the alert appears. 单击按钮后,将显示警报。 I want the alert to slide up slowly using jQuery's .slideUp() . 我希望警报使用jQuery的.slideUp()缓慢滑动。 Here is my attempt: 这是我的尝试:

<script>
  $(document).ready (function(){
              $("#success-alert").hide();
              $("#submit-id-submit").click(function showAlert() {
                  $("#success-alert").fadeTo(2000, 2000).slideUp(500, function(){
                 $("#success-alert").slideUp(500);
                  });
              });
   });
</script>

This causes the alert to appear, and then abruptly disappear quickly (shorter than two seconds). 这将导致警报出现,然后突然消失(短于两秒钟)。 I believe the alert is somehow being called twice, because when I change the 500 (all three) to 2000 , the alert shows up, it slides up correctly, then a new alert appears and abruptly disappears. 我相信警报会以某种方式被调用两次,因为当我将500 (全部三个)更改为2000 ,警报会显示出来,它会正确向上滑动,然后会出现一个新警报,然后突然消失。

How should I achieve the slow slide up, once? 我应该如何一次缓慢地向上滑动?

The classes "fade" and "show" are the reason for the weird behavior in your code. 类“ fade”和“ show”是代码中出现奇怪行为的原因。

Is this what you're looking for? 这是您要找的东西吗?

JS FIDDLE DEMO JS FIDDLE DEMO

I got rid of those classes and added some CSS. 我摆脱了这些类,并添加了一些CSS。

Relevant code: 相关代码:

$("#submit-id-submit").click(function () {
    $("#success-alert").show(); // use slide down for animation
    setTimeout(function () {
      $("#success-alert").slideUp(500);
    }, 2000);
});

Hope this helps. 希望这可以帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM