简体   繁体   English

更改一个div的不透明度,并将鼠标悬停在另一个div上

[英]Change opacity of one div WITH transition on hover over another div

I'm trying to change the opacity of a div slowly when hovering over another div. 我想在将div悬停在另一个div上时缓慢地更改div的不透明度。

I can change the opacity of the div when hovering over the other div but the problem is that I dont know how to do slowly with a transition. 将鼠标悬停在另一个div上时,可以更改div的不透明度,但是问题是我不知道如何缓慢地进行过渡。

My code looks like this: 我的代码如下所示:

 #my-id:hover ~ #info .hc { display: block; opacity: 1; } #info div { display: none; opacity: 0; } .test-border { border: 1px solid red; width: 100%; height: 100%; color: red; } 
 <div> <div id="my-id">Hover here</div> <div id="info"> <div class="hc test-border">Information</div> </div> </div> 

Add the transition property on the div that changes: 在更改的div上添加transition属性:

#info div {
    transition:opacity 1s ease;
    opacity: 0;
}

The problem here is you may need to remove the display:none since this property can't be animated by transition. 这里的问题是您可能需要删除display:none因为此属性无法通过过渡设置动画。

 #my-id:hover ~ #info .hc { opacity: 1; } #info div { opacity: 0; transition:opacity 1s ease; } .test-border { border: 1px solid red; width: 100%; height: 100%; color: red; } 
 <div> <div id="my-id">Hover here</div> <div id="info"> <div class="hc test-border">Information</div> </div> </div> 

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

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