gpt4 book ai didi

bootstrap-4 - 如何延迟加载 Bootstrap 4 卡图像?

转载 作者:行者123 更新时间:2023-12-04 03:14:10 25 4
gpt4 key购买 nike

我想弄清楚如何延迟加载 card-columnscard-img-top , 例子:

<div class="card-columns">
<div class="card text-center">
<img class="card-img-top lazy" src="img/foo.jpg" alt="foo" />
<div class="card-body">
<!-- more code -->
</div>
</div>
</div>

我的研究

搜索标签 我找到了“ How to animate Bootstrap 4 cards one by one? ”,但那是用于卡片上的动画而没有图像加载。

Bootstrap-carousel lazy loader ”适用于 Bootstrap 3。

在 Bootstrap 的文档中搜索我能够返回的唯一加载结果是 Spinners .

在标签下进一步搜索 我能够找到“ Bootstrap carousel Images not showing ”,但它专注于轮播而不是卡片。

尝试使用 lazyload 延迟加载后使用配方 Responsive images我省略了 data-srcset & data-sizes :

    var myLazyLoad = new LazyLoad({
elements_selector: ".lazy"
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card-columns">
<div class="card text-center">
<img class="card-img-top lazy" src="https://via.placeholder.com/300x150.png?text=this-is-a-sample" alt="foo" />
<div class="card-body">
<h3 class="card-title">Foo</h3>
<p class="card-text">Bacon ipsum dolor amet chuck tenderloin flank, shoulder buffalo sausage pork biltong ribeye drumstick jowl. Bacon flank salami, tri-tip chuck alcatra beef ribs brisket shankle ground round ham hock sausage t-bone. Ham hock frankfurter flank biltong cow. Flank picanha chuck meatball, porchetta pork loin frankfurter shank jerky pancetta bacon. Shoulder ham hock biltong shank meatloaf filet mignon sirloin corned beef kevin fatback landjaeger ham andouille burgdoggen cupim. Alcatra salami ball tip, bresaola tenderloin leberkas pig turducken pancetta ground round. Beef ribs tenderloin landjaeger chicken bresaola shankle, buffalo picanha ribeye fatback shoulder salami porchetta pancetta tongue.</p>
</div>
</div>
<div class="card text-center">
<img class="card-img-top lazy" src="https://via.placeholder.com/300x150.png?text=this-is-a-sample" alt="foo" />
<div class="card-body">
<h3 class="card-title">Foo</h3>
<p class="card-text">Bacon ipsum dolor amet chuck tenderloin flank, shoulder buffalo sausage pork biltong ribeye drumstick jowl. Bacon flank salami, tri-tip chuck alcatra beef ribs brisket shankle ground round ham hock sausage t-bone. Ham hock frankfurter flank biltong cow. Flank picanha chuck meatball, porchetta pork loin frankfurter shank jerky pancetta bacon. Shoulder ham hock biltong shank meatloaf filet mignon sirloin corned beef kevin fatback landjaeger ham andouille burgdoggen cupim. Alcatra salami ball tip, bresaola tenderloin leberkas pig turducken pancetta ground round. Beef ribs tenderloin landjaeger chicken bresaola shankle, buffalo picanha ribeye fatback shoulder salami porchetta pancetta tongue.</p>
</div>
</div>
<div class="card text-center">
<img class="card-img-top lazy" src="https://via.placeholder.com/300x150.png?text=this-is-a-sample" alt="foo" />
<div class="card-body">
<h3 class="card-title">Foo</h3>
<p class="card-text">Bacon ipsum dolor amet chuck tenderloin flank, shoulder buffalo sausage pork biltong ribeye drumstick jowl. Bacon flank salami, tri-tip chuck alcatra beef ribs brisket shankle ground round ham hock sausage t-bone. Ham hock frankfurter flank biltong cow. Flank picanha chuck meatball, porchetta pork loin frankfurter shank jerky pancetta bacon. Shoulder ham hock biltong shank meatloaf filet mignon sirloin corned beef kevin fatback landjaeger ham andouille burgdoggen cupim. Alcatra salami ball tip, bresaola tenderloin leberkas pig turducken pancetta ground round. Beef ribs tenderloin landjaeger chicken bresaola shankle, buffalo picanha ribeye fatback shoulder salami porchetta pancetta tongue.</p>
</div>
</div>
</div>


然而,我仍然面临的一个问题是图像具有响应性,所以我找到了“ Lazy loading with “responsive” images (unknown height)”,但与我正在尝试做的事情相冲突。

问答“ Lazy loading images how”确实出现在写问题时,但它是从 2010 年开始的,肯定有更好的方法。

问题

在 Bootstrap 4.2 中,如何延迟加载卡片的图像,该卡片将呈现响应图像的适当高度和宽度,但在滚动时加载? Bootstrap 4 是否有内置的方式来延迟加载我丢失的图像?我的测试是在慢速 3G 网络上的 Chrome 中进行的。

如果我没有收到通知或者我的方法是错误的,请告诉我我以前没有玩过延迟加载。

回答后

我在 referencing 之后修改了 HTML :
<img class="card-img-top lazyload" src="img/foo.jpg" data-src="img/foo.jpg" data-original="img/foo.jpg 480w, img/foo.jpg 640w, img/foo.jpg 1024w" alt="foo" width="765" height="auto" lazyload="on" />
<img class="card-img-top lazyload" src="img/bar.jpg" data-src="img/bar.jpg" data-original="img/bar.jpg 480w, img/bar.jpg 640w, img/bar.jpg 1024w" alt="fire" width="765" height="auto" lazyload="on" />

脚本调用:
<script src="https://cdn.jsdelivr.net/npm/lazyload@2.0.0-beta.2/lazyload.js"></script>
<script>$("img .lazyload").lazyload();</script>

Chrome 网络显示:

enter image description here

延迟加载也被添加到 FFFFFF&text=loading所以应该加载的第一个图像应该是 jumbo.jpg .

评论请求

每个请求一个 Bootply我尝试演示代码的内容。没有使用过 Bootply 但根据研究运行 JavaScript 我应该把它放在 <script>每个标签 here这就是我在 Bootply 中所做的。

最佳答案

使用JS延迟加载插件,相当完整和直观。

例子:

https://imagekit.io/blog/lazy-loading-images-complete-guide/

网址插件

https://github.com/tuupola/jquery_lazyload

HTML

<!-- Begin Body -->
<div class="container-fluid" id="main">
<div class="row">

<div class="col-md-12">
<h2>Image Grid with Lazy Loading Images</h2>
<p class="lead">Images will load as you scroll down</p>
<hr>
</div>
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-body"><img data-original="/assets/templates/tmp_129821.png" class="img-responsive"></div>
</div>
</div>
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-body"><img data-original="/assets/templates/tmp_128936.png" class="img-responsive"></div>
</div>
</div>
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-body"><img data-original="/assets/templates/101004.png" class="img-responsive"></div>
</div>
</div>
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-body"><img data-original="/assets/templates/100993.png" class="img-responsive"></div>
</div>
</div>
</div><!--/row-->
</div>
</div>

JS

$('#main .img-responsive').lazyload();

关于bootstrap-4 - 如何延迟加载 Bootstrap 4 卡图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150690/

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