简体   繁体   English

如果将鼠标悬停在div上,则显示菜单

[英]if mouse on hover of div then display menu

I have the following menu which cascades on hover but i need to add some conditional checks like if the mouse is on hover on the div then keep the menu sliding down. 我有以下菜单,当鼠标悬停在div上时,我需要添加一些条件检查,例如鼠标是否悬停在div上,然后保持菜单向下滑动。

Also if the mouse is hovered on the LI then check them menu down. 另外,如果将鼠标悬停在LI上,则请检查菜单。

As you can see it just slides down and back up once you leave the "div". 如您所见,一旦离开“ div”,它就可以上下滑动。

Im stuck... and have tried for hours searching for if statements etc, i just cant get the syntax correct. 我被困住了...并尝试了数小时来搜索if语句等,我只是无法正确获取语法。

my example 我的例子

Make it a child of the <div> , then it won't cancel the event when you leave it. 将其设为<div>的子项,然后在离开该事件时不会取消该事件。

Also I should note that it's more semantic to make a navigation out of nested lists (such as 另外我还要注意,从嵌套列表(例如,

  • Category 类别
    • Item 项目
    • Item 项目
<ul>
    <li>Category 
        <ul>
            <li>Item</li>
            <li>Item</li>
        </ul>
    </li>
</ul>

Here is a working example 是一个有效的例子

HTML HTML

<div id="leftWrap">
    <div id='accordion'>
        <ul>
           <li><div>Absorption</div>
               <ul style="display: none;">
                   <li>Accessories</a>
                       <ul style="display: none;">
                           <li>AA500AFG</li>
                           <li>AA500F</li>
                           <li>AA500G</li>
                           <li>AA990F</li>
                       </ul>
                   </li>
                   <li>Consumables</li>
                   <li>Products</li>
               </ul>
           </li>
           <li><div>Fluorescence</div>
               <ul style="display: none;">
                   <li>Accessories</li>
                   <li>Consumables</li>
                   <li>Products</li>
               </ul>
           </li>
       </ul>
    </div>
</div>

Javascript/JQuery 使用Javascript / JQuery的

jQuery(document).ready(function() {
    $('#accordion ul > li').hover(function() {
        $(this).children("ul").slideToggle('slow');
    });
});

If you ask me, it gets really messy when you use mousehover/mouseenter for such things. 如果您问我,当您使用mousehover / mouseenter这样的东西时,它会变得非常混乱。 I'd prefer using a click event after the first hover or something, this way the user won't get annoyed by all that movement. 我希望在第一次悬停后使用click事件,这样用户就不会因所有移动而烦恼。

jQuery(document).ready(function() {
    $('#accordion ul:first-child > li').hover(function() {
        $(this).children("ul").slideToggle('slow');
    });

    $('#accordion ul:not(:first-child) > li').click(function(){
        $(this).children("ul").slideToggle('slow');
    });
});

I tried to fiddle in your fiddle, but the markup and css are a lot confusing. 我试图在您的小提琴中摆弄,但是标记和CSS非常令人困惑。

As Rikudo said, you should make the div, its child its much easier to do it that way. 正如Rikudo所说,您应该使div及其子级更容易做到这一点。 I have created a simplest accordion skeleton. 我创建了一个最简单的手风琴骨架。 You can see it here . 你可以在这里看到它。

It does everything you want. 它可以满足您的所有需求。 However for the customizations and others things, I will leave it up to you. 但是,对于自定义和其他方面,我将由您自己决定。

http://jsfiddle.net/dttdB/13/ http://jsfiddle.net/dttdB/13/

You had attached hover to the heading div when the mouse leaves that, the hover effect is lost. 当鼠标离开时,您已经将鼠标悬停在标题div上,悬停效果消失了。

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

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