如何淡出、定值、淡入;但要做得“更好”
假设一个元素的值为 55:
<span id="some">55</span>
我想:
- 淡出元素
- 设置值 44
- 淡入元素
所以我尝试了:
$("#some").fadeOut("slow").html("44").fadeIn("slow");
但是上面的首先将跨度的内容设置为 44,然后< /em> 淡出并淡入。
所以我尝试使用回调:
function fadeOutComplete(){
$("#some").html("<%= @cc %>").fadeIn("slow");
}
$("#some").fadeOut("slow",fadeOutComplete);
现在这可以工作,但它看起来和感觉都很笨拙。有什么方法可以编写这个 DRYer 和更多 jQuery-er 吗? (甚至不确定jQuery-er是什么意思!)
我如何传入要设置值的元素以及要设置为fadeOutComplete
的值,所以我可以使该回调变得通用吗?
Say a element has value 55:
<span id="some">55</span>
I want to:
- fade out the element
- set value 44
- fade in the element
So I tried:
$("#some").fadeOut("slow").html("44").fadeIn("slow");
But the above first sets the span's content to 44, and then fades out and fades in.
So I tried with a callback:
function fadeOutComplete(){
$("#some").html("<%= @cc %>").fadeIn("slow");
}
$("#some").fadeOut("slow",fadeOutComplete);
Now this works, but it's looks and feels clunky. Is there some way to write this DRYer and more jQuery-er? (not even sure what I mean by jQuery-er!)
How could I pass in the element whose value is to be set and the value to be set to fadeOutComplete
so I can make that callback sort-of generic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这样:
try like this:
这是淡入淡出命令的问题。该命令异步运行,这意味着当它淡出时,文本正在更改。查看这个问题的答案:jQuery 同步操作
This is a problem with the fade command. The command runs asyncronous, meaning while it is fading out the text is being changed. Look at this question for an answer: jQuery synchronous operation
检查这个...
fadeOut()
的完整回调中,this
指向原生 DOM 元素。这允许您以 DRY 方式再次引用它。Check this...
fadeOut()
,this
is pointing to the native DOM element. This allows you to reference it again in a DRY way.相同的方法,但有一些清洁:
Same approach but with some cleanliness: