简体   繁体   English

动态动画的问题

[英]Issues with dynamic animations

I am creating an object which has 2 properties: 我正在创建一个具有2个属性的对象:

animationName - an array with the names of pre-made @keyfame animations animationName-具有预制@keyfame动画名称的数组

&

animate - a function which accepts a target, animation name, duration and timing function 动画 -接受目标,动画名称,持续时间和计时功能的功能

I have the animate function checking that atleast one of the selected targets exist and I am also making sure that the animation name matches one of the indexes in animationName. 我具有动画功能,检查是否存在至少一个所选目标,并且还要确保动画名称与animationName中的索引之一匹配。

If I manually enter the style attribute and animation information, it works as I would expect, however, I cannot seem to get the code to work in the JS! 如果我手动输入style属性和动画信息,则它可以按我期望的那样工作,但是,我似乎无法使代码在JS中工作!

I have tried different things such as .prop() but I am pretty sure .attr() is right. 我已经尝试了诸如.prop()但是我很确定.attr()是正确的。

Here is the JS: 这是JS:

var animateElement = {
            //put in our animations that we know exist
            animationName: ["bounce", "shake"],
            //set up an animate renderer
            animate: function(target, animationName, duration, timingFunction) {
                        //cache the known animations in an easy to use variable
                        var selectedAnim = this.animationName;

                        //query the target to make sure it exists
                        var el = document.querySelectorAll(target);

                        //make sure atleast one of the targets exist
                        if (el.length != -1) {
                                    //check if the parameter animation is equal to one of our available animations
                                    if ($.inArray(animationName, selectedAnim) != -1) {
                                                //if the animation exists, change the style attribute of the target element to run the animation
                                                el.attr("style", "animation:" + animationName + " " + duration + " " + timingFunction);
                                    } else {
                                                //otherwise alert that the selected animation is invalid (doesn't match our array of known animations)
                                                alert("invalid animation selected");
                                    }
                        }
            },
}
animateElement.animate("button", "shake", "0.25s", "infinite");

SASS: 上海社会科学院:

@-webkit-keyframes shake 
    0%
        transform: translateX(0)
    25%
        transform: translateX(-25px)
    50%
        transform: translateX(0)
    75%
        transform: translateX(25px)
    100%
        transform: translateX(0)

@keyframes shake 
    0%
        transform: translateX(0)
    25%
        transform: translateX(-25px)
    50%
        transform: translateX(0)
    75%
        transform: translateX(25px)
    100%
        transform: translateX(0)

@-webkit-keyframes bounce 
    0%
        transform: translateY(0)
    25%
        transform: translateY(-25px)
    50%
        transform: translateY(0)
    75%
        transform: translateY(25px)
    100%
        transform: translateY(0)

@keyframes bounce 
    0%
        transform: translateY(0)
    25%
        transform: translateY(-25px)
    50%
        transform: translateY(0)
    75%
        transform: translateY(25px)
    100%
        transform: translateY(0)

There are two issues with your code which is preventing it from working properly and they are as follows: 您的代码存在两个问题,导致其无法正常工作,如下所示:

  1. document.querySelectorAll returns a nodelist and so you can't directly set attributes. document.querySelectorAll返回一个节点列表,因此您不能直接设置属性。 You either have to loop through the returned nodes (or) assign the attributes to one single item in the node list using [x] . 您要么必须遍历返回的节点(或者)使用[x]将属性分配给节点列表中的一项。
  2. .attr() is a jQuery method but the el is not a jQuery object. .attr()是jQuery方法,但el不是jQuery对象。 You need to use the vanilla JS equivalent which is .setAttribute . 您需要使用.setAttribute JS等效项。

If you want to test by applying the animation property (through style attribute) for one node then use the below code and it will apply the property to only the first node returned. 如果要通过将动画属性(通过样式属性)应用于一个节点进行测试,请使用以下代码,它将仅将该属性应用于返回的第一个节点。

el[0].setAttribute("style", "-webkit-animation:" + animationName + " " + duration + " " + timingFunction);

For your actual scenario, traverse through all nodes returned by using a for loop like below and then assign the animation property: 对于您的实际情况,遍历使用如下所示的for循环返回的所有节点,然后分配animation属性:

for (var i = 0; i < el.length; i++) {
  el[i].setAttribute("style", "animation:" + animationName + " " + duration + " " + timingFunction);
}

Below is a sample snippet with a random animation effect added. 以下是添加了随机动画效果的示例代码段。 I had included the prefix library in the snippet only for supporting the older browsers (I am using one :D). 我在代码段中包含了前缀库,仅用于支持较旧的浏览器(我正在使用一个:D)。

 var animateElement = { //put in our animations that we know exist animationName: ["bounce", "shake"], //set up an animate renderer animate: function(target, animationName, duration, timingFunction) { //cache the known animations in an easy to use variable var selectedAnim = this.animationName; //query the target to make sure it exists var el = document.querySelectorAll(target); //make sure atleast one of the targets exist if (el.length != -1) { //check if the parameter animation is equal to one of our available animations if ($.inArray(animationName, selectedAnim) != -1) { //if the animation exists, change the style attribute of the target element to run the animation el[0].setAttribute("style", "animation:" + animationName + " " + duration + " " + timingFunction); } else { //otherwise alert that the selected animation is invalid (doesn't match our array of known animations) alert("invalid animation selected"); } } }, } animateElement.animate("div", "shake", "0.25s", "infinite"); 
 @keyframes shake { from { transform: translateX(200px); } to { transform: translateX(0px); } } div { height: 200px; width: 200px; border: 1px solid; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Some content</div> 

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

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