Below is some jQuery code the i wrote to make a simple animation. However I’m SO new to it i have no idea how to condense it down and get baffled by tutorials!
<!-- Login Form Div Animation -->
<script type="text/javascript">
$(document).ready(function() {
$('#button-active').hide();
if ($("#LoginButton").hasClass("inactive")) {
$("#TopLoginForm").hide();
} else {
$("#TopLoginForm").show();
}
$("#LoginButton").click( function() {
if ($("#LoginButton").hasClass("inactive")) {
$("#LoginButton").animate({
marginTop: "-40px"
}, 500 );
$("#button-inactive").animate({
opacity: "0"
}, 500 );
$('#button-inactive').remove();
$('#button-active').hide();
$('#button-active').delay(30).fadeIn(1000);
$('#LoginWelcome').delay(0).fadeOut(1000);
$('#TopLoginForm').delay(800).fadeIn(1000);
$("#LoginButton").addClass("button");
$("#LoginButton.button").click( function() {
document.forms["LoginForm"].submit();
});
}
});
});
</script>
<!-- End Login Form Div Animation -->
The live code can be seen on http://www.trainingthemlive.co.uk/temp its at the top of the page
The big thing you can do is avoid running the same query twice, if you know the results haven’t changed. Chain or cache the results of your jQuery calls! Instead of:
you can use the chainability of jQuery objects. In fact, you’re already doing it in the second line–why not take the extra step?
For readability:
But that’s not the whole answer. Chaining is concise and great, if you need to do a bunch of stuff to one object or collection in sequence. But if you need to access one object, like
button-active, at several different points during execution, you should store the query as a variable.All together, with a few other efficiency tricks: