javascript - 如何设置 .animate 函数 vanilla JS 参数中的动画填充模式?

标签 javascript html css animation css-animations

我正在努力寻找 vanilla JavaScript 中允许我设置 animation-fill-mode 的方法的文档或示例。我正在使用 Element.animate(animation, Timing) 函数来实现此目的。

我尝试将 animation-fill-mode 添加到 timing 对象的条目中,但根据我的测试,它不是有效参数。我还尝试同时使用 Animation.onfinishAnimation.pause() 在完成时暂停它,但这也不起作用。这是它使用的所有代码:

const quotemove = [
    { transform: "translate(0vw) rotate(0deg)" },
    { transform: "translate(80vw) rotate(180deg)" }
]

const quotetiming = {
    duration: 1000,
    iterations: 1,
    // this is where i attempted to add fill mode    
}

const quoteholders = document.getElementsByClassName("quote")
for(let i = 0; i < quoteholders.length; i++) {
    let quoteholder = quoteholders.item(i)
    const quotemark = quoteholder.querySelector(".quotationmark")
    quoteholder.addEventListener("mouseover", () => {
        let animation = quotemark.animate(quotemove, quotetiming)
    })     
}

我还应该提到,我打算向 mouseout 事件添加另一个动画,以便在您悬停时它保持在一个位置,而在不悬停时保持另一个位置。

如果无法将填充模式设置为forwards并在将来实现上述请求,那么我是否应该考虑另一种类似的方法?我很感激。

最佳答案

您的 quotetiming KeyframeEffect 对象就是正确的位置。
不确定你做错了什么,你需要的是设置 fill 属性:

const quotemove = [
    { transform: "translate(0vw) rotate(0deg)" },
    { transform: "translate(80vw) rotate(180deg)" }
]

const quotetiming = {
    duration: 1000,
    iterations: 1,
    fill: "forwards"
}

const quoteholders = document.getElementsByClassName("quote")
for(let i = 0; i < quoteholders.length; i++) {
    let quoteholder = quoteholders.item(i)
    const quotemark = quoteholder.querySelector(".quotationmark")
    quoteholder.addEventListener("mouseover", () => {
        let animation = quotemark.animate(quotemove, quotetiming)
    })     
}
.quote {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: lightgray;
}
blockquote {
  background: ivory;
  transform-origin: center;
  display: inline-block;
}
<div class="quote">
  <blockquote class="quotationmark">Hover the page</blockquote>
</div>

关于javascript - 如何设置 .animate 函数 vanilla JS 参数中的动画填充模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75122828/

相关文章:

javascript - 尝试实现特定于 Django 模型属性的“阅读更多”按钮

javascript - jQuery DOM 遍历

html - 重置输入元素的样式并重新设置样式以恢复默认外观

jquery - jQuery 帖子上没有发生任何事情

javascript - Safari 中的低图像质量,使用了 jssor slider

c# - 避免在 ASP.NET 上打印 HTML 标记

javascript - 如何将日期选择器附加到单个动态插入的输入字段?

javascript - 跨度必须出现在圆 div 内

html - 当一个 CSS 文件不符合媒体类型 HTML 声明时,它是否仍然被下载?

javascript - 弹出表单失败,但在 JsFiddle 中,它有效