javascript - 如何在 Css 或 Javascript 中为其他图像制作动画?

标签 javascript html css

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 6 年前

假设第一张图片是黑白的。 第二张图片是彩色的。 超时后,黑白图像应更改为彩色图像..带有动画 就像加载进度条一样?我如何使用 Css 或 JavaScript 执行此操作?

[为清楚起见,从 OP 的评论中添加,如下]

OP: is it possible with some slow linear kind of animation ?? like filling water in glass..color filling in b/w image ?

最佳答案

首先使用 position: absolute; 将两张图片放在彼此上方。使用背景图片,您可以这样做:

<div class="container">
  <div class="image1"></div>
  <div class="image2" id="image_resize"></div>
</div>

CSS:

.container {
  position: relative;
  width: 300px;
  height:200px;
}

.image1 {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url(http://images.freeimages.com/images/previews/cf6/bird-1394216.jpg);
  background-repeat: no-repeat;
}

.image2 {
  position: absolute;
  height: 100%;
  background-image: url(http://images.freeimages.com/images/previews/4f3/apple-1322812.jpg);
  background-repeat: no-repeat;
}

要为第二张图片设置动画,最简单的方法是使用 jquerys animate。使用纯 JavaScript,您可以执行以下操作:

var percent = 0; // this will count from 0 to 100, defining the with in percent

/* with setinterval you can call a function periodically, in this example every 50 milliseconds */
var myInterval = window.setInterval(function() {
    /* find the image that should get resized by id */
    var img = document.getElementById('image_resize');

    /* set the with of the image */
    img.style.width = percent+"%";

    /* increment by 1 percent */
    percent ++;

    if(percent > 100) {
      /* when 100% is reached, stop the interval */
      clearInterval(myInterval);
    }
}, 50);

这是一个有效的 jsfiddle:https://jsfiddle.net/c51u6r8t/

关于javascript - 如何在 Css 或 Javascript 中为其他图像制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42433097/

上一篇:css - 使用CSS在响应图像上播放按钮

下一篇:jquery - 如何使用 jquery 添加类 'active'

相关文章:

javascript - @Html.TextBoxFor 文本改变事件

javascript - 如何在用户点击页面的地方打开引导对话框

javascript - replace() 与第二个参数作为变量?

html - Bootstrap 导航栏不会折叠 + 小按钮

javascript - 如何使用 jQuery toggle 更改 css 的内容

html - 在过渡期间,我如何阻止动画内容与其他立即卡入到位的元素重叠?

css - 让第一个 child 进入同一类(class)?

javascript - 使用隐藏字段值编写 JQuery/Javascript IF/else 语句

javascript - React-Router:如果未通过身份验证则显示主页,如果在 "/"中通过身份验证则显示仪表板

jquery - 如何使内容适合剩余高度