如何使用CSS在图像之间交叉淡入淡出?

时间:2012-05-28 09:00:34

标签: html css transitions

我完全按照教程,而不是从Print_tab.png淡入Print_tab_hover.png, 它只是淡化为白色。我能解决这个问题(不使用javascript)吗?。

以下是我使用的代码:

HTML:

    <div id="print"
     <img class="bottom" src="images/print_tab_hover.png"  />
     <img class="top" src="images/print_tab.png"  />
    </div>

CSS:

    #print {
    position:relative;
    width: 300px;
    height: 169px;
    margin: 0 auto;
    }

    #print img {
    position: absolute;
    left: 0;
    -webkit-transition: opacity 1s ease-in-out;
    -moz-transition: opacity 1s ease-in-out;
    -o-transition: opacity 1s ease-in-out;
    -ms-transition: opacity 1s ease-in-out; 
    transition: opacity 1s ease-in-out;
    }

    #print img.top:hover {
    opacity: 0;
    }

1 个答案:

答案 0 :(得分:1)

这有效:

#print {
    position:relative;
    width: 200px;
    height: 120px;
    margin: 0 auto;
    background-image: url(http://fc06.deviantart.net/images2/i/2004/07/e/7/Firefox_dock_icon.png);
}

#print img {
    position: absolute;
    left: 0;
    width: 200px;
    -webkit-transition: opacity 1s ease-in-out;
    -moz-transition: opacity 1s ease-in-out;
    -o-transition: opacity 1s ease-in-out;
    -ms-transition: opacity 1s ease-in-out; 
    transition: opacity 1s ease-in-out;
}

#print img.top:hover {
    opacity: 0;
}

<div id="print">
   <img class="top" src="http://lanscaping-ideas.com/wp-content/uploads/2012/04/Landscape-Paintings-2.jpg"  />
</div>

只需为#print设置默认背景并淡化新图像,反之亦然。