用js改变运行css动画的风格

jvlzgdj9  于 2023-03-25  发布在  其他
关注(0)|答案(1)|浏览(140)

我想在js中改变一个正在运行的css动画的颜色,如下所示:
https://jsfiddle.net/kpf7ce4a/21/
动画flash使用自定义变量--fly_color。应该使用paintFlies()进行更改,但到目前为止还没有这样做。是否可以更改正在进行的动画的变量/样式?

zed5wv10

zed5wv101#

您需要将CSS变量放在:root伪元素中用于全局作用域,否则变量仅作为块级作用域。
我还修复了更改所有LoL苍蝇的代码。
JSFiddle

function addLolFlies() {
    var fireflyContainer = document.getElementById('lolfly-container');
    for(var i=0; i < 5; i++){
        var newLolflie = document.createElement('div');
        newLolflie.className = 'lolfly';
        fireflyContainer.appendChild(newLolflie);
    }
    document.body.appendChild(fireflyContainer);
}

function paintFlies() {
    const lolFlies = document.querySelectorAll('.lolfly');
  lolFlies.forEach((e)=>e.style.setProperty('--fly_color', '#000'));
}
:root { --fly_color: red}

body {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: white;
}

.lolfly {
    position: fixed;
    left: 50%;
    top: 50%;
    height: 100px;
    width: 100px;
    margin: -0.2vw 0 0 9.8vw;
    animation: ease 200s alternate infinite;
   
}

.lolfly:before, .lolfly:after {
    content: 'lol';
    opacity: 0.8;
    font-size: 1.7em;
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    transform-origin: -10vw;
}

.lolfly:before {
    opacity: 0.2;
    animation: drift ease alternate infinite;
}
.lolfly:after {
    opacity: 0;
    animation: drift ease alternate infinite, flash ease infinite;
}

