禁用移动设备的悬停和活动伪类

时间:2012-05-18 20:51:44

标签: css mobile hover

是否可以仅为移动设备禁用悬停和活动伪类?

我发现了这个

html.touch {
  /* Touch is enabled */
}

html.no-touch {
  /* Touch is disabled */
}

这看起来很整洁。但我无法让它发挥作用。

代码如下,您可以在此处进行测试:http://jsfiddle.net/5qb2J/

<html>
<head>
<style type="text/css">
#button{background:url("http://www.webstuffshare.com/wp-content/uploads/2010/03/button3.jpg") no-repeat 0 0;display:block;width:201px;height:67px;}
#button:hover{background-position:0px -67px;}
#button:active{background-position:0px -134px;}
#button span{position:absolute;top:-99999999em;}
</style>    
</head>
<body>
<a id="button" href="#"><span>this is foo</span></a>
</body>
</html>

修改

我现在正在使用

<?php // detect mobile
$Mobile = FALSE;
if
(strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), "android") ||
strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), "webos") ||
strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), "iphone") ||
strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), "ipod") ||
strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), "ipad") ||
strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), "zune"))
{
$Mobile = TRUE;
}
?>

<?php
If ($Mobile == FALSE)
{
echo <<<escapethis
some html here
escapethis;
}
?>

使用单词escapethis的行不能以空格开头

1 个答案:

答案 0 :(得分:2)

html.touchhtml.no-touch之类的声音正在与modernizr等功能检测库一起使用,它将执行各种功能测试并向html元素添加类根据这些测试的结果。

在这种情况下,您可能希望执行以下操作:

html.no-touch #ElementThatShouldHaveNoHoverEventOnTouchDevices:hover{
    //do your stuff
}

<div id="ElementThatShouldHaveNoHoverEventOnTouchDevices">
    hello
</div>