如果 $(element) hasClass 那么 .animate() 不起作用?
当一个元素具有“activeSlide”类时,由于某种原因它无法按预期运行,我正在努力为箭头设置动画。我不确定这是为什么,任何人都可以提供一些关于我做错了什么的见解吗?
$(document).ready(function() {
if($('#navitem-2').hasClass("activeSlide")){
$("#navarro").animate({marginLeft:"220px"}, 500);
};
});
谢谢!
*更新*:以下是类在单击时更改,但动画不起作用的示例:http://jsfiddle.net/somewhereinsf/pn5sx/1/
Im working on animating an arrow when an element has a class of "activeSlide" for some reason it is not functioning as expected. Im not sure why this is, can anyone provide a little insight in to what im doing wrong?
$(document).ready(function() {
if($('#navitem-2').hasClass("activeSlide")){
$("#navarro").animate({marginLeft:"220px"}, 500);
};
});
Thanks!
*Update*: Here is an examaple where the classes change onclick, but the animation does not function: http://jsfiddle.net/somewhereinsf/pn5sx/1/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
条件下,这应该 100% 有效。
#navitem-2
存在的activeSlide
类是#navitem-2
的子级#navarro< /代码> 存在。
如果您有一个控制台,例如 Google Chrom 开发人员工具,您可以在 javascript 中添加一些日志记录机制
,然后尝试:
您可以看到日志控制台中显示的内容。
This should work 100% under the conditions
#navitem-2
existsactiveSlide
is a child of#navitem-2
#navarro
exists.if you have a console such as Google Chrom Developer Tools you can add some logging mechanism in your javascript
And then try:
and you can see what's appearing in the log console.
您只在加载时运行动画一次...您需要在每次单击时运行它们。
这是使用您发布的代码的快速示例。
HTH,
查尔斯
You're only running the animations once on load... you need to run them on each click.
Here's quick example using the code you posted.
HTHs,
Charles
您的代码可以正常工作,只需确保
#navarrow
的布局方式与 marginLeft 相关,即position:absolute
。演示: http://jsfiddle.net/cameronjordan/uwf9y/
根据您的评论/示例进行更新:
在此示例中,类更改和检查似乎没有任何作用,使用实时事件并直接触发动画要简单得多。
http://jsfiddle.net/cameronjordan/pn5sx/3/
如果代码需要对于比这更大的事件,我强烈建议您使用自定义事件,这样代码就不会重复,并且您可以尽可能少地关注每个代码块;动画在一处进行控制并在需要时触发。
Your code works, just make sure
#navarrow
is laid out in a way that marginLeft will matter, i.e.position:absolute
.Demo: http://jsfiddle.net/cameronjordan/uwf9y/
Updated based on your comment/example:
The class change and checking does not seem to be serving any purpose in this example it is much more straightforward to use live events and trigger the animation directly.
http://jsfiddle.net/cameronjordan/pn5sx/3/
If the code needed to be any bigger than this I highly encourage you to use custom events so that code is not repeated and you can keep each code chunk focused on as little as possible; the animation is controlled in one place and triggered wherever needed.