简体   繁体   English

当我将 animation 效果包装在 function 中时,它停止工作。 谁能告诉我为什么?

[英]When I wrap my animation effect in a function, it stops working. Can anyone tell me why?

I copied the css and javascript into jsfiddle, but I don't know what to put in html, anyway, I'm trying to get a single image to pulse, the problem is it is only defined as a class, and I'm having trouble with it. I copied the css and javascript into jsfiddle, but I don't know what to put in html, anyway, I'm trying to get a single image to pulse, the problem is it is only defined as a class, and I'm遇到麻烦。 When I have it alone as it's own function it works fine http://jsfiddle.net/EQs9N/ , but when I wrap it in an invisible function (or whatever its called) the animation stops firing http://jsfiddle.net/EQs9N/1/ . When I have it alone as it's own function it works fine http://jsfiddle.net/EQs9N/ , but when I wrap it in an invisible function (or whatever its called) the animation stops firing http://jsfiddle.net/ EQs9N/1/ . Does this have something to do with me animating a class?这与我制作 class 的动画有关吗? Is there anyway to define it as an object, and would this help?无论如何将其定义为 object,这有帮助吗? Thanks谢谢

$(document).ready(function() { is the way jquery delays any code execution to after the page is fully loaded. $(document).ready(function() {是 jquery 将任何代码执行延迟到页面完全加载后的方式。

If you wrap this part of the code within a function, it won't work anymore and the code would only be called when you call the function.如果将这部分代码包装在 function 中,它将不再起作用,并且只有在调用 function 时才会调用该代码。 That's why it doesn't work when you wrap your code.这就是为什么包装代码时它不起作用的原因。

See this tuto for the good use of $(document).ready请参阅本教程以充分利用$(document).ready

In your second jsfiddle you're defining a function (function A) but not actually calling it.您的第二个 jsfiddle 中,您正在定义一个 function (函数 A),但实际上并未调用它。

If you change your code slightly and pass A to document.ready (instead of passing an anonymous function) you should find that it works:如果您稍微更改代码并将A传递给document.ready (而不是传递匿名函数),您应该会发现它有效:

function A(){
    if (1 == 1){
        function pulsate() {
            $(".image2_template").
              animate({opacity: 0.1}, 1500, 'linear').
              animate({opacity: 1}, 1500, 'linear', pulsate);
          }
        pulsate();
    } else {
        $('.image2_template').animate({
             opacity: 0,
        });
        $('.text3_template').animate({
            opacity: 0,
        });
    }
}

$(document).ready(A);

You are declaring a function A, but you aren't calling the function.您正在声明 function A,但您没有调用 function。

(function(){
    ...
})()

That will create an anonymous function (is that the term you were looking for) and execute it.这将创建一个匿名 function (即您要查找的术语)并执行它。 I'm not sure why you need to wrap it in this case, $(document).ready() should be sufficient.我不确定为什么在这种情况下需要包装它, $(document).ready()应该就足够了。

Side note on jsfiddle usage:关于 jsfiddle 用法的旁注:
In jsfiddle, you can select onDomReady instead of onLoad from the framework dropdowns to automatically wrap your javascript in $(document).ready() making your invokation of $(document).ready() redundant.在 jsfiddle 中,您可以使用 select onDomReady 而不是框架下拉菜单中的 onLoad 来自动将 javascript 包装在$(document).ready()中,从而使您对$(document).ready()的调用变得多余。 The option onLoad will wrap it in $(window).load() onLoad 选项会将其包装在$(window).load()

If you select "no wrap", then jsfiddle will only surround it with the necessary script tags and then put that in the head or the body, depending on the selected option.如果您 select “无包装”,那么 jsfiddle 只会用必要的脚本标签将其包围,然后将其放在头部或正文中,具体取决于所选选项。

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

相关问题 我的Jquery按钮-禁用/启用功能不起作用。 谁能告诉我怎么了? - My Jquery button- disable/enable function is not working. Can anyone tell me what's wrong? 谁能告诉我为什么我的Scrollspy无法正常工作? - Can anyone tell me why my Scrollspy is not working? 谁能告诉我为什么我的JavaScript点击无法正常工作? - can anyone tell me why my javascript clicks isnt working? 谁能告诉我为什么在尝试调用此函数时会出错? - Can anyone tell me why I get a error when i try to call this function? 谁能告诉我为什么这一次有效? - Can anyone tell me why this is working once? 谁能告诉我为什么我的最后2个如果其他语句不起作用? 全新的JavaScript在这里 - Can anyone tell me why my last 2 if else statements are not working? Brand new to JavaScript here 谁能告诉我为什么我的投影透视矩阵不起作用? - Can anyone tell me why my projection perspective matrix isn't working? 谁能告诉我为什么这个javascript函数调用不起作用? - Can anyone tell me why this javascript function call isn't working? 谁能告诉我为什么在不处理样式组件之前? - Can anyone tell me why before not working on styled components? 谁能告诉我为什么这个循环无法正常工作? - Can anyone tell me why this loop isn't working as it should?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM