悬停在文本上,图像淡入淡出&出来

Hover Over Text, Image Fades In & Out

本文关键字:淡出 amp 出来 淡入 图像 文本 悬停      更新时间:2023-09-26

昨天,当我悬停在另一个链接上时,我得到了一些帮助,让图像淡出,但我很难弄清楚如何让它淡出。这可能涉及java?我很抱歉,因为我对更高级的编码(过去的基本CSS/HTML)还很陌生。

这是昨天有人给我的,它会很好地淡出,但我喜欢它在你从链接中删除鼠标后淡出。

img { opacity: 0; }
a:hover +img {
    -webkit-animation: changeOpacity 5s;
    animation: changeOpacity 5s;
}
@-webkit-keyframes changeOpacity {
    0%   { opacity: 0; }    
    100% { opacity: 1; }
}
/* Standard syntax */
@keyframes changeOpacity {
    0%   { opacity: 0; }    
    100% { opacity: 1; }
}

这就是HTML:

<a href="http://lorempixel.com/300/300/sports/1">
    Hover the see Full image,click to go there
</a>
<img src="http://lorempixel.com/300/300/sports/1" width="200" height="200" />
<br>
<a href="http://lorempixel.com/300/300/sports/1">
    Hover the see Full image,click to go there
</a>
<img src="http://lorempixel.com/300/300/sports/1" width="200" height="200" />

如有任何帮助,我们将不胜感激!

关键帧在这里被过度渲染了。您可以简单地使用transition,然后根据需要更改opacity值:

img { 
    opacity: 0; 
    transition: opacity 5s;
}
a:hover +img {
    opacity: 1;
}

示例Fiddle

请注意,我在Fiddle中加快了过渡速度,因为在我的经验中,5秒对于渐变效果来说有点过分。

以下是我的操作方法:(直接在下面运行)

$('a').hover( function(){
    $(this).parent().find('img').css('opacity',1)
}, function(){
    $(this).parent().find('img').css('opacity',0)
});
img{
    opacity : 0;
  transition: opacity 1s ease-in-out;
   -moz-transition: opacity 1s ease-in-out;
   -webkit-transition: opacity 1s ease-in-out;
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <a href="http://lorempixel.com/300/300/sports/1">Hover the see Full image,click to go there</a>        <br>
    <img src="http://lorempixel.com/300/300/sports/1" width="200" height="200" />
</div>
    
<div>   
<a href="http://lorempixel.com/300/300/sports/1">Hover the see Full image,click to go there</a><br>
<img src="http://lorempixel.com/300/300/sports/1" width="200" height="200" />
</div>