@keyframes drift {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

@keyframes flash {
    0%, 30%, 100% {
        opacity: 0;
        box-shadow: none;
    }
    5% {
        color: var(--fly_color);
        opacity: 1;
        scale: 2;
        text-shadow: var(--fly_color) 1px 0 10px;
    }
}

.lolfly:nth-child(1) {
    animation-name: move1;
}
.lolfly:nth-child(1)::before {
    animation-duration: 12s;
}
.lolfly:nth-child(1)::after {
    animation-duration: 12s, 5582ms;
    animation-delay: 0ms, 7687ms;
}

@keyframes move1 {
    0% {
        transform: translateX(49vw) translateY(10vh) scale(0.37);
    }
    3.5714285714% {
        transform: translateX(11vw) translateY(43vh) scale(0.45);
    }
    7.1428571429% {
        transform: translateX(25vw) translateY(-31vh) scale(0.83);
    }
    10.7142857143% {
        transform: translateX(23vw) translateY(36vh) scale(0.59);
    }
    14.2857142857% {
        transform: translateX(-32vw) translateY(-42vh) scale(0.34);
    }
    17.8571428571% {
        transform: translateX(50vw) translateY(-27vh) scale(0.46);
    }
    21.4285714286% {
        transform: translateX(-26vw) translateY(27vh) scale(0.58);
    }
    25% {
        transform: translateX(-48vw) translateY(6vh) scale(0.36);
    }
    28.5714285714% {
        transform: translateX(29vw) translateY(30vh) scale(0.65);
    }
    32.1428571429% {
        transform: translateX(47vw) translateY(38vh) scale(0.55);
    }
    35.7142857143% {
        transform: translateX(48vw) translateY(-45vh) scale(0.79);
    }
    39.2857142857% {
        transform: translateX(-34vw) translateY(25vh) scale(0.28);
    }
    42.8571428571% {
        transform: translateX(-45vw) translateY(-23vh) scale(0.53);
    }
    46.4285714286% {
        transform: translateX(-8vw) translateY(26vh) scale(0.81);
    }
    50% {
        transform: translateX(-13vw) translateY(-44vh) scale(0.82);
    }
    53.5714285714% {
        transform: translateX(21vw) translateY(-46vh) scale(0.8);
    }
    57.1428571429% {
        transform: translateX(40vw) translateY(-9vh) scale(0.54);
    }
    60.7142857143% {
        transform: translateX(-32vw) translateY(40vh) scale(0.88);
    }
    64.2857142857% {
        transform: translateX(-1vw) translateY(38vh) scale(0.43);
    }
    67.8571428571% {
        transform: translateX(15vw) translateY(-24vh) scale(0.63);
    }
    71.4285714286% {
        transform: translateX(42vw) translateY(26vh) scale(0.83);
    }
    75% {
        transform: translateX(15vw) translateY(35vh) scale(0.42);
    }
    78.5714285714% {
        transform: translateX(-42vw) translateY(-19vh) scale(0.46);
    }
    82.1428571429% {
        transform: translateX(13vw) translateY(-13vh) scale(0.71);
    }
    85.7142857143% {
        transform: translateX(25vw) translateY(-16vh) scale(0.83);
    }
    89.2857142857% {
        transform: translateX(8vw) translateY(46vh) scale(0.88);
    }
    92.8571428571% {
        transform: translateX(19vw) translateY(-36vh) scale(0.54);
    }
    96.4285714286% {
        transform: translateX(39vw) translateY(33vh) scale(0.28);
    }
    100% {
        transform: translateX(-20vw) translateY(28vh) scale(1);
    }
}
.lolfly:nth-child(2) {
    animation-name: move2;
}
.lolfly:nth-child(2)::before {
    animation-duration: 14s;
}
.lolfly:nth-child(2)::after {
    animation-duration: 14s, 9465ms;
    animation-delay: 0ms, 8141ms;
}

@keyframes move2 {
    0% {
        transform: translateX(3vw) translateY(11vh) scale(0.84);
    }
    5.8823529412% {
        transform: translateX(-24vw) translateY(13vh) scale(0.32);
    }
    11.7647058824% {
        transform: translateX(30vw) translateY(50vh) scale(0.67);
    }
    17.6470588235% {
        transform: translateX(-32vw) translateY(8vh) scale(0.79);
    }
    23.5294117647% {
        transform: translateX(30vw) translateY(-42vh) scale(0.99);
    }
    29.4117647059% {
        transform: translateX(37vw) translateY(24vh) scale(0.78);
    }
    35.2941176471% {
        transform: translateX(-15vw) translateY(-1vh) scale(0.3);
    }
    41.1764705882% {
        transform: translateX(46vw) translateY(23vh) scale(0.4);
    }
    47.0588235294% {
        transform: translateX(48vw) translateY(41vh) scale(0.67);
    }
    52.9411764706% {
        transform: translateX(-19vw) translateY(16vh) scale(0.98);
    }
    58.8235294118% {
        transform: translateX(-6vw) translateY(-39vh) scale(0.42);
    }
    64.7058823529% {
        transform: translateX(3vw) translateY(-12vh) scale(0.89);
    }
    70.5882352941% {
        transform: translateX(29vw) translateY(40vh) scale(0.47);
    }
    76.4705882353% {
        transform: translateX(47vw) translateY(22vh) scale(0.93);
    }
    82.3529411765% {
        transform: translateX(-39vw) translateY(7vh) scale(0.92);
    }
    88.2352941176% {
        transform: translateX(30vw) translateY(-41vh) scale(0.57);
    }
    94.1176470588% {
        transform: translateX(-47vw) translateY(35vh) scale(0.97);
    }
    100% {
        transform: translateX(34vw) translateY(38vh) scale(0.78);
    }
}
.lolfly:nth-child(3) {
    animation-name: move3;
}
.lolfly:nth-child(3)::before {
    animation-duration: 14s;
}
.lolfly:nth-child(3)::after {
    animation-duration: 14s, 7282ms;
    animation-delay: 0ms, 5003ms;
}

@keyframes move3 {
    0% {
        transform: translateX(13vw) translateY(-20vh) scale(0.8);
    }
    3.7037037037% {
        transform: translateX(-48vw) translateY(20vh) scale(0.93);
    }
    7.4074074074% {
        transform: translateX(43vw) translateY(35vh) scale(0.63);
    }
    11.1111111111% {
        transform: translateX(-4vw) translateY(35vh) scale(0.5);
    }
    14.8148148148% {
        transform: translateX(9vw) translateY(50vh) scale(0.57);
    }
    18.5185185185% {
        transform: translateX(40vw) translateY(-26vh) scale(0.75);
    }
    22.2222222222% {
        transform: translateX(31vw) translateY(13vh) scale(0.42);
    }
    25.9259259259% {
        transform: translateX(18vw) translateY(6vh) scale(0.92);
    }
    29.6296296296% {
        transform: translateX(-38vw) translateY(-34vh) scale(0.68);
    }
    33.3333333333% {
        transform: translateX(6vw) translateY(-11vh) scale(0.74);
    }
    37.037037037% {
        transform: translateX(-38vw) translateY(24vh) scale(0.72);
    }
    40.7407407407% {
        transform: translateX(33vw) translateY(49vh) scale(0.89);
    }
    44.4444444444% {
        transform: translateX(-35vw) translateY(-42vh) scale(0.91);
    }
    48.1481481481% {
        transform: translateX(11vw) translateY(34vh) scale(0.73);
    }
    51.8518518519% {
        transform: translateX(-13vw) translateY(17vh) scale(0.57);
    }
    55.5555555556% {
        transform: translateX(21vw) translateY(15vh) scale(0.39);
    }
    59.2592592593% {
        transform: translateX(-32vw) translateY(41vh) scale(0.76);
    }
    62.962962963% {
        transform: translateX(47vw) translateY(-48vh) scale(0.7);
    }
    66.6666666667% {
        transform: translateX(-29vw) translateY(-38vh) scale(0.69);
    }
    70.3703703704% {
        transform: translateX(-18vw) translateY(49vh) scale(0.41);
    }
    74.0740740741% {
        transform: translateX(32vw) translateY(-47vh) scale(0.87);
    }
    77.7777777778% {
        transform: translateX(37vw) translateY(-47vh) scale(0.67);
    }
    81.4814814815% {
        transform: translateX(7vw) translateY(39vh) scale(0.91);
    }
    85.1851851852% {
        transform: translateX(-44vw) translateY(-15vh) scale(0.71);
    }
    88.8888888889% {
        transform: translateX(-10vw) translateY(10vh) scale(0.64);
    }
    92.5925925926% {
        transform: translateX(11vw) translateY(-39vh) scale(0.6);
    }
    96.2962962963% {
        transform: translateX(-3vw) translateY(9vh) scale(0.74);
    }
    100% {
        transform: translateX(32vw) translateY(27vh) scale(0.4);
    }
}
.lolfly:nth-child(4) {
    animation-name: move4;
}
.lolfly:nth-child(4)::before {
    animation-duration: 11s;
}
.lolfly:nth-child(4)::after {
    animation-duration: 11s, 7023ms;
    animation-delay: 0ms, 533ms;
}

@keyframes move4 {
    0% {
        transform: translateX(21vw) translateY(33vh) scale(0.63);
    }
    4% {
        transform: translateX(-31vw) translateY(-39vh) scale(0.46);
    }
    8% {
        transform: translateX(36vw) translateY(39vh) scale(0.5);
    }
    12% {
        transform: translateX(-23vw) translateY(-29vh) scale(0.97);
    }
    16% {
        transform: translateX(-30vw) translateY(-20vh) scale(0.97);
    }
    20% {
        transform: translateX(-20vw) translateY(21vh) scale(0.76);
    }
    24% {
        transform: translateX(-28vw) translateY(-42vh) scale(0.34);
    }
    28% {
        transform: translateX(37vw) translateY(-33vh) scale(0.9);
    }
    32% {
        transform: translateX(44vw) translateY(-44vh) scale(0.68);
    }
    36% {
        transform: translateX(4vw) translateY(44vh) scale(0.94);
    }
    40% {
        transform: translateX(10vw) translateY(-35vh) scale(0.6);
    }
    44% {
        transform: translateX(9vw) translateY(-33vh) scale(0.46);
    }
    48% {
        transform: translateX(-33vw) translateY(-27vh) scale(0.67);
    }
    52% {
        transform: translateX(-9vw) translateY(-7vh) scale(0.41);
    }
    56% {
        transform: translateX(-45vw) translateY(40vh) scale(0.84);
    }
    60% {
        transform: translateX(12vw) translateY(-5vh) scale(0.96);
    }
    64% {
        transform: translateX(0vw) translateY(-27vh) scale(0.44);
    }
    68% {
        transform: translateX(-15vw) translateY(23vh) scale(0.77);
    }
    72% {
        transform: translateX(-36vw) translateY(-10vh) scale(0.3);
    }
    76% {
        transform: translateX(-43vw) translateY(-38vh) scale(0.3);
    }
    80% {
        transform: translateX(-46vw) translateY(11vh) scale(0.6);
    }
    84% {
        transform: translateX(28vw) translateY(32vh) scale(0.48);
    }
    88% {
        transform: translateX(-24vw) translateY(29vh) scale(0.59);
    }
    92% {
        transform: translateX(25vw) translateY(-34vh) scale(0.68);
    }
    96% {
        transform: translateX(-48vw) translateY(-20vh) scale(0.69);
    }
    100% {
        transform: translateX(-36vw) translateY(-17vh) scale(0.56);
    }
}
.lolfly:nth-child(5) {
    animation-name: move5;
}
.lolfly:nth-child(5)::before {
    animation-duration: 13s;
}
.lolfly:nth-child(5)::after {
    animation-duration: 13s, 8655ms;
    animation-delay: 0ms, 5809ms;
}

@keyframes move5 {
    0% {
        transform: translateX(27vw) translateY(-14vh) scale(0.38);
    }
    4.5454545455% {
        transform: translateX(33vw) translateY(-26vh) scale(0.78);
    }
    9.0909090909% {
        transform: translateX(7vw) translateY(25vh) scale(0.39);
    }
    13.6363636364% {
        transform: translateX(-47vw) translateY(9vh) scale(0.91);
    }
    18.1818181818% {
        transform: translateX(-45vw) translateY(-5vh) scale(0.59);
    }
    22.7272727273% {
        transform: translateX(29vw) translateY(-24vh) scale(0.49);
    }
    27.2727272727% {
        transform: translateX(17vw) translateY(-34vh) scale(0.39);
    }
    31.8181818182% {
        transform: translateX(48vw) translateY(-6vh) scale(0.87);
    }
    36.3636363636% {
        transform: translateX(-45vw) translateY(34vh) scale(0.57);
    }
    40.9090909091% {
        transform: translateX(-14vw) translateY(-31vh) scale(0.36);
    }
    45.4545454545% {
        transform: translateX(-20vw) translateY(6vh) scale(0.45);
    }
    50% {
        transform: translateX(-11vw) translateY(-5vh) scale(0.36);
    }
    54.5454545455% {
        transform: translateX(40vw) translateY(-35vh) scale(0.46);
    }
    59.0909090909% {
        transform: translateX(-35vw) translateY(41vh) scale(0.6);
    }
    63.6363636364% {
        transform: translateX(-5vw) translateY(22vh) scale(0.79);
    }
    68.1818181818% {
        transform: translateX(48vw) translateY(-10vh) scale(0.46);
    }
    72.7272727273% {
        transform: translateX(46vw) translateY(-46vh) scale(0.69);
    }
    77.2727272727% {
        transform: translateX(45vw) translateY(9vh) scale(0.83);
    }
    81.8181818182% {
        transform: translateX(-26vw) translateY(-23vh) scale(0.92);
    }
    86.3636363636% {
        transform: translateX(-49vw) translateY(-36vh) scale(0.41);
    }
    90.9090909091% {
        transform: translateX(14vw) translateY(-8vh) scale(0.67);
    }
    95.4545454545% {
        transform: translateX(38vw) translateY(-35vh) scale(0.58);
    }
    100% {
        transform: translateX(38vw) translateY(39vh) scale(0.29);
    }
}
<div id='lolfly-container'></div>
<button onclick='addLolFlies()'>FLIES!</button>
<button onclick='paintFlies()'>PINK FLIES!</button>

相关问题