Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari Mobile: Toggle contents on touch

I am adapting a website in order to make it feel native on the iPad.

This website has navigation that shows a drop-down with the sub-navigation on hover. This works like a charm on the iPad. When you touch it the subnav, it opens and closes again when you click/touch somewhere else.

Now i have the requirement to make it close again when the navigation point is touched again.

I was thinking, i could just set the pointer-events:none on hover & active for the iPad, but this makes the sub-navigation flicker and it does not work...

i have also tried to cover the navigation point with element set with the before selector and setting its pointer events to none, but this does not work...

Any idea, how i could solve this problem using CSS only. (I can not modify the HTML nor the JS)

PS: you can reproduce this on www.macprime.ch for example... (click the main navigation on the top, and try to close the dropdown again)

edit ok i tried almost everything that was possible with CSS only. I don't think its possible. If anyone can tell me why, he/she will get the bounty reward.

like image 849
meo Avatar asked Oct 04 '11 11:10

meo


1 Answers

You could have a second transparent element that appears above the one you tapped. That way, when the user taps again, they will be selecting the other element and the first will lose its hover status:

<div class="blocker" onclick="void()"></div>
<div class="menuItem" onclick="void()"></div>

<style>
    .blocker, .menuItem {
        /* use the same position, width, height, etc */
    }
    .menuItem {
        /* make it look pretty */
        z-index: 100;
    }
    .blocker {
        z-index: 99;
    }
    .menuItem:hover {
        z-index: 98;
    }
</style>

Of course, this will have a negative effect on the desktop, so you will want to do something like:

<style>
    .blocker { display: none; }
    .touchevents .blocker { display: block; }
</style>
<script>
    if('ontouchstart' in document)
        document.body.className += ' touchevents';
</script>

UPDATE Added onclick events to make them clickable.

You can see a working demo here: http://fiddle.jshell.net/vfkqS/6/

Unfortunately, I could not find a solution that does not require HTML or JavaScript changes, but I was able to keep them to a minimum.

You would need to make two non-CSS changes total:

  1. Add a JavaScript mechanism for identifying if touch events are supported. See two line example above.
  2. Add one div per menu which is clickable (onclick="void()") and has a unique identifier that can link it to the menu.

You may be able to do those two things with CSS but I'm not sure. Tablet detection would be a little sketchy in CSS and I don't think you can make something that sophisticated with a :before or :after pseudo-selector.

like image 154
Brian Nickel Avatar answered Dec 22 '22 23:12

Brian Nickel