我试图阻止模态加载,我使用Jquery Lazy使用bootstrap 4模式。我可以在页面加载时得到模态不加载,但我遇到的问题是当一个模态被激活时,它们都加载。我希望防止这种情况发生,让每个模态仅在激活时加载。我正在考虑拥有超过300个模态(图像带来旋转木马模态),我怀疑有人会定期检查每个模态。任何帮助表示赞赏。
$('.modal').on("show.bs.modal", function() {
$('img.lazy').each(function() {
var img = $(this);
img.attr('src', img.data('src'));
});
});

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.5/jquery.lazy.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.5/jquery.lazy.plugins.min.js"></script>
<div class="col-lg-3 col-md-3 col-sm-6">
<div class="img-thumbnail image"><img data-toggle="modal" src="https://placeimg.com/200/120/any" data-target="#modal" class="img-fluid">
</div>
<h4>...</h4>
</div>
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container-fluid carousel-container">
<div class="container">
<div class="row">
<div class="col">
<div class="carousel slide carousel-fade" id="carousel-">
<div class="carousel-inner">
<div class="active carousel-item" data-slide-number="0">
<img data-src="https://placeimg.com/450/250/any" class="lazy img-thumbnail"></div>
</div>
</div>
</div>
</div>
<hr>
<!--/Slider-->
<div class="row">
<div class="col">
<a class="carousel-selector" data-slide="0"><img data-src="https://placeimg.com/75/42/any" class="lazy img-thumbnail"></a>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark btn-sm" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:1)
需要使用更具体的功能。这非常有效。
$('.modal').on("show.bs.modal", function() {
$('.lazy', this).Lazy({bind: 'event', chainable: false});
});
您还可以使用src属性将加载gif添加到图像中,jquery.lazy中的插件将在加载实际图像时切换两者。因此,将实际图像放在使用data-src="actual image page"
和放入加载gif src="loading.gif"