简体   繁体   English

媒体查询以狭窄的台式机而非移动设备为目标

[英]media query to target narrow desktop and not mobile

Let's say I have the following code: 假设我有以下代码:

@media (max-width: 1024px) {
   div {
      color: green;
   }
}

Is there any way to add a condition to the media query to exclude mobile devices? 有什么方法可以向媒体查询添加条件以排除移动设备?

... or will I have to override this explicitly within the media queries containing the mobile css code? ...还是我必须在包含移动CSS代码的媒体查询中显式覆盖此内容?

Increasing specificity in the mobile code is not what I'm after because the properties which I'm setting in the desktop code may not be set at all in the mobile code. 我不希望在移动代码中增加专用性,因为在桌面代码中设置的属性可能根本没有在移动代码中设置。

I also realize that with the mobile first approach this problem wouldn't even arise. 我还意识到,采用移动优先的方法甚至不会出现此问题。 (But this approach isn't used in my case) (但是我使用这种方法)

So is the only way to do this by overriding the desktop css rules in the mobile rules? 那么,通过在移动规则中覆盖桌面CSS规则来做到这一点的唯一方法是吗?

Edit : So it seems you can't do this with just CSS... that being the case, what what be a good javascript solution? 编辑 :所以看来您不能只使用CSS来做到这一点...既然如此,什么是好的javascript解决方案?

I just realized that I'm using Modernizr anyway. 我只是意识到我仍然在使用Modernizr So I think the best solution for me would be to use Modernizr to detect touch devices. 因此,我认为对我来说最好的解决方案是使用Modernizr来检测触摸设备。 Like so: 像这样:

1) First check touch events 1)首先检查触摸事件

在此处输入图片说明

2) Click the generate button 2)点击生成按钮

Just to demonstrate this, I copied the generated javascript into a jsbin 为了说明这一点,我将生成的javascript复制到了jsbin中

Now right-click and inspect the html element. 现在,右键单击并检查html元素。 Modernizr adds the class 'no-touch' on desktop and the class 'touch' for phones and tablets (touch devices) Modernizr在桌面上添加了“非触摸”类,并在手机和平​​板电脑(触摸设备)上添加了“ touch”类

So now I can use code like this: 所以现在我可以使用如下代码:

@media (max-width: 1024px) {
   .no-touch {
      div {
         color: green;
      }
   }
}

you can check this out. 您可以检查一下。 This question states a similar problem like yourself. 这个问题说明了类似您自己的问题。 It might help. 这可能会有所帮助。 Check the comments and answer. 检查评论并回答。 Media query to differentiate desktop from iPad in landscape mode . 媒体查询可在横向模式下区分台式机和iPad Also check this article for media queries http://css-tricks.com/snippets/css/media-queries-for-standard-devices/ . 另外,请查看这篇文章以获取媒体查询http://css-tricks.com/snippets/css/media-queries-for-standard-devices/ You can use min-width and max-width together. 您可以同时使用min-widthmax-width But landscape mode for iPads will come into it. 但是iPad的横向模式将会出现。

I started with using keyword Not - to reverse the logic with @media query not handheld and (max-width)... in media queries, but iPhone, Nokia etc don't consider themselves as handheld so that solution was not working. 我开始使用关键字Not -扭转与逻辑@media query not handheld and (max-width)...在媒体的质疑,但iPhone,诺基亚等不认为自己是手持式,这样的解决方案是行不通的。

Finally I came up with this solution. 最后,我想到了这个解决方案。

Using 运用

@media only screen and (orientation : portrait){ 
/* codes here to target mobile/tablet devices, as they are held in portrait mode, 
   unless user changes to landscape mode */
}

@media only screen and (orientation : landscape) and (max-width: 1024px){ 
/* codes here to target desktops/laptops devices, as they are in landscape mode, 
   unless user changes to portrait mode which rarely someone does by a multiple key combinations */
}

Note : 注意 :

This solution fails if : 如果出现以下情况,此解决方案将失败:

  • Your website/app can be used for both landscape and portrait mode on mobile devices and user changes the orientation by rotating the mobile/tablet. 您的网站/应用程序可以在移动设备上用于横向和纵向模式,并且用户可以通过旋转移动设备/平板电脑来更改方向。

(remember you can always restrict user for portrait mode only) (请记住,您始终只能将用户限制为纵向模式)

  • If a user on desktop/laptop changes screen orientation 如果台式机/笔记本电脑上的用户更改了屏幕方向

(which rarely one does without any necessary requirement.) (很少有人会在没有任何必要要求的情况下这样做。)

@Gaurav, Please go over this link. @Gaurav,请查看此链接。 Very usefull information. 非常有用的信息。

http://webdesignerwall.com/tutorials/responsive-design-with-css3-media-queries http://webdesignerwall.com/tutorials/responsive-design-with-css3-media-queries

http://foundation.zurb.com/docs/media-queries.html http://foundation.zurb.com/docs/media-queries.html

And apart from that if you are using only width not Device-width then it will only target the desktop browser. 除此之外,如果您仅使用宽度而不是设备宽度,那么它将仅针对桌面浏览器。

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

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