简体   繁体   English

document.ready & window.onload vs CSS 媒体查询

[英]document.ready & window.onload vs CSS Media Queries

While doing research on CSS and JS codes that run before the user sees the content, something was stuck to my mind.在研究在用户看到内容之前运行的 CSS 和 JS 代码时,我想到了一些东西。 When exactly were CSS media queries were running? CSS 媒体查询何时运行? Which js and jquery function run first (document.onload, document.ready etc.)?哪个 js 和 jquery 函数先运行(document.onload、document.ready 等)? When I did research on it I found This and This questions which explain the difference between domcontentloaded, document.ready and window.onload functions.当我对它进行研究时,我发现了ThisThis questions 解释了 domcontentloaded、document.ready 和 window.onload 函数之间的区别。 However, I could not figure out when exactly media queries in CSS run compared to these functions.但是,与这些函数相比,我无法弄清楚 CSS 中的媒体查询何时运行。 So I checked it through JSFiddle in here .所以我通过这里的JSFiddle 检查了它。

In HTML part I only wrote a div with and ID;在 HTML 部分,我只写了一个带有 ID 的 div;

<div id="testDiv">
  Test Media Queries And JS Functions
</div>

In CSS there was only 1 media query that ran;在 CSS 中,只有 1 个媒体查询运行;

@media screen and (min-width:1px){
  #testDiv{
    color: black;
  }
}

And in JS part, there was different load functions而在 JS 部分,有不同的加载功能

$('document').ready(function() {
  document.getElementById("testDiv").style.color = "blue";
});

document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("testDiv").style.color = "red";
});

window.onload = function() {
  document.getElementById("testDiv").style.color = "yellow";
};

The CSS code and its effects were visible for a brief moment. CSS 代码及其效果在短时间内可见。 And than the div was turning to blue when I ran the code.当我运行代码时,div 变成了蓝色。 However, I encountered a strange problem.但是,我遇到了一个奇怪的问题。 Only in Google Chrome New Tab (Not Incognito), the code ran as yellow which means window.onload was running last and in all other browsers and tabs shows the div as blue.仅在 Google Chrome 新标签(非隐身)中,代码显示为黄色,这意味着 window.onload 最后运行,并且在所有其他浏览器和标签中将 div 显示为蓝色。

Google Chrome Not Incognito Tab谷歌浏览器不隐身标签

Google Chrome Incognito Tab and Safari New Tab Google Chrome 隐身标签和 Safari 新标签

First of all is this some kind of bug that I encountered or somehow is it intended to run like this?首先,这是我遇到的某种错误还是它打算像这样运行?

Second of all, I could not find any documentation on when exactly media queries run compared to these functions.其次,与这些函数相比,我找不到任何关于媒体查询何时运行的文档。

If anyone has a link for documentation on CSS queries and their exact execution time, I would really appreciate it.如果有人有关于 CSS 查询及其确切执行时间的文档的链接,我将不胜感激。

Second Answer First先回答第二个

From Here You already familiar that if you use Javascript (that is not async or defer) then the DOM creation will wait for the JS to load.这里您已经熟悉了,如果您使用 Javascript(不是异步或延迟),那么DOM创建将等待 JS 加载。 Since JS also modifies CSS , so JS will wait for the CSSOM to load, and in this way you got the answer that media queries runs earlier.由于JS也会修改CSS ,所以JS会等待CSSOM加载,这样你就得到了媒体查询更早运行的答案。

Now First one现在第一个

Basically基本上

The DOMContentLoaded event is fired when the DOM is created.创建DOM时会触发DOMContentLoaded事件。

The jQuery $(document).ready() event is fired when the DOM is ready.DOM准备好时,jQuery $(document).ready()事件被触发。

The load event is fired when the DOM , CSSOM and all other resources are loaded (jQuery library is one of them).load DOMCSSOM和所有其他资源(jQuery 库是其中之一)时,将触发load事件。

In this case在这种情况下

First DOM creation will wait for the JS to load when it is loaded then DOM is created, and DOMContentLoaded event fires at first place so color:red overrides color:black applied by media query.第一次DOM创建将等待JS在加载时加载,然后创建DOM ,并且DOMContentLoaded事件首先触发,因此color:red覆盖由媒体查询应用的color:black

Now load event is fired at second place because JS is already loaded and DOM and CSSOM is also loaded so color:yellow overrides color:red ,现在load事件在第二位触发,因为JS已经加载并且DOMCSSOM也加载所以color:yellow覆盖color:red

And document.ready event is fired at third place when DOM is ready so color:blue overrides color:yellow .当 DOM 准备好时, document.ready事件在第三位触发,因此color:blue覆盖color:yellow and you see color blue .你会看到蓝色

Important重要的

If you add an image just like Here then you'll see that如果你添加一个像Here那样的图像,那么你会看到

First DOM creation will wait for the JS to load when it is loaded then DOM is created, and DOMContentLoaded event fires at first place so color:red overrides color:black applied by media query.第一次DOM创建将等待JS在加载时加载,然后创建DOM ,并且DOMContentLoaded事件首先触发,因此color:red覆盖由媒体查询应用的color:black

Now document.ready event is fired at Second place when DOM is ready so color:blue overrides color:red .现在document.ready事件在 DOM 准备好时在第二位触发,因此color:blue覆盖color:red

And load event is fired at Third place because image is not loaded when DOM was ready and so color:yellow overrides color:blue after image is loaded,并且load事件在第三位触发,因为当DOM准备好时image没有加载,所以color:yellow在图像加载后覆盖color:blue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM