0

I'm using the following JQuery code to control some style elements on mouse hover for a drop down menu solution:

$(window).load(function () {

    // On hover show or hide
    $("#menu ul li").hover(function(){
             $(this).children("ul").show();
             $(this).children("ul").css('background-image', 'none');
       },
       function(){
             $(this).children("ul").hide();

    })

    // On hover show or hide
    $("#menu ul ul li, #menu ul ul li a ").hover(function(){
             $(this).css('color', '#666');
       },
       function(){
             $(this).css('color', '#FFF');       
    })

});

See working example:

http://www.youmeusdesign.co.uk/menu_test

I would like to modify this so that I can replace the hover function with a click function. When the client is using a touch interface that does not support the hover functionality. Windows Phone for example. iOS works ok as the device already has some classes to handle hover events.

I've tried modifying my script replacing the .hover with .click but it does not work?

e.g.

$("#menu ul li").click(function(){

Any help would be most appreciated.

1
  • In my opinion there is no other way to detect first if it is a mobile touch device an then set up the menue function. Look here for more information detecting devices stackoverflow.com/questions/12299098/… Commented Mar 24, 2013 at 19:54

3 Answers 3

1

try this one: (tested here: http://jsfiddle.net/d9mPC/)

$("#menu ul li").on("mouseover mouseout tap",function(event){      
    if($(this).children("ul").is(":hidden") && (event.type == "mouseover" || event.type == "tap")){
         $(this).children("ul").show();
         $(this).children("ul").css('background-image', 'none');
   }else if($(this).children("ul").not(":hidden") && (event.type == "mouseout" || event.type == "tap")){
         $(this).children("ul").hide();
   }    
});

// On hover show or hide
$("#menu ul ul li, #menu ul ul li a ").on("mouseover mouseout tap",function(event){        
   if(!$.hasData($(this),"hovered") && (event.type=="mouseover" || event.type=="tap")){
         $(this).css('color', '#666');
         $.data((this),"hovered",true);
   }else if($.hasData((this),"hovered") && (event.type=="mouseout" || event.type=="tap")){
         $(this).css('color', '#FFF');
       $.removeData((this),"hovered");
   }
});
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks for your feedback. I've tried your example but no luck unfortunately. See modified example: youmeusdesign.co.uk/menu_test2
Modified this code again and provided a jsfiddle. try it again.
I've modified that recently. this code is a previous version.
Thanks for your continued support but I still can't get it working. Getting no response when I click on the menu. youmeusdesign.co.uk/menu_test4
I thought you wanted hover on mouse enabled devices and tab on mobile devices?
|
0

You can probably do both at the same time.

What I would do is allow a click or hover to show the item (track a click state so that you aren't closing it immediately when hover opens it).

Then you will do achieve a few things

  • You won't have to sniff the device
  • You will give people the ability to click toggle a menu item

The alternative is to either sniff the device type or conditionally load different javascript files for each device (sniff device on the server side) which could lead to maintenance headaches.

I would recommend just implementing both for every device.

Comments

0

I would like to modify this so that I can replace the hover function with a click function.

$("#menu").on('click', 'ul li', function(){
         $("#menu li ul").hide().find('li a').css('color', '#FFF');
         $("ul", this).show().children('li a').css('color', '#666');
});

4 Comments

With this workaround the click function will be also available for non-touch users, right? If this doesn't matters this will work of course without detecting devices first.
click has some delay on touch devices, but yes. Will work here and there.
Thanks yes it could be implemented to work on both touch and non touch users. I've tried your solution and the drop down menu opens on click but I'm unable to click on any sub-menu items. See example youmeusdesign.co.uk/menu_test3
Sorry not sure what you mean by dynamically populating the drop down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.