当有人单击x图标时,试图关闭bootstrap 4弹出窗口。
如果我删除代码的data-backDrop =" static",每当我单击模式外的任何地方,它都会关闭弹出窗口。但是,这种情况无法使用,因为每当有人尝试单击旋转木马的下一个图标时,它将自动关闭弹出窗口。我只希望当有人只单击x图标时弹出窗口。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div id="myModal" class="modal fade in" role="dialog" data-backdrop="static">
<div class="modal-dialog">
<button type="button" class="close" data-dismiss="myModal">×</button>
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="../images/placeimg_550_250_grayscale_any.jpg" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="https://placeimg.com/550/250/any" alt="Second slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
<script type="text/javascript">
$(window).on('load', function() {
$('#myModal').modal('show');
});
</script>
您应链接到 prom prom css和javaScript文件;对于Bootstrap 4(截至编写的当前版本为4.3.1(:
-
css:https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css
-
javascript:https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js
关闭按钮的data-dismiss
属性的值也应将其设置为modal
:
<button type="button" class="close" data-dismiss="modal">×</button>
关闭按钮和旋转木马也应放置在.modal-content
&MDASH中。您可以使用自定义CSS使模态保持透明。
工作示例
/* Automatically open the modal */
$('#myModal').modal('show');
/* Make the modal be transparent */
#myModal .modal-content {
background-color: transparent;
border: 0;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<div id="myModal" class="modal fade" role="dialog" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<!-- Button to close the modal -->
<div><button type="button" class="close" data-dismiss="modal">×</button></div>
<!-- Carousel -->
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="https://placeimg.com/550/250/nature" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="https://placeimg.com/550/250/any" alt="Second slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
将此代码添加到您的脚本标签:
$('#myModal').find(".close").click(function() {
$('#myModal').modal("hide");
});
因此您的代码应该是这样:
<script type="text/javascript">
$(window).on('load',function(){
$('#myModal').modal('show');
});
$('#myModal').find(".close").on("click", function() {
$('#myModal').modal("hide");
});
</script>
启动模式时,您可以通过下面给出的选项:
{ keyboard: false, backdrop: 'static'}
将通过单击背景和逃生按钮禁用模态。
或
<div id="modal" class="modal hide fade in" data-keyboard="false" data-backdrop="static">
和在关闭图标上您可以通过添加属性
关闭模型data-dismiss="modal" or ng-click="cancel()"
并在cancel((方法上添加功能以关闭模型
`$('#myModal').modal("hide");
在这里查看相同的地方: