gpt4 book ai didi

jquery - 仅在激活时加载模态(一次一个)

转载 作者:行者123 更新时间:2023-12-01 04:37:49 27 4
gpt4 key购买 nike

我试图阻止模态加载,我正在使用 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">&times;</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>

最佳答案

需要使用更具体的函数。这非常有效。

$('.modal').on("show.bs.modal", function() {
$('.lazy', this).Lazy({bind: 'event', chainable: false});
});

您还可以使用 src 属性将加载 gif 添加到图像中,jquery.lazy 的插件将在加载实际图像时切换两者。因此,使用 data-src="actual image page" 放入实际图像,并在同一个 img 标签上放入加载 gif src="loading.gif"

关于jquery - 仅在激活时加载模态(一次一个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46422345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com