【问题标题】:How not to request for sidebar elements if the sidebar is not visible如果侧边栏不可见,如何不请求侧边栏元素
【发布时间】:2021-05-10 16:19:04
【问题描述】:

让我们想象一个博客。

例如:https://around.createx.studio/blog-grid-rs.html

这里我们可以看到有一个内容区和一个侧边栏。侧边栏在窄屏幕上不可见。换句话说,它存在,但不可见。

我们可以通过图像的例子清楚地揭示它。让我们专注于那个戴着虚拟现实眼镜的女孩的形象。那是图片th01.jpg。

如果没有侧边栏,th01.jpg 一样加载。

我想摆脱它。侧边栏可能真的承载着图像或其他东西。它可能包含广告脚本等。如果侧边栏不可见,如果我们不向服务器请求其元素,这将大大提高性能。

如果侧边栏不可见,您能告诉我如何避免对侧边栏元素的请求吗?即使我无法使用它们,我也想要这些方法的完整列表。我只想知道所有这些,即使我只使用一个。

【问题讨论】:

    标签: html web-performance


    【解决方案1】:

    如果您可以收到请求中的侧边栏数据:

    const wrapper = document.querySelector('.wrapper');
    fetch('https://jsonplaceholder.typicode.com/albums/1/photos')
      .then(response => response.json())
      .then(result => result.forEach((el) => {
        const img = new Image();
        img.src = el.thumbnailUrl;
        img.onload = function() {
            wrapper.append(img);
        };
      }))
    

    【讨论】:

    • 侧边栏中的东西很多。不仅是图像。有广告脚本,非常缓慢且繁重。嵌入视频和 Instagram 帖子。他们都很慢。还有其他想法吗?
    猜你喜欢
    • 2016-08-17
    • 2022-12-10
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多