CSS 淡入背景图片

Tur*_*tax 3 html css hover

我正在学习CSS,我想在将鼠标悬停在某些文本上时,文本消失并且图像上淡入。我已经测试了一些东西,但它没有按照我想要的方式工作。

文本是 a 中的链接,在悬停时我设置了背景图像。我可以在 the 中使用 an,但我希望文本位于 the 的中间,并且它已经位于具有绝对位置的 a 中。

这是 HTML :

<div class="header"> 
  <!--<ul>-->
  <div class="header_element">
    <a title="Home" href="page.php"> Home </a>
  </div>
Run Code Online (Sandbox Code Playgroud)

还有CSS:

    div.header_element {
      display: table-cell;
      text-align: center;
      vertical-align: middle;
      height: 100%;
    }

    div.header_element:hover {
      background-position: center;
      background-image: url("hover.png");
      background-repeat: no-repeat; 
    }
Run Code Online (Sandbox Code Playgroud)

如果有人能帮助我,我将不胜感激。

kyb*_*.cz 5

您可能想使用不透明度和淡入效果。

div.header_element {
display: table-cell;
text-align: center;
vertical-align: middle;
}

div.header_element:hover {
background-position: center;
background-image: url("http://dummy-images.com/abstract/dummy-100x100-Rope.jpg");
background-repeat: no-repeat;
    -webkit-animation: fadein 2s; /* Safari, Chrome and Opera > 12.1 */
       -moz-animation: fadein 2s; /* Firefox < 16 */
        -ms-animation: fadein 2s; /* Internet Explorer */
         -o-animation: fadein 2s; /* Opera < 12.1 */
            animation: fadein 2s;
}

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Firefox < 16 */
@-moz-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Internet Explorer */
@-ms-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Opera < 12.1 */
@-o-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}
Run Code Online (Sandbox Code Playgroud)

如果这是你想要的,请参阅jsfidle 。