我正在编写一个媒体播放器,我已经使用了JavaScript和CSS来使歌曲的元信息滚动到字幕风格的效果中,但仅当曲目正在播放时.当您按下播放/暂停按钮时,我使用JS添加/删除了动画类.我想知道,如果动画类在动画中间被移除,是否有任何方法可以让动画在停止之前播放到结束.

var MetaScroll = document.getElementById("MetaScroll");
var PlayButton = document.getElementById("PlayButton");

function playPause() {
  MetaScroll.classList.toggle('scrolling');
  PlayButton.classList.toggle('fa-play');
  PlayButton.classList.toggle('fa-pause');
}
body {
  background: black;
}

.albumArt {
  width: 200px;
  height: 200px;
  border: 3px solid #212121;
  display: flex;
  flex-direction: column-reverse;
}

.metaContainer {
  height: 25%;
  overflow: hidden;
  background-color: white;
}

.meta {
  white-space: nowrap;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
}

.meta p {
  font-family: Courier, monospace;
  font-size: 20px;
  margin: 0;
  color: black;
}

.scrolling {
  transform: translateX(0);
  animation: b-text-scroll 5s linear infinite;
}

@keyframes b-text-scroll {
  0% {
    transform: translate3d(0, 0, 0);
  }
  20% {
    transform: translate3d(0, 0, 0);
  }
  100% {
    transform: translate3d(-100%, 0, 0);
  }
}

.pIcons {
  font-size: 20px;
  color: white;
  background-color: transparent;
  border: none;
}
<script src="https://kit.fontawesome.com/1706aa300d.js" crossorigin="anonymous"></script>

<body>
  <div class="albumArt">
    <div class="metaContainer">
      <div id="MetaScroll" class="meta">
        <p>&nbsp; The Absense of a Title - A Deadly Lack of Artist</p>
        <p>&nbsp; The Absense of a Title - A Deadly Lack of Artist</p>
      </div>
    </div>
  </div>
  <button class="pIcons" onclick="playPause()">
    <i id="PlayButton" class="fa-solid fa-play"></i>
  </button>
</body>

推荐答案

这需要比现在使用的更多的逻辑,但是不是在暂停时删除类,而是在那个时刻添加一个animationend事件侦听器,并在该事件触发时删除类.

var MetaScroll = document.getElementById("MetaScroll");
var PlayButton = document.getElementById("PlayButton");

function playPause() {
    // You need a way to track whether this is a play or pause action
    // Assume here that the variable pause is true when the click represents pausing
    if (pause) {
        // Let the animation continue, but add an event lister for when it ends
        MetaScroll.addEventListener('animationend', endAnimationAfterCompletion);
    } else {
        MetaScroll.classList.add('scrolling');
        // Remove the event listener so it doesn't fire if a user pause and then unpaused before the 'animationend' event had time to fire
        MetaScroll.removeEventListener('animationend', endAnimationAfterCompletion);
    }
    PlayButton.classList.toggle('fa-play');
    PlayButton.classList.toggle('fa-pause');
}

function endAnimationAfterCompletion() {
    MetaScroll.classList.remove('scrolling');
}

Javascript相关问答推荐

有什么(最佳)方法可以从模块中获取脚本模块的多姆元素吗?

我应该在redux reducer中调用其他reducer函数吗?

jQuery提交按钮重新加载页面,即使在WordPress中使用preventDefault()

D3 Scale在v6中工作,但在v7中不工作

Rehype将hashtag呈现为URL

对网格项目进行垂直排序不起作用

React.Development.js和未捕获的ReferenceError:未定义useState

如何将未排序的元素追加到数组的末尾?

元素字符串长度html

OpenAI转录API错误请求

使用Document.Evaluate() Select 一个包含撇号的HTML元素

当我点击一个按钮后按回车键时,如何阻止它再次被点击

处理app.param()中的多个参数

Clip-Path在网页浏览器(Mozilla、Edge、Chrome)上不能正常工作,但在预览版Visual Code Studio HTML、CSS、JS上却能很好地工作

在GraphQL解析器中修改上下文值

调用特定数组索引时,为什么类型脚本不判断未定义

Chrome上的印度时区名S有问题吗?

通过ng-绑定-html使用插入的HTML中的函数

调试jQuery代码以获取所有行的总和(票证类型)

元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件),但GET:Object.在Reaction项目中