我需要解释浏览器(Chrome/FF)如何加载CSS图像.

据我目前所知,CSS用作背景图像的图像会作为请求出现在网络选项卡中,相应的CSS文件作为启动器.

我目前的体验并不涵盖这一点-我体验到图像,这些图像由CSS加载,并且在页面上可见(非懒惰、上方、匿名窗口、禁用缓存的DevTools)-但不存在于网络选项卡中的请求列表中.当然,它涉及这个页面:https://www.arag.de/rechtsschutzversicherung/,关于这个图像,在页面的最顶部,如屏幕截图所示-它不会作为请求出现在网络选项卡中.

enter image description here

我的问题:我try 用两个控制台JavaScript计算页面上的图像.这两个JavaScript和网络选项卡都没有列出这些受影响的图像.

因此,我需要对浏览器的工作进行解释,以了解这种情况是如何发生的,尽管有些图像是由带有背景的CSS加载的:(IMG).

以下是我统计图像的JavaScript:

  1. 对于背景图像:

var elems = document.getElementsByTagName('*'),
  backgrounds = [];
for (var i = 0, len = elems.length; i < len; i++) {
  if (window.getComputedStyle(elems[i], null).getPropertyValue('background-image') != 'none') {
    backgrounds.push(window.getComputedStyle(elems[i], null).getPropertyValue('background-image'));
  }
}
console.log(backgrounds);
  1. 对于IMG

var imageSearch = {
  image_array: {},
  valid_image_nodes: ["DIV", "P", "SECTION", "SPAN"],
  scan_for_valid_images: function(node) {
    if (node.nodeType === 1) {
      if (node.nodeName === "IMG") {
        this.image_array[node.getAttribute("src")] = true;
      } else {
        if (this.valid_image_nodes.indexOf(node.nodeName) !== -1) {
          div_style = node.currentStyle || window.getComputedStyle(node, false);
          if (div_style.backgroundImage !== "none") {
            url = div_style.backgroundImage;
            this.image_array[url.slice(4, url.indexOf(')'))] = true;
          }
        }
      }
    }
    var children = node.childNodes;
    for (var i = 0; i < children.length; i++) {
      this.scan_for_valid_images(children[i]);
    }
  },
  get_image_array: function() {
    return Object.keys(this.image_array)
  }
}
imageSearch.scan_for_valid_images(document);
imageSearch.get_image_array()

推荐答案

缓存的图像不会重新下载,因此它们不会出现在网络选项卡中.因此,这些图像不会影响加载,但它们的状态显示为:"200 OK(来自磁盘缓存)".

您需要分别判断:before:after个伪元素,因为它们不是独立的HTML元素,而是元素的一部分.

var elems = document.querySelectorAll('*'),
  backgrounds = [];

elems.forEach(function(elem) {
  var computedStyle = window.getComputedStyle(elem),
      backgroundImage = computedStyle.getPropertyValue('background-image');

  // If the background image of the element is not 'none', add it to the backgrounds array
  if (backgroundImage !== 'none') {
    backgrounds.push(backgroundImage);
  }

  // Check if there is a ::before pseudo-element and its background image
  var beforeBackgroundImage = window.getComputedStyle(elem, ':before').getPropertyValue('background-image');
  if (beforeBackgroundImage && beforeBackgroundImage !== 'none') {
    backgrounds.push(beforeBackgroundImage);
  }

  // Check if there is an ::after pseudo-element and its background image
  var afterBackgroundImage = window.getComputedStyle(elem, ':after').getPropertyValue('background-image');
  if (afterBackgroundImage && afterBackgroundImage !== 'none') {
    backgrounds.push(afterBackgroundImage);
  }
});

console.log(backgrounds);
.example-div:before {
  content: '';
  position: absolute;
  width: 200px;
  height: 200px;
  border: 1px solid #000;
  background-image: url('https://picsum.photos/200/200');
}
<div class="example-div"></div>

Css相关问答推荐

我控制的自定义单选按钮无法重新 Select

如何允许在bslb中 Select 输入重叠卡片?

如何在css中从圆中切出1/n?

如何防止背景重复图像被截断

我的css是我的react 应用程序没有按预期工作

flexbox换行时自动设置高度的原理是什么

在 React 中使用 CSS 动画 onClick()

在 dbc.Container 上方包含横幅 - Dash

在 SVG 元素上方添加标签而不移动 SVG 元素

:required 或 [required] CSS Select 器

宽度为 x VW 的元素似乎对窗口调整大小没有react

基于变量动态生成 CSS 类 - SCSS

使用 Flex Box Tailwind CSS 的元素不相等

基于内容显示(show)/隐藏(hide)同级div

在 中制作等宽的列

如何在 CSS 中控制列表样式类型光盘的大小?

绝对位置和溢出:隐藏

如何更改 Angular Material 上 mat-icon 的大小?

输入和文本字段中的背景 colored颜色

在 react-native 中使用 flex 使项目粘在底部