简体   繁体   English

对不支持CSS媒体查询的浏览器使用Javascript

[英]Using Javascript for browsers that don't support CSS media queries

I'm working on a project and I have been researching various approaches to responsive layouts by using media queries. 我正在从事一个项目,并且我一直在研究通过使用媒体查询来响应布局的各种方法。 One of the many things I've been thinking about is browsers that don't support them, Internet Explorer versions 5.5 - 8 in particular ( source ) and using Javascript as a fallback method with options such as css3-mediaqueries.js or respond.js . 我一直在考虑的众多问题之一是不支持它们的浏览器,尤其是Internet Explorer 5.5-8 ),并且使用Javascript作为后备方法,并带有诸如css3-mediaqueries.js之类的选项或响应。 js

I'm not looking to know which is best to use out of the two but questioning how much of a necessity it is to even use them. 我不是想知道这两种中哪一种是最佳使用,而是质疑甚至使用它们的必要性。

The way I'm seeing it right now is, if you're using a handheld device (mobile or tablet) you're most likely using a browser which supports media queries, since I'd imagine, most of these devices browsers are modern and were built with media queries in mind. 我现在所看到的是,如果您使用的是手持设备(移动设备或平板电脑),则很可能是使用支持媒体查询的浏览器,因为我想像到,大多数这些设备浏览器都是现代的并且在构建时考虑了媒体查询。 So if a user is viewing the website in an old IE browser it is most likely going to be on a desktop computer with (in theory) a desktop sized monitor. 因此,如果用户正在使用旧的IE浏览器查看网站,则很可能是在装有理论上桌面大小的监视器的台式计算机上。 Which means they'd see the base grid defaults of the css files anyway. 这意味着他们无论如何都会看到css文件的基本网格默认设置。 Which makes me think there can't be a huge need to try and support someone, who for whatever reason, has an old version of IE with a handheld sized screen. 这使我认为,无需出于任何原因而尝试并支持具有旧版IE且具有手持式屏幕的IE的需求非常巨大。

But at the same time, there must be a need for these Javascript files or they wouldn't be used by so many sites. 但与此同时,必须有这些Javascript文件,否则它们将不会被那么多站点使用。 The only reason I can see the need for them is for the browsers that don't read media queries, that they may cause problems when trying to read the media query and throw errors or inconsistencies in the layout. 我看到需要它们的唯一原因是不读取媒体查询的浏览器,它们可能会在尝试读取媒体查询时引起问题,并在布局中引发错误或不一致。

I was just wondering if someone could shed some light on to it and help give me a better understanding of why they should be used, I'm not closed from the idea of using them I just don't see the need, but it is probably because I am missing something. 我只是想知道是否有人可以阐明它,并帮助我更好地理解为什么应该使用它们,我对使用它们的想法并没有感到满意,但我只是没有看到需要,而是可能是因为我缺少了一些东西。

Personally, I've never tried to replicate a mediaquery in a browser that doesn't support it. 就个人而言,我从未尝试过在不支持该查询的浏览器中复制它。 In cases where I've had to support a non-css3 browsers on a site that has mediaqueries, I've always just fixed the width for the non-css3 version via a css conditional comment, because as you say, a phone or tablet is likely to have support. 如果必须在有媒体查询的网站上支持非CSS 3浏览器,我总是通过CSS条件注释来固定非CSS 3版本的宽度,因为正如您所说,电话或平板电脑可能会得到支持。

However, media queries aren't only helpful for creating mobile friendly designs, but also fluid designs that are perhaps made for a 1200px or even 1600px wide display but still need to be workable on a 1024px screen. 但是,媒体查询不仅有助于创建移动友好的设计,而且还可以进行流畅的设计,这些设计可能适用于1200px甚至1600px宽的显示屏,但仍需要在1024px屏幕上可用。 In this example, it's more likely that an old browser is being run on a smaller screen, so a media query would be really useful. 在此示例中,很可能旧的浏览器正在较小的屏幕上运行,因此媒体查询将非常有用。 In this example one of these libraries would be useful. 在此示例中,这些库之一将很有用。

Hope that helps. 希望能有所帮助。

I build a lot of websites for fortune 100 clients. 我为财富100强的客户建立了许多网站。 For the most part, we don't use these js plugins. 在大多数情况下,我们不使用这些js插件。 It really depends on a few things: 这实际上取决于几件事:

  1. Did you build your css mobile first or desktop first? 您是先构建CSS移动版还是台式机? If mobile first, IE8 and below will see the mobile view (which isn't optimal). 如果首先移动设备,则IE8及更低版本将显示移动设备视图(并非最佳)。 If you build desktop first, they see the desktop view. 如果您先构建桌面,他们将看到桌面视图。
  2. How much funkiness can you support? 您能支持多少时髦? Most clients want it to work in ie8 but that's about it. 大多数客户希望它在ie8中工作,仅此而已。 Most everybody has completely abandoned ie7 and below. 大多数人都完全放弃了ie7及以下版本。 The average website isn't perfect in ie8. 在ie8中,普通网站并不完美。 So long as the end user can get the content, that's usually all that matters with regards to ie8. 只要最终用户可以获取内容,通常这就是ie8的所有重要事项。 Some clients may completely disagree though. 有些客户可能会完全不同意。
  3. Do you have a compelling reason to use it or to avoid it? 您是否有充分的理由使用或避免使用它? This is usually the most important one. 这通常是最重要的。 Pull up the site in ie8 and if not having media queries completely breaks it, try it with respond.js and see how it looks. 在ie8中拉起该站点,如果没有媒体查询将其完全破坏,请使用response.js进行尝试,并查看其外观。

It's incredibly simple to include it and if you use conditional statements, it won't even attempt to be loaded on newer browsers. 包含它非常简单,如果使用条件语句,它甚至不会尝试在较新的浏览器中加载。

<!--[if lt IE 9]>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
<!-- <![endif] -->

Basically, just look at it on a per site basis. 基本上,仅按每个站点进行查看。 If it helps, use it. 如果有帮助,请使用它。 If it doesn't, ignore it. 如果不是,请忽略它。

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

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