jQuery Drop Down Hover Menu

前端 未结 7 855
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 06:44

I\'m new to jQuery, I was hoping you guys could help me. I\'m trying to make a hover dropdown menu, but it\'s extremely buggy. Can you help me clean up my Javascript? Loo

相关标签:
7条回答
  • 2020-12-29 07:19

    Aram Mkrtchyan's answer was almost there for me. Problem with his was if you add anything below the menu then it gets all screwy. Here is an example of what I mean, I added a div below his menu: http://jsfiddle.net/4jxph/3418/

    I am submitting this updated answer using div instead of lists and list items (which I find much easier to work with, and way more flexible) and jQuery version 1.9.1

    here is link to jFiddle: http://jsfiddle.net/4jxph/3423/

    Here is the code:

    --------------- HTML:

    <div id="divMenuWrapper1" class="divMenuWrapper1">
        <div id="hoverli">
            <div class="lbtn">
                Actions
            </div>
            <div id="actions_menu" class="file_menu">
                <div><a href="#file">File</a></div>
                <div><a href="#edit">Edit</a></div>
                <div><a href="#view">View</a></div>
                <hr />
                <div><a href="#insert">Insert</a></div>
                <div><a href="#modify">Modify</a></div>
                <div><a href="#control">Control</a></div>
                <div><a href="#debug">Debug</a></div>
                <div><a href="#window">Window</a></div>
                <div><a href="#help">Help</a></div>
            </div>
        </div>
    </div>
    
    <div>
        testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu testing content below menu 
    </div>
    

    --------------- Css:

    .lbtn
    {
        display:inline-block;
        cursor:pointer;
        height:20px;
        background-color:silver;
        background-repeat:repeat-x;      
        border:1px solid black; /* dark navy blue */ 
        text-decoration:none;
        font-size:11pt;
        text-align:center;
        line-height:20px;
        padding:0px 10px 0px 10px;
    }
    
    .divMenuWrapper1
    {
        height: 25px;
        width: 75px;
    }
    
    .file_menu 
    {
        display:none;
        width:250px;
        border: 1px solid #1c1c1c;
        background-color: white;
        position:relative;
        z-index:100000;
    }
    
    .file_menu div 
    {
        background-color: white;
        font-size:10pt;
    }
    
    .file_menu div a 
    {
        color:gray; 
        text-decoration:none; 
        padding:3px; 
        padding-left:15px;
        display:block;
    }
    
    .file_menu div a:hover 
    {
        padding:3px;
        padding-left:15px;
        text-decoration:underline;
        color: black;
    }
    

    --------------- jQuery (to be placed in document.ready or pageLoad()):

    $("#hoverli").hover(
        function () {
            $('#actions_menu').finish().slideDown('fast');
        },
        function () {
            $('#actions_menu').finish().slideUp('fast');
        }
    );
    
    0 讨论(0)
  • 2020-12-29 07:22

    I know this is probably a bit late but just found this thread saw that your question above about things below the menu 'getting a bit screwy' was unanswered.

    If you give your div with the class 'file menu' a position of absolute then it should cease to affect any elements ahead of it as you will have taken it out of the normal flow.

    0 讨论(0)
  • 2020-12-29 07:24

    You can do something like this:

    $(document).ready(function() {
        $(".hoverli").hover(
            function() {
                $('ul.file_menu').stop(true, true).slideDown('medium');
            },
            function() {
                $('ul.file_menu').stop(true, true).slideUp('medium');
            }
        });
    });
    

    And here an example with sub-menus:

    $(document).ready(function() {
        $(".hoverli").hover(
            function() {
                $('ul.file_menu').slideDown('medium');
            },
            function() {
                $('ul.file_menu').slideUp('medium');
            }
        );
    
        $(".file_menu li").hover(
            function() {
                $(this).children("ul").slideDown('medium');
            },
            function() {
                $(this).children("ul").slideUp('medium');
            }
        );
    });
    
    0 讨论(0)
  • 2020-12-29 07:24

    Use the finish function in jQuery to prevent the bug where you rapidly hover your mouse over the menu and out of the menu. Finish is better than the stop function previously suggested.

    $(document).ready(
        function () {
            $(".hoverli").hover(
              function () {
                 $('ul.file_menu').finish().slideDown('medium');
              }, 
              function () {
                 $('ul.file_menu').finish().slideUp('medium');
              }
        );
    });
    
    0 讨论(0)
  • 2020-12-29 07:28

    To get a select box to open on hover to the exact height required by its contents, figure out how many elements there are:

    JavaScript

    function DropList(idval) {
        //
        // fully opens a dropdown window for a select box on hover
        //
        var numOptgroups = document.getElementById(idval).getElementsByTagName('optgroup').length;
        var numOptions   = document.getElementById(idval).getElementsByTagName('option').length;
        document.getElementById(idval).size = numOptgroups + numOptions;
    }
    

    HTML

    <select class="selectpicker" id="heightMenu" onmouseover="DropList('heightMenu')" onmouseout="this.size=1;" size="1">
        <option value="0">Any height</option>
        etc.
    </select>
    
    0 讨论(0)
  • 2020-12-29 07:35

    For anyone who finds this in the future Aram's answer can be shortened with .slideToggle() to handle both up and down.

    Here's the modified fiddle

    http://jsfiddle.net/4jxph/2009/

    If you have a sub-menu set to display: none; it will trigger it also, so what you'll want to do is set it to block, then add something like this

    var subMenu = $('li.hoverli > ul > li');
    
    subMenu.hover(function () {
                $(this).find("ul").slideToggle(200);
            });
    

    And place it right below your first slideToggle. Why don't I just show you?

    $(document).ready(function () {
        $(".hoverli").hover(function () {
         $(this).find('ul').slideToggle('medium');
        });
    
        var subMenu = $('li.hoverli > ul > li');
    
        subMenu.hover(function () {
          $(this).find("ul").slideToggle(200);
        });
    });
    
    0 讨论(0)
提交回复
热议问题