jeudi 16 avril 2015

jQuery - Why doesn't the alert animation work?

Good day, I wrote this script which is supposed to show and animate an alert when clicking the submit button of a form. Showing the alert works fine but I can't get the animation to work. I hope you guys know what's wrong. By the way I have jQuery 1.11.2 as a CDN.


This is the opening tag of the form:



<form method="post" name="profile-form" onsubmit="profileSaved(); return false;" role="form">


this is the form submit button:



<button type="submit" class="btn btn-pink btn-block">Save</button>


And this is the script I wrote:



function profileSaved(error) {
var alertbarContainer = $("#alertbar-container");
var alertbar = $("#alertbar-container.alert");

alertbarContainer.find(".alert").remove();

alertbar
.css({
opacity: "0",
top: "-32px"
})
.prependTo(alertbarContainer)
.animate({
top: "0px",
opacity: "1"
});

setTimeout(function () {
alertbar.animate({ top: "-32px", opacity: "0" }, function () {
$(this).remove();
});
}, 3000);

if(!error){
$("#alert-container").append('<li class="alert alert-success" role="alert">Profile saved</li>');
} else {
$("#alert-container").append('<li class="alert alert-danger" role="alert">Saving profile failed</li>');
}
}

Aucun commentaire:

Enregistrer un commentaire