jQuery fadeIn 和 fadeOut - 交换 div
总的来说,我对 jQuery 和 javascript 很陌生,但感谢这些网站,我才得以顺利通过。
我有一个页面,其中有六个隐藏的 div,通过带有各个 id 的相应链接进行调用。当每个 div 变得可见 (.fadeIn) 时,当前显示的 div 将被隐藏 (.fadeOut)。
一切正常,但我的代码似乎非常长且笨拙。 有没有一种更简单、更短、代码密集程度更少的方法来执行相同的任务?
这是我的js:
非常感谢。 迈克
$(document).ready(function(){
$("#link1").click(function() {
$("#panel2").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel1").fadeIn("slow")});
});
});
});
});
});
$("#link2").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel2").fadeIn("slow")});
});
});
});
});
});
$("#link3").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel2").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel3").fadeIn("slow")});
});
});
});
});
});
$("#link4").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel2").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel4").fadeIn("slow")});
});
});
});
});
});
$("#link5").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel2").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel5").fadeIn("slow")});
});
});
});
});
});
$("#link6").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel2").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeIn("slow")});
});
});
});
});
});
});
I'm new to jQuery and javascript in general but am plugging my way through thanks to sites like these.
I have a page with six hidden divs that are called with corresponding links with individual ids. When each div is made visable (.fadeIn), the currently displayed div is hidden (.fadeOut).
It all works fine but my code seems to be really long and ungainly.
Is there an easier, shorter, less code-intensive way to perform the same task please?
Here is my js:
Many thanks in advance.
Mike
$(document).ready(function(){
$("#link1").click(function() {
$("#panel2").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel1").fadeIn("slow")});
});
});
});
});
});
$("#link2").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel2").fadeIn("slow")});
});
});
});
});
});
$("#link3").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel2").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel3").fadeIn("slow")});
});
});
});
});
});
$("#link4").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel2").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel4").fadeIn("slow")});
});
});
});
});
});
$("#link5").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel2").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel6").fadeOut("slow", function() {
$("#panel5").fadeIn("slow")});
});
});
});
});
});
$("#link6").click(function() {
$("#panel1").fadeOut("slow", function() {
$("#panel2").fadeOut("slow", function() {
$("#panel3").fadeOut("slow", function() {
$("#panel4").fadeOut("slow", function() {
$("#panel5").fadeOut("slow", function() {
$("#panel6").fadeIn("slow")});
});
});
});
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有了你的 ids 可能是这样的:
Having your ids it might be like this:
这段代码当然可以变得更加高效和灵活,但对于上面的简单 6 元素示例来说应该足够了。这主要是作为概念验证而完成的。
我选择以编程方式添加类,但理想情况下您应该在 HTML 中添加类。如果是我,我可能也会使用 Expandos 而不是 id 字符串替换。
编辑,添加修复:
顺序动画的递归函数可确保在正确的时间处理淡入。可能有更有效的方法,例如使用计数器,但对于只有 6 个元素应该没问题,并且这更忠实地匹配您的原始代码。
通过停止并完成动画,修复了在错误时间处理动画的问题,例如当您同时单击多个链接时,导致多个面板尝试淡入。
This code could certainly be made more efficient and flexible, but for a simple 6 element example as the above it should be enough. This was mostly done as just a proof of concept.
I chose to add the classes programmatically, but ideally you should have the classes added in the HTML. If it were me I would probably also have used expandos instead of id string replacement.
EDIT, fixes added:
Recursive function for sequential animation makes sure that fadeIn is processed at the right time. There may be a more efficient method for this, such as using a counter, but for just 6 elements it should be fine, and this matches your original code more faithfully.
Fix for animations processing at incorrect times, such as when you click many links simultaneously, causing multiple panels to try to fadeIn, by stopping and finishing animations.
最好的方法是使用 http://api.jquery.com/toggle/
Toggle 让你一个动画
至于真正缩短代码,我会为您的面板使用类标识符。
识别面板组
,然后为关联的 HTML 元素分配一个类,如
您的 jQuery 现在可以是
$(".panelGroup1").fadeOut...
。 jQuery 的类选择器,# 是 id 选择器。
查看选择器,您可以做很多其他疯狂的事情
http:// api.jquery.com/?ns0=1&s=Selector&go=
The best way would be to use http://api.jquery.com/toggle/
Toggle lets you the animation for one
As for truely Shortening the code I would use a class identifier for your panels.
Identify panel groups like
and then assign the associated HTML elements a class like
Your jQuery can now be
$(".panelGroup1").fadeOut...
The . class selector for jQuery and the # is the id Selector.
Check out selectors and you can do a bunch of other crazy things
http://api.jquery.com/?ns0=1&s=Selector&go=
很多好的解决方案,我有一个一般性的评论:
每个面板都有一个唯一的 id 似乎有点不必要。您可能有充分的理由这样做,但是用类选择器对面板进行分组还不够吗?这将使您的整体代码以及 jquery 更加简单且易于维护。
Lots of good solutions, I have a general comment:
It seems a bit unnecessary for each panel to have a unique id. You might have a perfect reason to do so, but wouldn't it be enough to group your panels with a class selector? This would make your overall code, as well as your jquery, much simpler and easier to maintain.
你可以做类似的事情
然后:
如果你想要淡出的元素和淡入的元素占据相同的空间,请记住调整你的元素
位置
You could do something like
And then:
remember to adjust your elements positioning if you want the one that's fading out and the one that's fading in to occupy the same space
cheers