現在の答えはどれもうまくいきませんでした。Bootstrap 3 を使用しています。
ロブ フェルメールのやり方が好きで、彼の反応から始めました。
フェードインしてからフェードアウトする効果のために、次の関数を書き、jQuery を使用しました。
アラートを追加するページの HTML:
<div class="alert-messages text-center">
</div>
アラートを表示および非表示にする Javascript 関数。
function showAndDismissAlert(type, message) {
var htmlAlert = '<div class="alert alert-' + type + '">' + message + '</div>';
// Prepend so that alert is on top, could also append if we want new alerts to show below instead of on top.
$(".alert-messages").prepend(htmlAlert);
// Since we are prepending, take the first alert and tell it to fade in and then fade out.
// Note: if we were appending, then should use last() instead of first()
$(".alert-messages .alert").first().hide().fadeIn(200).delay(2000).fadeOut(1000, function () { $(this).remove(); });
}
次に、アラートを表示および非表示にするには、次のように関数を呼び出します。
showAndDismissAlert('success', 'Saved Successfully!');
showAndDismissAlert('danger', 'Error Encountered');
showAndDismissAlert('info', 'Message Received');
補足として、上に固定された div.alert-messages のスタイルを設定しました。
<style>
div.alert-messages {
position: fixed;
top: 50px;
left: 25%;
right: 25%;
z-index: 7000;
}
</style>