简体   繁体   English

jQuery下拉悬停菜单

[英]jQuery Drop Down Hover Menu

I'm new to jQuery, I was hoping you guys could help me. 我是jQuery的新手,我希望你们能帮助我。 I'm trying to make a hover dropdown menu, but it's extremely buggy. 我正在尝试制作一个悬停下拉菜单,但它非常错误。 Can you help me clean up my Javascript? 你能帮我清理一下我的Javascript吗? Look at my code please. 请看我的代码。

http://jsdo.it/mretchin/4Ewk http://jsdo.it/mretchin/4Ewk

It doesn't work on jsdo.it for whatever reason, but it works in Komodo Edit. 它无论出于什么原因都不适用于jsdo.it,但它适用于Komodo Edit。

Try out the code yourself if you really want to, the problem is mainly the Javascript. 如果你真的想自己试试代码,问题主要是Javascript。 Can you help me make it so that when the user hovers over img.menu_class, ul.file_menu drops down, and then, if I wanted, I could hover over #something in ul and it would drop out horizantally, not vertically. 你可以帮助我做到这一点,当用户将鼠标悬停在img.menu_class上时,ul.file_menu会下降,然后,如果我愿意,我可以将鼠标悬停在ul中的#something上,它会在水平上掉落而不是垂直掉落。

Thanks for helping! 谢谢你的帮助! I appreciate it! 我很感激!

Should I just give up and make it work in CSS? 我应该放弃并让它在CSS中运行吗?

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');
        }
    );
});

For anyone who finds this in the future Aram's answer can be shortened with .slideToggle() to handle both up and down. 对于任何在将来发现这一点的人来说,Aram的答案可以通过.slideToggle()缩短来处理上下。

Here's the modified fiddle 这是修改过的小提琴

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

If you have a sub-menu set to display: none; 如果你有一个子菜单设置为display: none; it will trigger it also, so what you'll want to do is set it to block , then add something like this 它也会触发它,所以你要做的是将它设置为block ,然后添加这样的东西

var subMenu = $('li.hoverli > ul > li');

subMenu.hover(function () {
            $(this).find("ul").slideToggle(200);
        });

And place it right below your first slideToggle. 并将其放在第一个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);
    });
});

Not sure if you care but you want to make sure that you run the .stop() method that way the animations dont build themselves up and run over and over. 不确定你是否关心,但你想确保你运行.stop()方法,动画不会自行构建并反复运行。 Here's an example 这是一个例子

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

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

});

Use the finish function in jQuery to prevent the bug where you rapidly hover your mouse over the menu and out of the menu. 使用jQuery中的finish函数来防止将鼠标快速悬停在菜单上并退出菜单的错误。 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');
          }
    );
});

Aram Mkrtchyan's answer was almost there for me. Aram Mkrtchyan的答案几乎就在我身边。 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/ 这是我的意思的一个例子,我在他的菜单下面添加了一个div: 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 我使用div而不是列表和列表项(我发现它更容易使用,并且更灵活)和jQuery版本1.9.1提交此更新的答案

here is link to jFiddle: http://jsfiddle.net/4jxph/3423/ 这里是jFiddle的链接: http//jsfiddle.net/4jxph/3423/

Here is the code: 这是代码:

--------------- HTML: --------------- 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: --------------- 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()): --------------- jQuery(放在document.ready或pageLoad()中):

$("#hoverli").hover(
    function () {
        $('#actions_menu').finish().slideDown('fast');
    },
    function () {
        $('#actions_menu').finish().slideUp('fast');
    }
);

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. 如果你给div你的类'文件菜单'一个绝对的位置那么它应该停止影响它前面的任何元素,因为你将它从正常流程中取出。

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 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 HTML

<select class="selectpicker" id="heightMenu" onmouseover="DropList('heightMenu')" onmouseout="this.size=1;" size="1">
    <option value="0">Any height</option>
    etc.
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM