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)
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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With