[英]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.