简体   繁体   English

Web 动画 API - 如何在关键帧脚本中使用变量?

[英]Web Animations API - How to use a variable inside the keyframes script?

I have a simple animation test shown in the code below.我有一个简单的 animation 测试,如下面的代码所示。 This is just for the benefit of demonstrating the issue I'm currently having.这只是为了展示我目前遇到的问题。 If I hard code the hex color values in the keyframes script, everything works fine.如果我在关键帧脚本中硬编码十六进制颜色值,一切正常。 For my project I need to be able to override the color values using a variable, you can see in the keyframes code I have replaced a hard coded Hex value with a var called 'markerColor' but as soon as I do this the animation wont run that line item.对于我的项目,我需要能够使用变量覆盖颜色值,你可以在关键帧代码中看到我已经用一个名为“markerColor”的变量替换了一个硬编码的十六进制值,但是一旦我这样做,animation 就不会运行该订单项。 It will be likely something wrong with the syntax but I cant work out what the solution is, any help would be great thanks.语法可能有问题,但我无法弄清楚解决方案是什么,任何帮助都会非常感谢。

<header>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/web-animations/2.3.2/web-animations.min.js"> 
 </script>
</header>


<!-- Set up a target to animate -->
<div class="text-light" style="width: 150px;" id="testDivElement">Hello world!</div>

<script>

 var markerColor;
 var markerAlarmColor;


// THIS IS COMMENTED OT BUT DOES WORK
//// assign keyframes
//var marker1keyframes = [
//    {
//        backgroundColor: '#004A7F',
//        boxShadow: '0 0 10px #004A7F'
//    },
//    {
//        backgroundColor: '#00cc00',
//        boxShadow: '0 0 30px #00cc00'
//    },
//    {
//        backgroundColor: '#004A7F',
//        boxShadow: '0 0 10px #004A7F'
//    }
//];

// THIS IS PART I'M HAVING AN ISSUE WITH
// assign keyframes
var marker1keyframes = [
    {
        backgroundColor: '' + markerColor + '', // not working 
        boxShadow: '0 0 10px #004A7F'
    },
    {
        backgroundColor: '#00cc00',
        boxShadow: '0 0 30px #00cc00'
    },
    {
        backgroundColor: '' + markerColor + '', // not working
        boxShadow: '0 0 10px #004A7F'
    }
];

// assign timings
var marker1timing = {
    duration: 1000,
    fill: "both",
    easing: "ease-in-out",
    iterations: 10,
};

markerColor = "#E6E600";
markerAlarmColor = "#E6E600";

var test = document.getElementById("testDivElement").animate(
        marker1keyframes,
        marker1timing
    )

Turns out I had to declare the variable values before the keyframes script, in the answer below the variables have been moved before the keyframes script, this works for me now.原来我必须在关键帧脚本之前声明变量值,在下面的答案中,变量已在关键帧脚本之前移动,现在这对我有用。

 <script>

var markerColor = "#E6E600";
var markerAlarmColor = "#E6E600";


// assign keyframes
var marker1keyframes = [
    {
        backgroundColor: '' + markerColor + '',  
        boxShadow: '0 0 10px #004A7F'
    },
    {
        backgroundColor: '#00cc00',
        boxShadow: '0 0 30px #00cc00'
    },
    {
        backgroundColor: '' + markerColor + '', 
        boxShadow: '0 0 10px #004A7F'
    }
];

// assign timings
var marker1timing = {
    duration: 1000,
    fill: "both",
    easing: "ease-in-out",
    iterations: 10,
};

var test = document.getElementById("testDivElement").animate(
        marker1keyframes,
        marker1timing
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM