【问题标题】:Delay anchor link destination till animation completes?延迟锚链接目的地直到动画完成?
【发布时间】:2020-08-26 19:13:29
【问题描述】:

我正在尝试使用 vanilla JS 和 CSS 进行页面转换。当单击“收藏”按钮时,我希望将一个类添加到我的元素中。我有一个功能,我通过使用 prevent default 来延迟链接重定向,添加 CSS 动画类,然后在延迟 4 秒后设置新的窗口位置。但是目前当我点击收藏时,没有任何反应,尽管当我将鼠标悬停在文本上时,链接预览是正确的。我正在使用 animate.css 的内置动画,它只需要您在 head 标签中链接其样式表并将动画类添加到元素中。

HTML -

<div id="homeContainer" class="home-container">
         <div class="bg-home bg-move">
            <img src="/Home/Asset 3.png" alt="film">
         </div>
         <div class="nav-button-h text-fade">
            <p id="pressbtn">ABOUT</p>
            <p id="logo-center">SHEK LEUNG</p>
            <p id="aboutbtn">PRESS</p>
         </div>
         <div class="nav-button-v text-fade">
            <a href="/collections/MA/MA.html" class="text-fade" id="colbtn">COLLECTIONS</a>
            <p id="projbtn">PROJECTS</p>
         </div>
     </div>

JS

const colbtn = document.getElementById('colbtn');
const homeContainer = document.getElementById('homeContainer');

colbtn.addEventListener('click', animateDown);

function animateDown(e) {
   e.preventDefault();
   homeContainer.classList.add('animate__animated animate__fadeOutDown');
   if (this.href) {
      var target = this.href;
   }
   setTimeout(function () {
      window.location.replace(target);
   }, 4000);

}

【问题讨论】:

  • 控制台有错误吗?它找到元素了吗?检查时是否看到在元素上设置的类?

标签: javascript html css animate.css


【解决方案1】:

你不能像那样添加类,它们需要用逗号分隔。

const colbtn = document.getElementById('colbtn');
const homeContainer = document.getElementById('homeContainer');

colbtn.addEventListener('click', animateDown);

function animateDown(e) {
  e.preventDefault();
  homeContainer.classList.add('animate__animated', 'animate__fadeOutDown');
  if (this.href) {
    var target = this.href;
  }
  setTimeout(function() {
    window.location.replace(target);
  }, 4000);

}
<div id="homeContainer" class="home-container">
  <div class="bg-home bg-move">
    <img src="/Home/Asset 3.png" alt="film">
  </div>
  <div class="nav-button-h text-fade">
    <p id="pressbtn">ABOUT</p>
    <p id="logo-center">SHEK LEUNG</p>
    <p id="aboutbtn">PRESS</p>
  </div>
  <div class="nav-button-v text-fade">
    <a href="/collections/MA/MA.html" class="text-fade" id="colbtn">COLLECTIONS</a>
    <p id="projbtn">PROJECTS</p>
  </div>
</div>

【讨论】:

  • 嘿,我已将其更正为仅添加 animate__fadeOutDown"。现在链接确实会延迟重定向,但动画不会出现
  • 你有没有像这样添加两个类('animate__animated', 'animate__fadeOutDown')?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 2011-07-10
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多