jquery - Webkit 动画不会从无到有淡化

标签 jquery css webkit css-animations

我知道我没有正确使用 CSS 动画,但我正在努力寻找一个 web-kit 转换来满足我的需要。我需要图像 (enter_button.png) 以 0% 的不透明度加载,然后在 3 秒后淡入纯色 100% 图像。我目前的代码是,

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>denby</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="wrapper_index">
            <div id="enter_button"> 
                <img src="images/enter_button.png" width="138" height="50" class="withfadeout" /> 
            </div>
        </div>
    </body>
</html>

CSS:

@charset "utf-8";
.withfadeout { 
    -webkit-transition: all 2s ease-in-out; 
    -moz-transition: all 2s ease-in-out; 
    -ms-transition: all 2s ease-in-out; 
    -o-transition: all 2s ease-in-out; 
    transition: all 2s ease-in-out; 
} 
.withfadeout:hover { 
    -webkit-opacity: 0.25; 
    -moz-opacity: 0.25; 
    opacity: 0.25; 
}
#wrapper_index {
    height: 686px;
    width: 1024px;
    background-color: #000;
    margin-right: auto;
    margin-left: auto;
    margin-top: 50px;
    background-image: url(images/entry_image-01.png);
    background-repeat: no-repeat;
    position: relative;
}
#enter_button {
    width: 140px;
    position: absolute;
    left: 431px;
    top: 413px;
}

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

最佳答案

您可以使用 CSS3 动画来实现这一点。

img { /* using generic img tag selector only for demo, change selector as appropriate */
  opacity: 0;
  -webkit-animation: load 3s 3s linear forwards;
  -moz-animation: load 3s 3s linear forwards;
  animation: load 3s 3s linear forwards; /* adding mode as forwards makes the animation to retain its state as at the last keyframe */
  /* syntax is animation: animation-name animation-duration animation-delay animation-timing-function animation-fill-mode */
}
@-webkit-keyframes load {
  from {
    opacity: 0;
  }
  /* starts at opacity 0 */
  to {
    opacity: 1;
  }
  /* ends at opacity 1 */
}
@-moz-keyframes load {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes load {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<div id="wrapper_index">
  <div id="enter_button">
    <img src="https://www.google.com.sg/intl/en_ALL/images/srpr/logo11w.png" width="138" height="50" class="withfadeout" />
  </div>
</div>

注意:上述代码中的 3 秒延迟意味着动画本身将在 3 秒后开始,因此图像变为 100% 实体总共需要 6 秒。如果不需要延迟,可以将其删除。

进一步阅读: MDN - CSS3 Animation Spec

关于jquery - Webkit 动画不会从无到有淡化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988470/

相关文章:

php - 在其他域的另一个页面上加载 PHP 页面的 div

javascript - 如何使用 webkit 浏览器选择范围内的节点?

php - 使用 htaccess 时未应用样式

php - 如果条件为真,在保存按钮附近显示 div

html - 在 inline-block 元素上使用 box-align 使文本垂直居中

debugging - iOS 设备上的远程 console.log()

jquery - 使用文本淡入播放声音

javascript - 类型错误 : Cannot call method 'replace' of null - Backbone. js

javascript - 为了了解 jQuery,我必须先了解 JavaScript 吗?

javascript - 页面加载时出现弹出窗口