Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i fix :hover on iPhone if there is no <a> element?

How do i fix :hover on iPhone if there is no <a> element?

I'm using a <li> element and the iPhone doesn't open my sub-menu when i tab it.

Example html:

<ul>
  <li>Menu item (no <a> element)
    <ul>
      <li><a href="#">Menu item</a><li>
    </ul>
  <li>
</ul>

I answered a JavaScript way to fix this, but i want to know if there are different ways to fix this (maybe a better one)

like image 701
Maarten Kuilman Avatar asked Apr 06 '12 08:04

Maarten Kuilman


People also ask

Does hover work on iOS?

Hover works by hovering the mouse on an object, without clicking at it. This can't be done on neither Android nor iOS. If you want to test your code, test it on a computer.

What happens to hover on mobile?

There are no mouse devices on mobile, so users don't have the luxury of using hover effects. Using a hover effect on mobile apps causes buttons to stay stuck in the hovered state when tapped. This stickiness not only confuses users, but it frustrates them when they're forced to double-tap buttons to initiate an action.

How do you use mouseover on Iphone?

The usual method cited for viewing the mouse-over text of an image on an iOS device is to press and hold on the image. This works for most images with mouse-over texts. For example, pressing down on the image in.

Does hover work on mobile devices?

However, one thing that Hover doesn't work with is mobile devices. If you're looking to use Hover on a mobile device, you're out of luck. The tool doesn't work with any touch-based devices, so you'll need to find another way to create your site's content.


1 Answers

You can fix this by using JavaScript. The following script will add hover as class to the <li> element:

<script type="text/javascript">
$("li").hover(
  function () {
    $(this).addClass("hover");
  },
  function () {
    $(this).removeClass("hover");
  }
);  
</script>

Just add li.hover where you got li:hover in your CSS like this:

This:

ul li:hover ul{
    display:block;
}

Will be:

ul li:hover ul,
ul li.hover ul,{
    display:block;
}

jQuery Documentation:
http://api.jquery.com/hover/
http://api.jquery.com/addClass/

like image 84
Maarten Kuilman Avatar answered Sep 29 '22 12:09

Maarten Kuilman