如何使用JavaScript(无jQuery)和Promise或异步/等待淡出动画替换元素?
我正在尝试使用仅使用JavaScript淡入动画来替换元素。我认为它需要使用Promise
和async/等待
机制,因此我尝试编写下面的代码。 vadeout
过程正常工作,但是第二个元素不会出现。我在做什么错?
这是我的代码:
const main = document.querySelector('#main');
const el0 = document.querySelector('.x');
const str = '<div class="asd1">Hello</div>'; // generated from fetch result
main.insertAdjacentHTML('beforeend', str);
const el1 = document.querySelector('.asd1');
(async() => {
await fadeOut(el0, 1000);
await fadeIn(el1, 1000);
})();
function fadeIn(elem, ms) {
return new Promise((resolve, reject) => {
if (!elem)
return;
elem.style.opacity = 0;
elem.style.filter = "alpha(opacity=0)";
elem.style.display = "inline-block";
elem.style.visibility = "visible";
if (ms) {
var opacity = 0;
var timer = setInterval(function() {
opacity += 50 / ms;
if (opacity >= 1) {
clearInterval(timer);
opacity = 1;
}
elem.style.opacity = opacity;
elem.style.filter = "alpha(opacity=" + opacity * 100 + ")";
}, 50);
} else {
elem.style.opacity = 1;
elem.style.filter = "alpha(opacity=1)";
}
resolve(elem);
});
}
function fadeOut(elem, ms) {
return new Promise((resolve, reject) => {
if (!elem)
return;
if (ms) {
var opacity = 1;
var timer = setInterval(function() {
opacity -= 50 / ms;
if (opacity <= 0) {
clearInterval(timer);
opacity = 0;
elem.style.display = "none";
elem.style.visibility = "hidden";
}
elem.style.opacity = opacity;
elem.style.filter = "alpha(opacity=" + opacity * 100 + ")";
}, 50);
} else {
elem.style.opacity = 0;
elem.style.filter = "alpha(opacity=0)";
elem.style.display = "none";
elem.style.visibility = "hidden";
}
});
}
.asd1 {
width: 100px;
height: 100px;
background-color: red;
opacity: 0;
}
.x {
width: 50px;
height: 50px;
background-color: purple;
}
<div id="main">
<div class="x">
blah
</div>
</div>
I'm trying to replace an element using fade animation with only javascript. I figure it needs to use Promise
and async/await
mechanism so I tried to write the code below. the fadeOut
process works fine, but somehow the 2nd element won't appear.. what am I doing wrong?
here's my code:
const main = document.querySelector('#main');
const el0 = document.querySelector('.x');
const str = '<div class="asd1">Hello</div>'; // generated from fetch result
main.insertAdjacentHTML('beforeend', str);
const el1 = document.querySelector('.asd1');
(async() => {
await fadeOut(el0, 1000);
await fadeIn(el1, 1000);
})();
function fadeIn(elem, ms) {
return new Promise((resolve, reject) => {
if (!elem)
return;
elem.style.opacity = 0;
elem.style.filter = "alpha(opacity=0)";
elem.style.display = "inline-block";
elem.style.visibility = "visible";
if (ms) {
var opacity = 0;
var timer = setInterval(function() {
opacity += 50 / ms;
if (opacity >= 1) {
clearInterval(timer);
opacity = 1;
}
elem.style.opacity = opacity;
elem.style.filter = "alpha(opacity=" + opacity * 100 + ")";
}, 50);
} else {
elem.style.opacity = 1;
elem.style.filter = "alpha(opacity=1)";
}
resolve(elem);
});
}
function fadeOut(elem, ms) {
return new Promise((resolve, reject) => {
if (!elem)
return;
if (ms) {
var opacity = 1;
var timer = setInterval(function() {
opacity -= 50 / ms;
if (opacity <= 0) {
clearInterval(timer);
opacity = 0;
elem.style.display = "none";
elem.style.visibility = "hidden";
}
elem.style.opacity = opacity;
elem.style.filter = "alpha(opacity=" + opacity * 100 + ")";
}, 50);
} else {
elem.style.opacity = 0;
elem.style.filter = "alpha(opacity=0)";
elem.style.display = "none";
elem.style.visibility = "hidden";
}
});
}
.asd1 {
width: 100px;
height: 100px;
background-color: red;
opacity: 0;
}
.x {
width: 50px;
height: 50px;
background-color: purple;
}
<div id="main">
<div class="x">
blah
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于将整个功能重新制作为这样的简单功能,它起作用
I finally remake the whole function into a simpler one like this and it works