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

javascript - How to fix this JQuery dropdown menu? - Stack Overflow

programmeradmin1浏览0评论

I tried to create a dropdown menu in JQuery, but it's proving quite difficult.

My code is here:

  $(document).ready(function(){

        $('ul li').mouseover(function()
        {
              $(this).children("ul").show();
        });
        $('ul li ul').mouseover(function()
        {
              $('ul li ul').show();
        }).mouseout(function(){
              $('ul li ul').hide();
        });

  });

Basically I want to hover over a list item and the sub ul to drop down, then I can hover over the list items and If the mouse goes off of the sub nav, the menu hides again.

thanks, Keith

UPDATE: I removed the border from the CSS and it works fine, so it appears the mouseout is triggered when I hover over the 1px border, quite weird.

I tried to create a dropdown menu in JQuery, but it's proving quite difficult.

My code is here:

  $(document).ready(function(){

        $('ul li').mouseover(function()
        {
              $(this).children("ul").show();
        });
        $('ul li ul').mouseover(function()
        {
              $('ul li ul').show();
        }).mouseout(function(){
              $('ul li ul').hide();
        });

  });

Basically I want to hover over a list item and the sub ul to drop down, then I can hover over the list items and If the mouse goes off of the sub nav, the menu hides again.

thanks, Keith

UPDATE: I removed the border from the CSS and it works fine, so it appears the mouseout is triggered when I hover over the 1px border, quite weird.

Share Improve this question edited Dec 1, 2011 at 15:45 user1228 asked Apr 2, 2009 at 0:25 KeithKeith 26.5k36 gold badges98 silver badges129 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

you should use jQuery's hover() function as it avoids all sorta browser specific issues ..

Without a lick of testing I'd imagine the code would look something more like:

    $('.clearfix li').hover(function() {
          $('ul', this).show();
        }, function() {
          $('ul', this).hide();
        });

Are you aware of superfish? It is menu jQuery plug-in with excellent cross-browser support. It definitely doesn't have the problem you are experiencing. I haven't checked the source code, but the key difference is that it adds a delay on mouseout. This means that an action isn't triggered, unless the position of the cursor is steady for some time (default delay is 800ms). This will solve your problem and is also a good thing to implement, as it will make your menu more user-friendly.

The problem is, that the border is "outside" the box. It helps if you just move up the dropdown menu by 1 pixel so it overlaps the menu bar by that 1 pixel.

Simply change the top position of the dropdown menu in your CSS from 30px to 29px like that:

        ul li ul {
            border: none;
            position: absolute;
            top: 29px; /* <-- was 30px */

It works fine on my version of Firefox 1.5.0.1 Perhaps you don't have the latest version. I run an image gallery myself, but I don't think type of layout would artı work very well for me as I have descriptions for my images and some of the images are quite large. I don't know, it might be worth experimenting with.

发布评论

评论列表(0)

  1. 暂无评论