最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect if Hover Exists or Is Available - Stack Overflow

programmeradmin3浏览0评论

Everyone has been pushing towards feature detection for a long time. I'd like to detect if a visitor's browser supports the :hover pseudo class. It's my understanding there are enough, if not most, mobile devices that do not support hovering, so I'd like to gear my event listeners accordingly. But without mobile detection, I'm unsure how to accomplish this, and I've not found anything via Google or SO thus far.

Perhaps something similar to question #8981463

$(function() {
  var canHover = $(document).is(":hover");
});

I won't be able to test this on a mobile device 'till next week.

Thoughts?

Everyone has been pushing towards feature detection for a long time. I'd like to detect if a visitor's browser supports the :hover pseudo class. It's my understanding there are enough, if not most, mobile devices that do not support hovering, so I'd like to gear my event listeners accordingly. But without mobile detection, I'm unsure how to accomplish this, and I've not found anything via Google or SO thus far.

Perhaps something similar to question #8981463

$(function() {
  var canHover = $(document).is(":hover");
});

I won't be able to test this on a mobile device 'till next week.

Thoughts?

Share Improve this question edited May 23, 2017 at 12:31 CommunityBot 11 silver badge asked May 9, 2014 at 5:41 Keith DCKeith DC 6732 gold badges9 silver badges25 bronze badges 6
  • 1 I'm not sure if this will help, but, I'll leave them all here for you to look over: api.jquerymobile.com/vmouseover, izilla.com.au/git/jquery.izilla.touchMenuHover.html, prowebdesign.ro/how-to-deal-with-hover-on-touch-screen-devices, forum.jquery.com/topic/…, – adamj Commented May 9, 2014 at 5:50
  • Again: why do you want to detect the hover-ability? In general to tell if it's a mobile device? To disable hovering for mobile devices? – try-catch-finally Commented May 9, 2014 at 5:57
  • 1 Hope this makes sense, but I have some small aesthetic events firing on hover over a clickable (menu) button. But my brother let me know their audience is primarily mobile-based. I can adjust the code to suit mobile better, but then it becomes a little strange behavior for non-mobiles, such as having to click the button twice to activate it, or click the button to get a dropdown menu. If I detect the hover-ability, I can still allow non-mobile users to hover, which is more expected behavior. – Keith DC Commented May 9, 2014 at 6:03
  • 1 Sounds like the hover will show some animation (on or beside the button). Or do you show kind of a submenu? You might completely change the usability ob the website for mobile users as they have completely different needs (not wasting space, distraction free content and navigation, save battery, short loadtimes on slower networks, ...). For example, an animation that's only for an aestetic reason might be uninteresting (if the website is not all about that animation). – try-catch-finally Commented May 9, 2014 at 6:22
  • I asked something simular a few months ago, there is a plugin – AME Commented May 9, 2014 at 6:57
 |  Show 1 more comment

3 Answers 3

Reset to default 14

There is now a well-supported media query to detect whether hover is supported: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/hover

Which you can use with Window.matchMedia: https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia

Then you would use:

if (window.matchMedia( "(hover: none)" ).matches) {
   // hover unavailable
}

There is no is(':hover'), you can't detect CSS pseudo classes with javascript as they are not part of the DOM.

You can however detect certain events that are only available on touch screens and act accordingly, modernizer does something like this

if ('ontouchstart' in document.documentElement) {
  document.documentElement.classList.add('touch');
} else {
  document.documentElement.classList.add('no-touch');
}

where it adds classes to the <html> element that tells you wether or not the device has a touch screen or not, so you can do something like this in CSS

.no-touch .element:hover {color: red;} // for users with a mouse
.touch .element {color: blue;} // for touch devices

Couldn't sleep, and may have figured it out, at least perhaps for my particular situation. I use sprites on these buttons and I'm already doing sprite position checks elsewhere. My thought is when the button is clicked, prior to firing, it can also do the sprite position check. If the sprite hasn't been re-positioned for the clicked button, it would seem it is not a hover-able device, and I can treat the click as the 1st of a 2-part activation. I read earlier today that some mobile devices do that already, in that they treat the 1st click as the hover.

My sprite positioning logic as as such, and perhaps could be called prior to the button's click-fire event:

if ($(elem).css('background-position').indexOf('76px') >= 0 ) {
发布评论

评论列表(0)

  1. 暂无评论