Does anyone know of a good jQuery content switcher? I want to switch a log in and register form.
+5
A:
You can do something like this with minimal code, for example if you had:
<div class="links">
<a href="#login">Login</a>
<a href="#register">Register</a>
</div>
<div id="login" class="panels">
Login Stuff
</div>
<div id="register" class="panels">
Registration Stuff
</div>
You could do jQuery like this:
$(".links a").click(function() {
$(".panels").hide();
$(this.hash).fadeIn();
});
Just hide both initially with CSS if you wanted, like this:
.panels { display: none; }
When you clicked either link above, it would hide the other panels, and fade in the one you wanted to switch to for a nice effect...use the same convention as above and you could add any number of panels and links.
Some other pre-built alternatives if you want more complicated content arrangement would be something like:
Nick Craver
2010-04-25 05:09:03
+1 Which is probably what the switcher would do in 30KB of code.
Ben
2010-04-25 05:15:51
This didn't work for me. My source:<html><head><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"></script><script type="text/javascript">$(".links a").click(function() { $(".panels").hide(); $(this.hash).fadeIn();});</script><style type="text/css">.panels { display: none; }</style></head><body><div class="links"> <a href="#login">Login</a> <a href="#register">Register</a></div><div id="login" class="panels"> Login Stuff</div><div id="register" class="panels"> Regist</body></html>
Christopher
2010-04-27 00:11:29
@Christopher - The html you posted is missing the `</div>` for the `<div id="register">`, add this and it should work, here's a full example of what you posted running: http://jsfiddle.net/PbkyG/
Nick Craver
2010-04-27 00:16:46