如果用户通过触摸设备访问我们的网站,我想忽略所有:hover CSS声明。因为:hover CSS没有意义,如果平板电脑在点击/点击时触发它,它甚至会令人不安,因为它可能会一直停留到元素失去焦点。说实话,我不知道为什么触屏设备觉得有必要触发:悬停在第一位——但这是现实,所以这个问题也是现实。
a:hover {
color:blue;
border-color:green;
/* etc. > ignore all at once for touch devices */
}
所以,(如何)我可以删除/忽略所有CSS:悬停声明在一次(而不必知道每一个)有他们声明后触摸设备?
试试这个(我在这个例子中使用background和background-color):
var ClickEventType = ((document.ontouchstart !== null) ? 'click' : 'touchstart');
if (ClickEventType == 'touchstart') {
$('a').each(function() { // save original..
var back_color = $(this).css('background-color');
var background = $(this).css('background');
$(this).attr('data-back_color', back_color);
$(this).attr('data-background', background);
});
$('a').on('touchend', function(e) { // overwrite with original style..
var background = $(this).attr('data-background');
var back_color = $(this).attr('data-back_color');
if (back_color != undefined) {
$(this).css({'background-color': back_color});
}
if (background != undefined) {
$(this).css({'background': background});
}
}).on('touchstart', function(e) { // clear added stlye="" elements..
$(this).css({'background': '', 'background-color': ''});
});
}
css:
a {
-webkit-touch-callout: none;
-webkit-tap-highlight-color: transparent;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
试试这个简单的2019 jquery解决方案,虽然它已经有一段时间了;
将这个插件添加到head:
src = " https://code.jquery.com/ui/1.12.0/jquery-ui.min.js "
将此添加到js:
美元(“*”)。On ("touchend",函数(e) {$(this).focus();});//应用于所有元素
一些建议的变化是:
$(":输入:复选框,")。On ("touchend",函数(e) {(this).focus);});/ /指定元素
美元(“*”)。On ("click, touchend",函数(e) {$(this).focus();});//包含点击事件
Css: body{游标:指针;} //触摸任意位置结束焦点
笔记
将plugin放在bootstrap.js之前,以避免影响工具提示
仅在使用Safari或Chrome的iphone XR ios 12.1.12和ipad 3 ios 9.3.5上测试。
引用:
https://code.jquery.com/ui/
https://api.jquery.com/category/selectors/jquery-selector-extensions/