简体   繁体   English

悬停另一个div时如何保持div可见

[英]How to keep div visible when hovering another div

I found a few similar threads here after a good while of searching, but none of them was in the same situation as me, so i decided to ask. 经过一段时间的搜索,我在这里找到了一些类似的线程,但是没有一个线程与我处于同一状况,所以我决定问一下。

I got this drop-down menu that gets the height 460px when i hover over a tab, and loses the height when unhovered. 我有一个下拉菜单,当我将鼠标悬停在选项卡上时,高度为460px,而当未悬停时会丢失高度。 I need the menu to stay there even when the mouse is moved from the tab over to the menu. 即使鼠标从选项卡移到菜单上,我也需要菜单停留在那儿。

Like this: http://jsfiddle.net/muo4ypvc/ 像这样: http : //jsfiddle.net/muo4ypvc/

I can accomplish this by setting the menu position to relative, however i need the position to be absolute in order to adjust the z-index. 我可以通过将菜单位置设置为相对来完成此操作,但是为了调整z索引,我需要将该位置设置为绝对位置。

When i try to set the position to absolute, the menu won't keep the 460px height on hover, it only has the height when the tab is hovered. 当我尝试将位置设置为绝对时,菜单不会在悬停时保持460px的高度,而是仅在将选项卡悬停时才具有该高度。 I've also tried some other things like wrapping all the html in a container, and put that as the mouseleave element, but with no success. 我还尝试了其他一些操作,例如将所有html包装在容器中,并将其作为mouseleave元素,但没有成功。 How can i make it stay on both tab and menu hover? 如何使它停留在选项卡和菜单悬停上?

html html

 <div id="tab">

        <a id="tab">Categories</a>

            <div id="menuDropdown">

                <div id="innerDropdown">

                    <!-- menu content -->

                </div>
            </div>
        </div>

js js

$(document).ready(function() {

    var inner = $('#innerDropdown')
    var rolldown = $('#menuDropdown');

    $('#tab').mouseenter(function() {
        rolldown.toggleClass('show');

        if(rolldown.hasClass('show'));
            setTimeout(showInner, 130); /* waits 'til drop-down animation is finished */

        function showInner(){
        inner.toggleClass('show');
        }
    });

$('#tab').mouseleave(function() {
        inner.toggleClass('show');
        rolldown.toggleClass('show');
    });
}); 

css 的CSS

#menuDropdown {
  position: absolute;
  transition: (some long ass transition code);  
}

#menuDropdown.show {
    height: 460px;
}

#menuDropdown.hide {
    height: 0px;
}

#innerDropdown.show {
    display: block;
    animation: fadein .15s;
}

I wrapped your html into another div to show that the wrapper keeps its height, but I'm not sure I understand what you're trying to do and what doesn't work. 我将您的html包装到另一个div中,以表明包装器保持其高度,但是我不确定我是否了解您要执行的操作和无效的操作。 I basically just added position: absolute : 我基本上只是添加position: absolute

.description {
    position: absolute;
}

See demo: http://jsfiddle.net/4tb29u6h/1/ 观看演示: http : //jsfiddle.net/4tb29u6h/1/

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

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