简体   繁体   English

CSS悬停/鼠标移出过渡?

[英]CSS Hover Out / Mouse Out Transition?

I am having a problem with mouse out transition I have used a few examples from similar questions but had no luck as it is a slightly different case: 我在鼠标移出转换方面遇到问题,我使用了类似问题的一些示例,但运气不佳,因为情况略有不同:

When the page loads I have a menu bar with one of the options triggering a hidden div: 当页面加载时,我有一个菜单栏,其中有一个选项触发了一个隐藏的div:

.top-links div.musictest {
    display: none;
    position:absolute;
    float:left;
    }

The particular option when hovered triggers div.musictest to display as a block. 悬停时的特定选项将触发div.musictest显示为块。

.top-links li.music:hover + div.musictest {
        display:block;
    }

So when this drop down div.musictest is displaying as a block to maintain it showing as it includes options itself to be clicked I have CSS to say: 因此,当此下拉列表div.musictest显示为一个块以使其保持显示时,因为它包含要单击的选项本身,所以我要说CSS:

.top-links div.musictest:hover {
     display:block;
    }

So here is my problem, from everywhere I have read if I want .top-links div.musictest to fade out when I stop hovering it needs a transition delay however when I put one in nothing happens as I believe display:none is stopping it from fading out. 所以这是我的问题,从我想阅读的所有地方开始,如果我想停止停止悬停时.top-links div.musictest消失,它需要一个过渡延迟,但是当我不放任何东西时,我相信display:none不会停止从褪色。 But without the display:none it shows the hidden div.musictest always. 但是没有display:none,它总是显示隐藏的div.musictest。

At the moment the code above works fine if I want the hidden div to disappear immediately, but I am wanting it to have a transition time before disappearing. 目前,如果我希望隐藏的div立即消失,则上面的代码可以正常工作,但是我希望它在消失之前有一个过渡时间。

Any ways around this or am I putting it in the wrong place completely? 有什么办法解决还是我将其完全放在错误的位置?

Thanks! 谢谢!

You can set opacity: 0 instead of display: none 您可以设置opacity: 0而不是display: none

(Edit: I just read your title again, you can check this link for only fade out) (编辑:我只是再次阅读了您的标题,您可以检查此链接以使其淡出)

So here is an example with both: 因此,这是两个示例:

 .top-links li.music:hover { background-color: #EEE; } .top-links li.music:hover + div.musictest { display: block; opacity: 1; } .top-links div.musictest { opacity: 0; transition: 1s; -moz-transition: 1s; -webkit-transition: 1s; -o-transition: 1s; position: absolute; float: left; top: 44px; width: 800px; left: 22px; } .top-links div.musictest:hover { display: block; } 
 <div class="top-links"> <toplinks> <ul id="menu"> <li>Home</li> <li class="music">Music</li> <div class="musictest"> Show when hover li class="music" </div> </ul> </toplinks> </div> 

I set the opacity of the div to zero so it won't be visible but it's still a in display mode block and when your mouse is out of the element it's start to fade out as you wanted. 我将div的不透明度设置为零,因此它不可见,但它仍然是处于显示模式的块,当鼠标移出该元素时,它会根据需要开始淡出。

Add this to your css: 将此添加到您的CSS:

.top-links li.music:hover + div.musictest {

       opacity:1;
       transition: opacity 0.1s;
       -moz-transition: opacity 0.1s;
       -webkit-transition: opacity 0.1s;
       -o-transition: opacity 0.1s;
    }

  .top-links li.music + div.musictest {
       opacity:0;
       display:block;
       transition: opacity 2s;
       -moz-transition: opacity 2s;
       -webkit-transition: opacity 2s;
       -o-transition: opacity 2s;
    }

And here is a working example 这是一个有效的例子

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

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