我怎样才能使这个 jQuery 比我现有的更快?
目前,我将此脚本用于一种“选项卡”系统。单击一个选项卡时,它会隐藏所有其他选项卡。他们都是div的。但现在,我认为在选定的 div 加载之前它的褪色速度不够快。它最终会移动到所选且现在显示的 div 下方。
我不需要切换,因为如您所见,我有 5 个选项卡,我想在单击它们时打开它们各自的“_s”div。淡出、淡入。
有什么办法可以让淡出在淡入之前发生,或者添加延迟吗?我不知道如何在这个脚本中添加延迟,或者检查以确保 div 在新 div 淡入之前完全淡出。
如果有任何帮助,我将不胜感激。谢谢你!
<script>
$("#teach_one").click(function() {
$("#teach_one_s").fadeIn("slow");
$("#teach_two_s").fadeOut("fast");
$("#teach_three_s").fadeOut("fast");
$("#teach_four_s").fadeOut("fast");
$("#teach_five_s").fadeOut("fast");
});
$("#teach_two").click(function () {
$("#teach_two_s").fadeIn("slow");
$("#teach_one_s").fadeOut("fast");
$("#teach_three_s").fadeOut("fast");
$("#teach_four_s").fadeOut("fast");
$("#teach_five_s").fadeOut("fast");
});
$("#teach_three").click(function () {
$("#teach_three_s").fadeIn("slow");
$("#teach_one_s").fadeOut("fast");
$("#teach_two_s").fadeOut("fast");
$("#teach_four_s").fadeOut("fast");
$("#teach_five_s").fadeOut("fast");
});
$("#teach_four").click(function () {
$("#teach_four_s").fadeIn("slow");
$("#teach_one_s").fadeOut("fast");
$("#teach_two_s").fadeOut("fast");
$("#teach_three_s").fadeOut("fast");
$("#teach_five_s").fadeOut("fast");
});
$("#teach_five").click(function () {
$("#teach_five_s").fadeIn("slow");
$("#teach_one_s").fadeOut("fast");
$("#teach_two_s").fadeOut("fast");
$("#teach_three_s").fadeOut("fast");
$("#teach_four_s").fadeOut("fast");
});
</script>
根据您的要求,这是我的 HTML:
<ul class="noselect teach_home_navigator_tabs">
<li id="teach_one">
</li>
<li id="teach_two">
</li>
<li id="teach_three">
</li>
<li id="teach_four">
</li>
<li id="teach_five">
</li>
</ul>
<div class="infotab teach_round" id="teach_one_s">
stufff
</div>
<div class="infotab teach_round" id="teach_two_s">
stufff
</div>
<div class="infotab teach_round" id="teach_three_s">
stufff
</div>
<div class="infotab teach_round" id="teach_four_s">
stufff
</div>
<div class="infotab teach_round" id="teach_five_s">
stufff
</div>
Currently, I am using this script for a type of "tab" system. When one tab is clicked, it hides all the others. They are all div's. But right now, I don't think it's fading fast enough before the selected div loads. It ends up getting shifted underneath the div that was selected and is now showing.
I don't want a toggle, because as you can see, I have 5 tabs that I want to open their respective "_s" div when they are clicked. Fade out, fade in.
Any way to make the fade out happen before the fade in, or maybe add in a delay? I do not know how to add in a delay into this script, or to check to make sure the div is completely faded before the new div fades in.
I'd appreciate any help. Thank you!
<script>
$("#teach_one").click(function() {
$("#teach_one_s").fadeIn("slow");
$("#teach_two_s").fadeOut("fast");
$("#teach_three_s").fadeOut("fast");
$("#teach_four_s").fadeOut("fast");
$("#teach_five_s").fadeOut("fast");
});
$("#teach_two").click(function () {
$("#teach_two_s").fadeIn("slow");
$("#teach_one_s").fadeOut("fast");
$("#teach_three_s").fadeOut("fast");
$("#teach_four_s").fadeOut("fast");
$("#teach_five_s").fadeOut("fast");
});
$("#teach_three").click(function () {
$("#teach_three_s").fadeIn("slow");
$("#teach_one_s").fadeOut("fast");
$("#teach_two_s").fadeOut("fast");
$("#teach_four_s").fadeOut("fast");
$("#teach_five_s").fadeOut("fast");
});
$("#teach_four").click(function () {
$("#teach_four_s").fadeIn("slow");
$("#teach_one_s").fadeOut("fast");
$("#teach_two_s").fadeOut("fast");
$("#teach_three_s").fadeOut("fast");
$("#teach_five_s").fadeOut("fast");
});
$("#teach_five").click(function () {
$("#teach_five_s").fadeIn("slow");
$("#teach_one_s").fadeOut("fast");
$("#teach_two_s").fadeOut("fast");
$("#teach_three_s").fadeOut("fast");
$("#teach_four_s").fadeOut("fast");
});
</script>
Here's my HTML at your request:
<ul class="noselect teach_home_navigator_tabs">
<li id="teach_one">
</li>
<li id="teach_two">
</li>
<li id="teach_three">
</li>
<li id="teach_four">
</li>
<li id="teach_five">
</li>
</ul>
<div class="infotab teach_round" id="teach_one_s">
stufff
</div>
<div class="infotab teach_round" id="teach_two_s">
stufff
</div>
<div class="infotab teach_round" id="teach_three_s">
stufff
</div>
<div class="infotab teach_round" id="teach_four_s">
stufff
</div>
<div class="infotab teach_round" id="teach_five_s">
stufff
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有看到你的标记我不能确定,但是,为了简化你的 jQuery,并减少你的重复,你可以尝试使用这样的东西:
编辑 响应由提供的 html @乔什。
参考文献:
^=
。$=
。not()
。Without seeing your mark-up I can't be sure, but, and just to simplify your jQuery, and to reduce your repetition, you could try using something like this:
Edited in response to html provided by @Josh.
References:
^=
.$=
.not()
.你可以像这样重写你的代码。除了点击的 #id 之外,所有 #id 的
fadeOut
都是faddIn
You can re-write your code like this. All #id's
fadeOut
except for the one clicked, itfaddIn
根据您的 HTML 进行更新
,您可以轻松连接一些功能,如下所示:
Updated based on your HTML
and you can easily wire up some functionality like so:
这样做:
重复其余部分,基本上等待 fadeIn 完成,然后调用回调函数淡出其余部分。
但是恕我直言,您的代码可以明显缩短,如果您显示您的 html,我打赌它可以被压缩为一次
click
绑定。Do like this:
Repeat for the rest, basically this waits for fadeIn to finish then calls the callback function to fadeOut the rest.
But your code can be significantly shorter IMHO, if you show your html I bet it can be compressed into one
click
binding.使用:
其中 200 是效果持续时间毫秒
这将允许您更好地控制转换的时间
use:
where 200 is milliseconds for effect duration
This will allow you to have better control over the timing of the transitions
这是我正在使用的 HTML。
stufff
stufff
stufff
stufff
stufff
Here is my HTML that I am using.
stufff
stufff
stufff
stufff
stufff