如何显示 bootstrap 弹出然后在 3 秒后隐藏?
How to show bootstrap pop up then hide after 3 seconds?
我想在页面加载时显示 bootstrap 弹出窗口,我想在 3 秒后隐藏此弹出窗口。我在 google 上找到了示例,但不知道如何根据我的要求实现此代码。
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
是的,这是一个使用CSS
的方法
html, body {
height:100%;
width:100%;
margin:0;
padding:0;
}
#exampleModalLong {
-moz-animation: cssAnimation 0s ease-in 3s forwards;
/* Firefox */
-webkit-animation: cssAnimation 0s ease-in 3s forwards;
/* Safari and Chrome */
-o-animation: cssAnimation 0s ease-in 3s forwards;
/* Opera */
animation: cssAnimation 0s ease-in 3s forwards;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
@keyframes cssAnimation {
to {
width:0;
height:0;
overflow:hidden;
}
}
@-webkit-keyframes cssAnimation {
to {
width:0;
height:0;
visibility:hidden;
}
}
您可以在 jquery 中使用 setTimeout 完成。您可以将 onload 函数放到正文中并调用 onload="openModal()"
function openModal(){
$("#exampleModalLong").modal();
setTimeout(function(){ $("#exampleModalLong").modal("hide"); }, 3000);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<body onload="openModal()">
<button type="button" class="btn btn-primary" onclick="openModal()" data-target="#exampleModalLong">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
</div>
</div>
</div>
</body>
我想在页面加载时显示 bootstrap 弹出窗口,我想在 3 秒后隐藏此弹出窗口。我在 google 上找到了示例,但不知道如何根据我的要求实现此代码。
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
是的,这是一个使用CSS
的方法 html, body {
height:100%;
width:100%;
margin:0;
padding:0;
}
#exampleModalLong {
-moz-animation: cssAnimation 0s ease-in 3s forwards;
/* Firefox */
-webkit-animation: cssAnimation 0s ease-in 3s forwards;
/* Safari and Chrome */
-o-animation: cssAnimation 0s ease-in 3s forwards;
/* Opera */
animation: cssAnimation 0s ease-in 3s forwards;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
@keyframes cssAnimation {
to {
width:0;
height:0;
overflow:hidden;
}
}
@-webkit-keyframes cssAnimation {
to {
width:0;
height:0;
visibility:hidden;
}
}
您可以在 jquery 中使用 setTimeout 完成。您可以将 onload 函数放到正文中并调用 onload="openModal()"
function openModal(){
$("#exampleModalLong").modal();
setTimeout(function(){ $("#exampleModalLong").modal("hide"); }, 3000);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<body onload="openModal()">
<button type="button" class="btn btn-primary" onclick="openModal()" data-target="#exampleModalLong">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
</div>
</div>
</div>
</body>