首页 > 解决方案 > 为什么anime.js 不为Firefox 或Microsoft Edge 中的left 属性设置动画?

问题描述

我正在尝试相对于其他一些 div 动态地为样式化 div 设置动画,这些 div 的数量和位置仅在运行时才知道。

以下 SSCCE 在 Chrome 中完美运行,但在 Firefox 或 Edge 中根本无法运行。有谁知道为什么?

我已经查看了许多其他类似的问题,但没有运气,并且说anime.js 文档很少是礼貌的。

import anime from "./anime.es.js";

var pips = document.getElementsByClassName("pip");
var fill = document.querySelector(".pip-fill");

var pipBorder = parseInt(window.getComputedStyle(pips[0]).borderWidth);

fill.style.left = (pips[0].offsetLeft + pipBorder) + "px";
fill.style.top = (pips[0].offsetTop + pipBorder) + "px";

anime({
    targets: fill,
    left: (pips[2].offsetLeft + pipBorder) + "px",
    duration: 1000,
    delay: 1000
});
.container {
    position: relative;
}

.pip {
    display: inline-block;
    width: 32px;
    height: 32px;
    border-style: solid;
    border-radius: 100%;
    border-width: 4px;
    border-color: black;
    margin: 64px;
    background-color: red;
}

.pip-fill {
    position: absolute;
    width: 32px;
    height: 32px;
    border-style: none;
    border-radius: 100%;
    background-color: black;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./index.css"/>
</head>
<body>
    <div class="container">
        <div class="pip"></div>
        <div class="pip"></div>
        <div class="pip"></div>
        <div class="pip-fill"></div>
    </div>
    <script src="./index.js" type="module"></script>
</body>
</html>

标签: javascriptcssfirefoxmicrosoft-edgeanime.js

解决方案


这是一个奇怪的,但可能会帮助别人。我通过目标 div 的边框宽度来偏移填充 div 的位置。由于某种原因,在 Firefox 和 Edge 中window.getComputedStyle(pips[0]).borderWidth都返回NaNwindow.getComputedStyle(pips[0]).borderTopWidth返回正确的值。我认为这可能是一个错误,因为border-width在 CSS 中明确设置。


推荐阅读