【发布时间】:2016-04-21 09:54:25
【问题描述】:
我有一些媒体查询对我来说不能正常工作,基本上下面的 css 应该在桌面上显示一些元素,在移动设备上显示一些元素。
例如,在手机/平板电脑上隐藏一些“悬停”元素,但让它们在桌面设备上可见。
@media (max-width: 1024px){
.desktop_only {
display: none !important;
}
}
@media (min-width: 1025px){
.mobile_only {
display: none !important;
}
}
在 CSS4 中,我们将能够使用“指针”功能更轻松地做到这一点:https://drafts.csswg.org/mediaqueries/#pointer 但我不能使用它,因为那是未来,尚未实现。
我尝试使用:
<script>
document.documentElement.className +=
(("ontouchstart" in document.documentElement) ? ' touch' : ' no-touch');
</script>
我也尝试过使用 Modernizer,它应该也添加了触摸/非触摸
与:
@media (max-width: 1024px){
.touch .desktop_only {
display: none !important;
}
}
@media (min-width: 1025px){
.no-touch .mobile_only {
display: none !important;
}
}
但它不起作用,它没有正确隐藏元素。我也试过没有@media (xxxx: 102x) 部分,但这也不起作用。我也尝试过切换触摸和非触摸,以防万一我弄错了。
我无法使用 css:-moz-system-metric(touch-enabled),因为许多浏览器不支持它
不确定如何使用http://detectmobilebrowsers.com/ 做我想做的事
有人知道我做错了什么吗?
【问题讨论】:
-
尝试
@media only screen and (max-width:1025px)以及在<head>某处包含<meta name="viewport" width=device-width">。 -
你试过验证你的 CSS 吗?有时,前面元素中某处缺少括号会弄乱它下面的任何代码。
-
@Brian css 都是有效的,我已经在页面的多个区域尝试过 css,它仍然无法正常工作。
标签: css responsive-design responsive