简体   繁体   English

JavaScript 测试 CSS 样式表是否应用了 CSS3 媒体查询

[英]JavaScript test if a CSS stylesheet is applied with CSS3 media query

I have two style sheets:我有两个样式表:

<link rel="stylesheet" type="text/css" href="/core.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/core-desktop.css" media="only screen and (min-width: 800px)" id="css-desktop" />

The second one should only be loaded when the window is 800px or wider (narrower displays get a layout more appropriate for mobile devices).第二个应该仅在 window 为 800 像素或更宽时加载(较窄的显示布局更适合移动设备)。

The JavaScript needs to know if that second style sheet is being applied, I have tried the jQuery: JavaScript 需要知道是否正在应用第二个样式表,我已经尝试过 jQuery:

($(window).width() > 800)

But when you get around the 790 - 810px width, Chrome/Firefox (and probably others) will return a value such as 791, and the 800px+ style sheet will still be loaded.但是当你达到 790 - 810px 宽度时,Chrome/Firefox(可能还有其他)将返回一个值,例如 791,并且 800px+ 样式表仍将被加载。

I suspect it's due to the scroll bar (but targeting the $(document) or $('html') either doesn't work, or are the same).我怀疑这是由于滚动条(但针对 $(document) 或 $('html') 要么不起作用,要么是相同的)。

So is there a way to test if $('#css-desktop') is enabled/active/used?那么有没有办法测试 $('#css-desktop') 是否启用/活动/使用?

You can do the following.您可以执行以下操作。

  1. Make a div which has a display: none;制作一个有display: none; in the first stylesheet, so it is not shown.在第一个样式表中,因此未显示。
  2. In the second stylesheet you will add a position: absolute在第二个样式表中,您将添加一个position: absolute
  3. In your Javascript you check if( $("#thediv").css("position") == "absolute")在你的 Javascript 你检查if( $("#thediv").css("position") == "absolute")

The position: absolute; position: absolute; is only applied in the second stylesheet.仅应用于第二个样式表。

You could try你可以试试

window.matchMedia(document.getElementById('css-desktop').getAttribute('media')).matches;

https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia https://developer.mozilla.org/zh-CN/docs/Web/API/Window/matchMedia

Here is an example as to how you can try to detect the " real " width of the browser window: detect window width and compensate for scrollbars - Javascript这是一个关于如何尝试检测浏览器“真实”宽度的示例 window: 检测 window 宽度并补偿滚动条 - Javascript

There is a problem with what you are attempting.您尝试的操作有问题。

If a user is browsing the page while resizing the window below 800px in width then, he will get the mobile version of the page and later when he/she maximizes, he will still get the mobile version.如果用户在浏览页面时将 window 的宽度调整到 800px 以下,他将获得页面的移动版本,稍后当他/她最大化时,他仍将获得移动版本。

So, relying on screen width are not a reliable method as the screen resolution of the mobiles are growing significantly nowadays.因此,依靠屏幕宽度并不是一个可靠的方法,因为如今手机的屏幕分辨率正在显着增长。

Your best shot is to read the User Agent information of the browser, which will easily reveal whether it is a mobile browser or other and load the css files according to it.最好的办法是读取浏览器的User Agent信息,这将很容易揭示它是移动浏览器还是其他浏览器,并根据它加载 css 文件。 Then for the variable screen resolution, you can use your current techniques to load width specific codes.然后对于可变屏幕分辨率,您可以使用当前技术来加载特定于宽度的代码。

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

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