【问题标题】:Unable to use Lazy load + Dynamic image manipulation Cloudinary无法使用延迟加载 + 动态图像处理 Cloudinary
【发布时间】:2021-10-04 19:07:48
【问题描述】:

我无法同时使用功能Cloudinary Lazyload + Dynamic image manipulation

同时使用这两个功能有什么技巧吗?

我正在使用 HTML 网站。

我的代码是

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://unpkg.com/cloudinary-core@latest/cloudinary-core-shrinkwrap.js"></script> 
<script type="text/javascript">
         var cl = cloudinary.Cloudinary.new({cloud_name: "syg"}); 
         // replace 'demo' with your cloud name in the line above 
         cl.responsive();
</script>
<script>
        document.addEventListener("DOMContentLoaded", function() {
            const imageObserver = new IntersectionObserver((entries, imgObserver) => {
                entries.forEach((entry) => {
                    if (entry.isIntersecting) {
                        const lazyImage = entry.target
                        console.log("lazy loading ", lazyImage)
                        lazyImage.src = lazyImage.dataset.src
                    }
                })
            });
            const arr = document.querySelectorAll('img.lzy_img')
            arr.forEach((v) => {
                imageObserver.observe(v);
            })
        })
</script>
&lt;img class="cld-responsive lzy_img"  data-src="https://res.cloudinary.com/syg/image/upload/w_auto,c_scale/sample.jpg" /&gt;

【问题讨论】:

    标签: lazy-loading cloudinary


    【解决方案1】:

    响应式脚本将应用相关的宽度值并根据容器大小替换 URL 中的w_auto。 由于您的代码不限制 &lt;img&gt; 容器,因此它应用了屏幕的最大宽度尺寸。

    出于测试目的,您可以用&lt;div style="width:50%;"&gt;&lt;/div&gt; 包裹您的&lt;img&gt; 元素,您会看到图片网址相应地调整了宽度变换:

    <div style="width:50%;">
      <img class="cld-responsive lzy_img" data-src="https://res.cloudinary.com/syg/image/upload/w_auto,c_scale/v346346/sample.jpg"/>
    </div>
    

    此外,您可以查看以下更广泛的 LQIP+Lazy Loading+Responsive 与 Cloudinary 实施,以获取有关如何在您的网站页面中实施这些功能的参考和想法。

    【讨论】:

    • 我能有一些简单的方法吗?我有 50 多张图像要使用延迟加载和动态图像处理来加载。在你的方法中,我必须在 javascript 中写下所有图像的名称,这将是一个太长的过程。
    • 我正在 div 中加载图像
      res.cloudinary.com/syg/image/upload/w_auto,c_scale/v346346/…>
    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 2013-11-09
    相关资源
    最近更新 更多