使用Javascript检查触摸设备
我有3个按钮与悬停状态,使一个小的工具提示出现,以描述按钮。它们工作正常,但在触摸屏上,用户点击按钮后,它们不会消失 所以我尝试了一些js脚本来检查一个设备是否是触摸设备。它们几乎可以工作,但当我在IE11上测试时,它也会被检测为触摸设备。Chrome和Firefox不会被误认为是触摸设备 有什么建议吗 她就是我试过的使用Javascript检查触摸设备,javascript,jquery,internet-explorer,modernizr,browser-detection,Javascript,Jquery,Internet Explorer,Modernizr,Browser Detection,我有3个按钮与悬停状态,使一个小的工具提示出现,以描述按钮。它们工作正常,但在触摸屏上,用户点击按钮后,它们不会消失 所以我尝试了一些js脚本来检查一个设备是否是触摸设备。它们几乎可以工作,但当我在IE11上测试时,它也会被检测为触摸设备。Chrome和Firefox不会被误认为是触摸设备 有什么建议吗 她就是我试过的 /***************************** TOUCH DEVICES HOVER FIX START **************************
/*****************************
TOUCH DEVICES HOVER FIX START
****************************/
// http://stackoverflow.com/a/4819886/1814446
function isTouchDevice() {
return 'ontouchstart' in window // works on most browsers
|| 'onmsgesturechange' in window; // works on ie10
};
// http://www.stucox.com/blog/you-cant-detect-a-touchscreen/#poke-it
var hasTouch;
window.addEventListener('touchstart', function setHasTouch () {
hasTouch = true;
// Remove event listener once fired, otherwise it'll kill scrolling
// performance
window.removeEventListener('touchstart', setHasTouch);
}, false);
// https://github.com/Modernizr/Modernizr/blob/master/feature-detects/touchevents.js
define(['Modernizr', 'prefixes', 'testStyles'], function( Modernizr, prefixes, testStyles ) {
// Chrome (desktop) used to lie about its support on this, but that has since been rectified: http://crbug.com/36415
Modernizr.addTest('touchevents', function() {
var bool;
if(('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
bool = true;
} else {
var query = ['@media (',prefixes.join('touch-enabled),('),'heartz',')','{#modernizr{top:9px;position:absolute}}'].join('');
testStyles(query, function( node ) {
bool = node.offsetTop === 9;
});
}
return bool;
});
});
if(bool===true) {
console.log('Touch Device'); //your logic for touch device
jQ( "#btn-1, #btn-2, #btn-3" ).click(function() {
jQ("#btn-1 .tooltip").css('opacity', '0');
jQ("#btn-2 .tooltip").css('opacity', '0');
jQ("#btn-3 .tooltip").css('opacity', '0');
});
}
else {
//your logic for non touch device
}
对于IE10+,您可以使用“window.navigator.msMaxTouchPoints”
示例代码
function isIETouch ()
{
return window.navigator.msMaxTouchPoints == undefined ? false : window.navigator.msMaxTouchPoints;
}
这仍然是不一致的。您是否尝试过=>函数isTouchDevice(){return(('ontouchstart'在窗口中)| | | | window.DocumentTouch&&documentinstanceof DocumentTouch | |(typeof(navigator.msMaxTouchPoints)!=“undefined”&&(navigator.msMaxTouchPoints>0))?true:false;}