【问题标题】:CSS Fade in on hoverCSS 在悬停时淡入
【发布时间】:2026-01-16 22:26:01
【问题描述】:

当我使用 CSS 将鼠标悬停在某些文本上时,我目前正在尝试让图像淡入。我已经应用了 CSS 代码,但没有显示效果; div 出现,但没有淡入。

另外,我意识到 CSS 过渡并不真正适用于 IE。如果有人能指出我正确的解决方法,将不胜感激。 (:

CSS:

.thumbnail{
position: relative;
z-index: 0;
}

.thumbnail:hover{
background-color: transparent;
z-index: 50;
}

.thumbnail span{ /*CSS for enlarged image*/
position: relative;
display: none;
color: black;
text-decoration: none;
opacity:0.0;
filter:alpha(opacity=0);
}

.thumbnail span img{ /*CSS for enlarged image*/
border-width: 0;
padding: 5px;
left: -1000px;
border: 1px solid gray;
background-color: #fff;
}

.thumbnail:hover span{ /*CSS for enlarged image on hover*/
position: relative;
display: inline;
top: -290px;
left: -25px; 
opacity:1.0;
filter:alpha(opacity=100);/*position where      
enlarged image should offset horizontally */
-webkit-transition: all 0.2s ease-in-out;
-moz-transition: all 0.2s ease-in-out;
-o-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}

#networking {
width: 200px; 
height: 140px; 
margin-left: 360px; 
top: 115px; 
position: absolute; 
background-color: #613286; 
opacity:1.0;
filter:alpha(opacity=100); 
color: #ffffff; 
text-align:center; 
border-radius: 20px; 
-webkit-transform: rotate(14deg);
-moz-transform: rotate(14deg);
-ms-transform: rotate(14deg);
-o-transform: rotate(14deg);
transform: rotate(14deg);
}

HTML:

<div id="networking">
<a class="thumbnail" href="1.5.2experientialstudios.html#down4"><h4>Networking Lounge</h4>    
<span><img src="images/net3.jpg" width="250" /></span></a>
</div>

谢谢!

【问题讨论】:

    标签: css


    【解决方案1】:

    尝试删除您的显示规则:

    .thumbnail span{ /*CSS for enlarged image*/
        position: relative;
    
        /*display: none; remove this */
    
        color: black;
        text-decoration: none;
        opacity:0.0;
        filter:alpha(opacity=0);
    }
    

    由于您的不透明度为 0,因此您不需要 display:none 并且您无法在根本不显示和内联之间进行转换,因为它们是不同的类型。

    并修改这条规则:

    .thumbnail:hover span { /*CSS for enlarged image on hover*/
    
        top: 0px; /* adjust as needed */
        left: -25px; 
        opacity:1.0;
        filter:alpha(opacity=100);/*position where      
        enlarged image should offset horizontally */
        -webkit-transition: all 0.2s ease-in-out;
        -moz-transition: all 0.2s ease-in-out;
        -o-transition: all 0.2s ease-in-out;
        transition: all 0.2s ease-in-out;
    }
    

    (悬停然后跨度会使它有点跳跃)。

    我还为过渡添加了一个 ms 前缀版本。在这种情况下它显然没有用。

    对于 IE9 及以下版本,您可以使用 jQuery 淡入元素(或简单地使用原生 JavaScript 在 setTimeout 循环中修改不透明度)。

    在这里调琴:
    http://jsfiddle.net/AbdiasSoftware/9rCQv/

    这就是你所追求的吗?

    【讨论】:

    • 嗨,是的,效果很好!谢谢!快速提问 - 以 ms 为前缀的版本也可以在旧版本的 IE 中使用吗?或者有没有办法让它在旧版本的 IE 上工作,例如IE 8/9?
    • @BlissL.Ng 它(过渡)肯定不能与 IE8 一起使用,而且我不确定 9+(我没有 IE9+,所以我无法测试),但我怀疑它只适用于 IE10。
    • 感谢您的回答!仍然希望寻找一种方法在 IE 8/9 中也能正常工作,但非常感谢!
    • @BlissL.Ng 您将无法在 IE8(可能是 9)中使用过渡,但请查看 jQuery 和 fadeIn/Out 函数(或者您可以使用 vanilla Javascript 函数来修改带有超时的不透明度样式)。
    • Microsoft Internet Explorer 9 及更低版本不支持通过 css 进行转换。其次,不需要使用 -ms-transition 前缀。这不是此规则的适当供应商扩展