简体   繁体   English

使用@media查询时,手机是否会加载不相关的查询和图片?

[英]When using @media queries, does a phone load non-relevent queries and images?

If I base my CSS on mobile styling, then use @media queries for gradually larger displays (tablets, desktops etc), will the mobile devices use the desktop styles? 如果我将CSS基于移动样式,然后使用@media查询逐渐扩大显示(平板电脑,台式机等),移动设备是否会使用桌面样式?

I believe that typically, mobile devices will load all images even if they don't apply to its own particular media size. 我相信通常情况下,移动设备会加载所有图像,即使它们不适用于自己特定的媒体大小。 Meaning it will load all images and hide ones not matching its query-based stylesheet. 这意味着它将加载所有图像并隐藏与其基于查询的样式表不匹配的图像。

What I am trying to do is use one background for the larger version of the site: 我想要做的是使用一个背景为更大的网站版本:

.splash {
    background: #1a1a1a url('/assets/imageLarge.png') no-repeat;
}

and another for the mobile version: 和另一个移动版本:

.splash {
    background: #1a1a1a url('/assets/imageSmall.png') no-repeat;
}

If I apply the mobile CSS before any media queries, and add the large media CSS below using a query like @media screen and (min-device-width: 481px) {...} , will mobile devices load the large image too? 如果我在任何媒体查询之前应用移动CSS,并使用@media screen and (min-device-width: 481px) {...}类的查询添加下面的大型媒体CSS,那么移动设备是否也会加载大图像?

Behaviour is browser depended but iOS Safari and Android Chrome will respect the media queries and only download the background images for the applicable media queries. 行为取决于浏览器,但iOS Safari和Android Chrome将尊重媒体查询,仅下载适用媒体查询的背景图片。

If you want to inspect this behaviour, try loading the page with Mobitest ( http://mobitest.akamai.com/ ) or a tethered device. 如果要检查此行为,请尝试使用Mobitest( http://mobitest.akamai.com/ )或系留设备加载页面。

If you use separate CSS files (and I'd urge you not too) the browser will download each of them even if it doesn't need them, this is a limitation of the CSSOM. 如果你使用单独的CSS文件(我也不会劝你),浏览器会下载每个文件,即使它不需要它们,这也是CSSOM的限制。 Some browsers eg WebKit and Blink based ones prioritise the stylesheets so the ones needed to render the page are downloaded first and the others at some point later. 一些浏览器(例如基于WebKit和Blink的浏览器)优先考虑样式表,因此首先下载呈现页面所需的页面,然后在某些时候下载其他页面。

One thing to watch out for is display:none on content images as it won't prevent download in many situations. 需要注意的一点是显示:内容图像上没有,因为在许多情况下它不会阻止下载。 Tim Kadlec explores this more here: http://timkadlec.com/2012/04/media-query-asset-downloading-results/ Tim Kadlec在这里更多地探讨了这个问题: http//timkadlec.com/2012/04/media-query-asset-downloading-results/

Tim Kadlec has put together some awesome research for this – Media Query & Asset Downloading Results Tim Kadlec为此进行了一些很棒的研究 - 媒体查询和资产下载结果

Your specific question is answered in Test #4 – results are spotty. 测试#4中回答了您的具体问题 - 结果不稳定。 Better to have media queries for both your images. 最好对您的图像进行媒体查询。

This sounds like it would be largely browser dependant, but I'd imagine any mobile browser worth its salt would try to cut down on data usage by not loading images (and possibly not loading entire stylesheets) that are marked as not for it. 这听起来很大程度上取决于浏览器,但我认为任何有价值的移动浏览器都会尝试通过不加载标记为不适合它的图像(并且可能不加载整个样式表)来减少数据使用量。 Furthermore many mobile browsers prefer to not be recognized as mobile browsers. 此外,许多移动浏览器不希望被识别为移动浏览器。 I know I hate it when I pop open a site on my iPad and a mobile-stylesheet forces me to view a skinny sliver of single column site on my 9.7" screen. 我知道当我在iPad上打开一个网站并且移动样式表迫使我在我的9.7英寸屏幕上查看一小块单列网站时,我讨厌它。

So media queries are unreliable, but still worthwhile (they really don't hurt anything, so long as they're used responsibly), and that doesn't help what is a fairly obtuse (but still good) question; 所以媒体查​​询是不可靠的,但仍然值得(他们真的不会伤害任何东西,只要他们被负责任地使用),这无助于一个相当迟钝(但仍然很好)的问题; time to do some testing! 是时候做一些测试了!

Most modern desktop browsers come packaged with developer tools. 大多数现代桌面浏览器都附带开发人员工具。 My current favorite is FireFox's dark and pretty web inspector (the 3D view is especially to die for). 我目前最喜欢的是FireFox的黑暗和漂亮的网络检查员(3D视图尤其适合死亡)。 But what about on Mobile? 但是移动设备呢? The largest parts of your mobile audience will not be on browsers that come with developement tools. 移动受众群体中最大的部分不会出现在开发工具附带的浏览器上。

Well, you have a couple options: 嗯,你有几个选择:

  • Firebug Lite has some mixed results on mobile browsers, but is an excelent choice in most cases where other inspectors are unavailable. Firebug Lite在移动浏览器上有一些混合的结果,但在其他检查员不可用的大多数情况下是一个很好的选择。 It does seem to work in iOS and other mobile browsers with HTML5 support, though. 但它确实可以在iOS和其他支持HTML5的移动浏览器中运行。
  • This question suggests using something called "weinre". 这个问题建议使用一种叫做“weinre”的东西。 I've never used it, but it looks legit enough. 我从来没有使用它,但它看起来足够合法。
  • If you're okay with targetting just a few certain browsers, many DO include developer tools. 如果您只针对几个特定的​​浏览器,那么很多DO包括开发人员工具。 Such as Google Chrome for Android ! 比如Google Chrome for Android

Whatever you choose, you'll be looking for an asset viewer of some sort; 无论你选择什么,你都会寻找某种资产观察者; perhaps a timeline view. 也许是一个时间轴视图。 Any sort of tool that will allow you to see what the page loaded, in what order it loaded it, and how long it took to load. 任何类型的工具,可以让您查看页面加载的内容,加载页面的顺序以及加载所需的时间。

Good luck! 祝好运!

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

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