javascript - alert box automatic disappear - Stack Overflow

I'm using Bootstrap and want to realize the following:having a kind of an alert box which disappe

I'm using Bootstrap and want to realize the following:
having a kind of an alert box which disappears after a short period of time automatically. I have something special in my Imagination which I saw often but can't find at the moment. I think the standard bootstrap alert ( and ) is not the right thing, is it? Because I want something to just "appear on top of something", so not as content of the page, if you know what I mean.

So it should just show a short "success information" when entering the page, and then it should disappear again. Or do I missunderstand Bootstrap alert wrong and it's exactly what I'm searching for? If yes, how do I use it? Where do I have to put that "div"?

I'm using Bootstrap and want to realize the following:
having a kind of an alert box which disappears after a short period of time automatically. I have something special in my Imagination which I saw often but can't find at the moment. I think the standard bootstrap alert (http://getbootstrap./javascript/#alerts and http://getbootstrap./ponents/#alerts) is not the right thing, is it? Because I want something to just "appear on top of something", so not as content of the page, if you know what I mean.

So it should just show a short "success information" when entering the page, and then it should disappear again. Or do I missunderstand Bootstrap alert wrong and it's exactly what I'm searching for? If yes, how do I use it? Where do I have to put that "div"?

Share Improve this question edited Feb 27, 2015 at 16:41 amphetamachine 30.7k12 gold badges68 silver badges74 bronze badges asked Feb 18, 2015 at 20:00 namelessnameless 1,5316 gold badges43 silver badges86 bronze badges 1
  • 1 I use toastr for something like this – exceptional exception Commented Feb 18, 2015 at 20:03
Add a ment  | 

2 Answers 2

Reset to default 3

Yes, that's also possible with alerts. Just add a timeout for the closing and add position: fixed style to have the alert in front of your content.

The following demo should give you an idea how you could use it.

The same code is also at this fiddle.

$().alert();

setTimeout(function(){$('.alert').alert('close')}, 3000);
.alert {
    position: fixed;
    top: 0px;
}
<link href="http://maxcdn.bootstrapcdn./bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn./bootstrap/3.3.2/js/bootstrap.min.js"></script>
<div class="alert alert-danger alert-dismissible fade in" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span>
    </button>
     <h4>Oh snap! You got an error!</h4>

    <p>Change this and that and try again. Duis mollis, est non modo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras mattis consectetur purus sit amet fermentum.</p>
</div>

<div><h1>Normal content</h1>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean modo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean modo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
</div>

If you want an alert box, you can consider something like bootstrap modal. Focus on the usage of the html and js

http://getbootstrap./javascript/#modals-examples

http://getbootstrap./javascript/#modals-usage

Here is a simple code block that can open modal on click, and auto dismiss modal after 2 seconds.

Html:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.2/css/bootstrap.min.css">
<script src="//code.jquery./jquery-1.11.2.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.2/js/bootstrap.min.js"></script>


<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Javascript:

$('#myModal').on('shown.bs.modal', function () {
  window.setTimeout(closeModal, 2000)
});

function closeModal(){
  $('#myModal').modal('hide');
}

Working example: http://codepen.io/anon/pen/ByrZwv

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744778032a4593161.html

相关推荐

  • javascript - alert box automatic disappear - Stack Overflow

    I'm using Bootstrap and want to realize the following:having a kind of an alert box which disappe

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信