jquery - 有没有办法在没有 jquery.color 的情况下为 rgba 背景的 alpha 设置动画?

标签 jquery css jquery-animate alpha rgba

我需要使用 jQuery 的 animate() 从 rgba(255,255,255,0.5)rgba(255,255,255,0.9) 的动画背景,而不使用 jquery.color 插件和更改 CSS不透明度。 背景颜色始终相同(白色),我只需要为 rgba 的 alpha 设置动画。 有什么建议吗?

最佳答案

您可以使用带有所需动画的预制 CSS 类,然后使用 jQuery 来打开/关闭该类。

这是一个工作 fiddle :https://jsfiddle.net/syoels/moL1q6ea/

$('#button1').click(function() {
  $('.square').removeClass('fadeMeIn').addClass('fadeMeOut');
});

$('#button2').click(function() {
  $('.square').removeClass('fadeMeOut').addClass('fadeMeIn');
});
.square {
  height: 50px;
  width: 50px;
  margin: 10px;
  background-color: rgba(255, 0, 0, 0.8);
}
.fadeMeOut {
  -webkit-animation: faeOutRGBA 1s linear;
  -moz-animation: faeOutRGBA 1s linear;
  -o-animation: faeOutRGBA 1s linear;
  animation: faeOutRGBA 1s linear;
  background-color: rgba(255, 0, 0, 0);
}
.fadeMeIn {
  -webkit-animation: faeInRGBA 1s linear;
  -moz-animation: faeInRGBA 1s linear;
  -o-animation: faeInRGBA 1s linear;
  animation: faeInRGBA 1s linear;
  background-color: rgba(255, 0, 0, 0.8);
}
@-webkit-keyframes faeOutRGBA {
  0% {
    background-color: rgba(255, 0, 0, 0.8)
  }
  100% {
    background-color: rgba(255, 0, 0, 0);
  }
}
@-moz-keyframes faeOutRGBA {
  0% {
    background-color: rgba(255, 0, 0, 0.8);
  }
  100% {
    background-color: rgba(255, 0, 0, 0);
  }
}
@-o-keyframes faeOutRGBA {
  0% {
    background-color: rgba(255, 0, 0, 0.8);
  }
  100% {
    background-color: rgba(255, 0, 0, 0);
  }
}
@keyframes faeOutRGBA {
  0% {
    background-color: rgba(255, 0, 0, 0.8);
  }
  100% {
    background-color: rgba(255, 0, 0, 0);
  }
  ;
}
@-webkit-keyframes faeInRGBA {
  0% {
    background-color: rgba(255, 0, 0, 0)
  }
  100% {
    background-color: rgba(255, 0, 0, 0.8);
  }
}
@-moz-keyframes faeInRGBA {
  0% {
    background-color: rgba(255, 0, 0, 0);
  }
  100% {
    background-color: rgba(255, 0, 0, 0.8);
  }
}
@-o-keyframes faeInRGBA {
  0% {
    background-color: rgba(255, 0, 0, 0);
  }
  100% {
    background-color: rgba(255, 0, 0, 0.8);
  }
}
@keyframes faeInRGBA {
  0% {
    background-color: rgba(255, 0, 0, 0);
  }
  100% {
    background-color: rgba(255, 0, 0, 0.8);
  }
  ;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="square"></div>
<button id="button1">fade out</button>
<button id="button2">fade in</button>

关于jquery - 有没有办法在没有 jquery.color 的情况下为 rgba 背景的 alpha 设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6744339/

相关文章:

javascript - 如何将单个用户的操作通知多个客户端?基于网络的 channel

javascript - 拉拉维尔 : can't I use enctype ='multipart/form-data' to POST non file inputs?

javascript - 在 jquery 中显示从中间向外扩展的 div

javascript - jQuery 动画背景位置不会改变但 css 会

javascript - 选中所有子复选框后如何选中全选复选框?

python - 如何动态重新渲染模板?

jquery - 拖动有序列表项时,后续项的编号总是增加 1

javascript - 将响应式自定义类添加到移动菜单

javascript - 如何在 jquery 中重新排序和动画文本?

jQuery 动画从 CSS "top"到 "bottom"