当我将动画效果包装在函数中时,它停止工作。谁能告诉我为什么?
我将 css 和 javascript 复制到 jsfiddle 中,但我不知道在 html 中放入什么,无论如何,我试图让单个图像脉冲,问题是它仅定义为一个类,而我遇到麻烦了。当我单独拥有它作为它自己的功能时,它工作正常 http://jsfiddle.net/EQs9N/,但是当我将它包装在一个不可见的函数(或任何它的名称)中时,动画停止触发 http://jsfiddle.net/EQs9N/1/。这与我为班级制作动画有什么关系吗?无论如何,是否可以将其定义为对象,这会有帮助吗?谢谢
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. 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? Is there anyway to define it as an object, and would this help? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在声明函数 A,但没有调用该函数。
这将创建一个匿名函数(就是您正在寻找的术语)并执行它。我不确定为什么在这种情况下需要将其包装起来,
$(document).ready()
应该足够了。关于 jsfiddle 使用的旁注:
在 jsfiddle 中,您可以从框架下拉列表中选择 onDomReady 而不是 onLoad,以自动将 javascript 包装在
$(document).ready()
中,从而调用$(document).ready()多余的。 onLoad 选项会将其包装在
$(window).load()
中,如果您选择“不包装”,则 jsfiddle 只会用必要的脚本标签将其包围,然后将其放在 head 或正文,具体取决于所选选项。
You are declaring a function A, but you aren't calling the function.
That will create an anonymous function (is that the term you were looking for) and execute it. I'm not sure why you need to wrap it in this case,
$(document).ready()
should be sufficient.Side note on jsfiddle usage:
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. The option onLoad will wrap it in$(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.
$(document).ready(function() {
是 jquery 将任何代码执行延迟到页面完全加载后的方式。如果将这部分代码包装在函数中,则不会 函数时才会调用该代码,这就是为什么在包装代码时它不起作用的原因。
不再工作,并且只有在调用该 文件准备就绪” rel="nofollow">本教程用于很好地使用
$(document).ready
$(document).ready(function() {
is the way jquery delays any code execution to after the page is fully loaded.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. That's why it doesn't work when you wrap your code.
See this tuto for the good use of
$(document).ready
在 你的第二个 jsfiddle 中,你正在定义一个函数(函数 A),但实际上并没有调用它。
如果您稍微更改代码并将
A
传递给document.ready
(而不是传递匿名函数),您应该会发现它有效:In your second jsfiddle you're defining a function (function A) but not actually calling it.
If you change your code slightly and pass
A
todocument.ready
(instead of passing an anonymous function) you should find that it works: