Ich benutze jQuery, um einfache Animationen für ein Login-Formular in meinem CMS zu erstellen. JQuery schaltet die Sichtbarkeit von zwei Formularen, einem Login-Formular und einem Reset-Passwort-Formular um.Refactoring Animationen in jQuery Login-Skript
Das Skript funktioniert einwandfrei, aber ich finde, wie man es verkürzen kann, da der meiste Code redundant ist.
Hinweis: Die Funktion .click
wird jeweils zweimal aufgerufen, um zu erzwingen, dass die Animationen gleichzeitig stattfinden.
Die JS:
$(function() {
var recover_text = "Password Recovery";
var login_text = "Login to CMS";
$("input").labelify({ text: "label", labelledClass: "inside" });
$("#forgot").click(function() {
$("#cms_login").fadeOut("fast", function() {
$("#pw_recover").fadeIn();
});
});
$("#forgot").click(function() {
$("#dialog h2").fadeOut("fast", function() {
$(this).html("" + recover_text + "").fadeIn();
});
document.title = '' + recover_text + '';
});
$("#login").click(function() {
$("#pw_recover").fadeOut("fast", function() {
$("#cms_login").fadeIn();
});
});
$("#login").click(function() {
$("#dialog h2").fadeOut("fast", function() {
$(this).html("" + login_text + "").fadeIn();
});
document.title = '' + login_text + '';
});
});
Und die (gekürzte) HTML:
<div id="form-wrap">
<form id="cms_login">
...login fields...
<a id="forgot" href="#">Forgot password?</a>
</form>
<form id="pw_recover">
...reset password fields...
<a id="login" href="#">⇐ Back to login</a>
</form>
</div>
#pw_recover { display: none; }
Wie kann ich optimieren/dieses Skript umrüsten? Irgendwelche Gedanken?
Brilliant!Nun, wo würde man 'return false 'einfügen, um zu verhindern, dass die Seite nach oben springt? – peehskcalba