📌

スマホ・タブレットの:hover無効化対応の最適解

2023/04/06に公開

var touch = 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
if(touch) {
try {
for (var si in document.styleSheets) {
var styleSheet = document.styleSheets[si];
if (!styleSheet.rules) continue;

  for (var ri = styleSheet.rules.length - 1; ri >= 0; ri--) {
    if (!styleSheet.rules[ri].selectorText) continue;

    if (styleSheet.rules[ri].selectorText.match(':hover')) {
      styleSheet.deleteRule(ri);
    }
  }
}

} catch (ex) {}
}

Discussion