jquery - 从左到右将图像从灰度动画化为彩色

标签 jquery html css

免责声明:我不想将图像从左向右移动。我在 Stack Overflow 上浏览了相当多的答案,所有这些答案都指的是从左到右移动图像。

我尝试过过渡和使用背景位置。我希望我的动画能够将灰度图像从左到右转换为彩色图像。就像颜色应该从左到右出现一样。动画应该类似于图像的彩色版本,从左到右与灰度图像重叠。

这是我到目前为止所做的:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
      $("#pp").addClass('prog_load')
    });
});

</script>

<style>

.button {
    background-color: #000000;
    border: none;
    cursor: pointer;
    color: #ff0000;
    padding: 8px 16px;
    text-align: center;
    display: inline-block;
    font-size: 12px;
    box-shadow: 2px 1px 4px #000000;
}

.button:active {
  background-color: #ff0000;
  color: #000000;
  box-shadow: 2px 1px 4px #000000;
  transform: translateY(2px);
}

.prog_bar {
  filter: grayscale(100%);
}


.prog_load {
filter: grayscale(0%);

transition: all 10s ease;


}

</style>
</head>
<body>
<button class = "button" id = "button"> Claik meh </button>
    <img src = imgurl class = "prog_bar" id = "pp">

</body>
</html>

codeshare.io

它所做的只是将灰度图像转换为彩色。

最佳答案

这就是罗伯特所说的......

JSfiddle

$(".effect img").each(function() {
  $this = $(this);
  imgS = $this.attr("src");

  $this.after("<div class='effect-dup'><img src='" + imgS + "' /></div>")
});

$("button").click(function() {
  $this = $(this);

  $(this).prev().addClass("cool-effect");
})
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.effect {
  position: relative;
}

.effect-dup {
  filter: grayscale(100%);
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  width: 0;
  transition: width 1s ease-in-out;
}

.effect-dup.cool-effect {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="effect">
  <img src="https://placeimg.com/640/480/nature" />
  <button>
    See cool effect
  </button>
</div>

<div class="effect">
  <img src="https://placeimg.com/640/480/tech" />
  <button>
    See cool effect
  </button>
</div>

纯 CSS 和 HTML:

body {
  margin: 0; /*neccessary for effect*/
}

.effect {
  position: relative;
}

.effect-dup {
  filter: grayscale(100%);
  position: absolute;
  overflow: hidden;
  top: 0;
  left: 0;
  width: 0;
  animation: width 1s ease-in-out;
  animation-fill-mode: forwards;
}

@keyframes width {
  from {
    width: 0;
  }
  to {
    width: 100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="effect">
  <img src="https://placeimg.com/640/480/nature" />
  <div class='effect-dup'><img src="https://placeimg.com/640/480/nature" /></div>
</div>

<div class="effect">
  <img src="https://placeimg.com/640/480/tech" />
  <div class='effect-dup'>
    <img src="https://placeimg.com/640/480/tech" />
  </div>
</div>

关于jquery - 从左到右将图像从灰度动画化为彩色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43004210/

相关文章:

javascript - 退出javascript函数不起作用

css - 侧边栏 div 未填充父级的 100%

html - 创建适合 div 图像高度的水平图像组合?

html - 如何将 CSS ID 属性设置为 Symfony2 表单输入

html - 相对和固定的百分比宽度之间的差异

html - 如何改变这种形式的看法?

html - 为什么这个 float 的 div 被其 sibling 的边距拉低了?

javascript - 固定标题表和 jquery side-col 动画的 CSS/Javascript 布局问题

javascript - 无法使用 jquery 在第二张图片后添加内容

jquery - 随机显示(洗牌)div