在JavaScript加载的图像上添加淡入

在JavaScript加载的图像上添加淡入,javascript,css,Javascript,Css,我有一个简单的函数,可以使用JavaScript将一个图像替换为其他图像。我有一条线可以在加载时淡入。但是,当显示新图像时,如何添加淡入 功能更改图像(a){ document.getElementById(“Image”).src=a; } 您可以像这样为fadeIn编写自定义函数: function fadeIn(el) { el.style.opacity = 0; var tick = function() { el.style.opacity = +el.sty

我有一个简单的函数,可以使用JavaScript将一个图像替换为其他图像。我有一条线可以在加载时淡入。但是,当显示新图像时,如何添加淡入


功能更改图像(a){
document.getElementById(“Image”).src=a;
}

您可以像这样为fadeIn编写自定义函数:

function fadeIn(el) {
  el.style.opacity = 0;


  var tick = function() {
    el.style.opacity = +el.style.opacity + 0.01;


    if (+el.style.opacity < 1) {
      (window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16)
    }
  };

  tick();
}
//taken from http://stackoverflow.com/questions/23244338/pure-javascript-fade-in-function

function changeImage(a) {
    var el = document.getElementById("Image");
    el.src = a;
    fadeIn(el)
}
功能fadeIn(el){
el.style.opacity=0;
var tick=函数(){
el.style.opacity=+el.style.opacity+0.01;
如果(+el.style.opacity<1){
(window.requestAnimationFrame&&requestAnimationFrame(勾号))| |设置超时(勾号,16)
}
};
勾选();
}
//取自http://stackoverflow.com/questions/23244338/pure-javascript-fade-in-function
功能更改图像(a){
var el=document.getElementById(“图像”);
el.src=a;
法代因(el)
}
其中
el=document.getElementById()
或其他内容


下面是

您可以像这样为fadeIn编写自定义函数:

function fadeIn(el) {
  el.style.opacity = 0;


  var tick = function() {
    el.style.opacity = +el.style.opacity + 0.01;


    if (+el.style.opacity < 1) {
      (window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16)
    }
  };

  tick();
}
//taken from http://stackoverflow.com/questions/23244338/pure-javascript-fade-in-function

function changeImage(a) {
    var el = document.getElementById("Image");
    el.src = a;
    fadeIn(el)
}
功能fadeIn(el){
el.style.opacity=0;
var tick=函数(){
el.style.opacity=+el.style.opacity+0.01;
如果(+el.style.opacity<1){
(window.requestAnimationFrame&&requestAnimationFrame(勾号))| |设置超时(勾号,16)
}
};
勾选();
}
//取自http://stackoverflow.com/questions/23244338/pure-javascript-fade-in-function
功能更改图像(a){
var el=document.getElementById(“图像”);
el.src=a;
法代因(el)
}
其中
el=document.getElementById()
或其他内容


下面是

您可以使用css3实现所需的更改图像()函数

@keyframes fadeIn{从{opacity:0;}到{opacity:1;}}
.container{顶部:20%;左侧:20%;}
.淡入{
动画:FadeinEase1;
动画持续时间:5s;
}

功能更改图像(a){
var elm=document.getElementById(“图像”);
var clonedElm=elm.cloneNode(真);
elm.parentNode.replaceChild(clonedElm,elm);
document.getElementById(“Image”).src=a;
}

您可以使用css3实现所需的更改图像()函数

@keyframes fadeIn{从{opacity:0;}到{opacity:1;}}
.container{顶部:20%;左侧:20%;}
.淡入{
动画:FadeinEase1;
动画持续时间:5s;
}

功能更改图像(a){
var elm=document.getElementById(“图像”);
var clonedElm=elm.cloneNode(真);
elm.parentNode.replaceChild(clonedElm,elm);
document.getElementById(“Image”).src=a;
}

可能重复:可能重复:感谢pregmatch提供此答案并检查其他答案。我将研究这个逻辑来解决这个问题。n_n为这个答案和检查另一个答案进行匹配。我将研究这个逻辑来解决这个问题。我想知道答案。我可以在这里玩得更多,因为css与脚本是分开的。谢谢你的回答。我可以用它玩得更多一些,因为css与脚本是分开的。