如何使用JavaScript(无jQuery)和Promise或异步/等待淡出动画替换元素?

发布于 2025-02-06 08:00:13 字数 2398 浏览 0 评论 0原文

我正在尝试使用仅使用JavaScript淡入动画来替换元素。我认为它需要使用Promiseasync/等待机制,因此我尝试编写下面的代码。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2025-02-13 08:00:13

我终于将整个功能重新制作为这样的简单功能,它起作用

let elem = document.querySelector('.asd');
let elem2 = document.querySelector('.asd2');
let ms = 2000;
let intrvl = 20;
const action = fadeOut(elem, ms, intrvl);
action.then(response => {
  console.log('res', response);
  ms = 200;
  intrvl = 20;
  return fadeIn(elem2, ms, intrvl); 
}).then(response => {
  console.log('res2', response);
});

function fadeIn(elem, ms, intrvl) {
  return new Promise((resolve) => {
    let opacity = 0;
    setInterval(() => {
      opacity += intrvl/ms;
      elem.style.opacity = opacity;
      if (opacity >=1) {
        resolve('selesai fade in');
      }
    }, intrvl);
  });
}

function fadeOut(elem, ms, intrvl) {
  return new Promise(resolve => {
    let opacity = 1;
    setInterval(() => {
      opacity -= intrvl/ms;
      elem.style.opacity = opacity;
      if (opacity < 0) {
        elem.style.display = 'none';
        resolve('selesai fade out');
      }
    }, intrvl);
  });
}
.asd {
  opacity: 1;
}

.asd2 {
  opacity: 0;
}
<div class="asd">Hello World!</div>
<div class="asd2">New Item!</div>

I finally remake the whole function into a simpler one like this and it works

let elem = document.querySelector('.asd');
let elem2 = document.querySelector('.asd2');
let ms = 2000;
let intrvl = 20;
const action = fadeOut(elem, ms, intrvl);
action.then(response => {
  console.log('res', response);
  ms = 200;
  intrvl = 20;
  return fadeIn(elem2, ms, intrvl); 
}).then(response => {
  console.log('res2', response);
});

function fadeIn(elem, ms, intrvl) {
  return new Promise((resolve) => {
    let opacity = 0;
    setInterval(() => {
      opacity += intrvl/ms;
      elem.style.opacity = opacity;
      if (opacity >=1) {
        resolve('selesai fade in');
      }
    }, intrvl);
  });
}

function fadeOut(elem, ms, intrvl) {
  return new Promise(resolve => {
    let opacity = 1;
    setInterval(() => {
      opacity -= intrvl/ms;
      elem.style.opacity = opacity;
      if (opacity < 0) {
        elem.style.display = 'none';
        resolve('selesai fade out');
      }
    }, intrvl);
  });
}
.asd {
  opacity: 1;
}

.asd2 {
  opacity: 0;
}
<div class="asd">Hello World!</div>
<div class="asd2">New Item!</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文