چگونه ایجاد شود: پیام‌های هشدار

یاد بگیرید که چگونه با استفاده از CSS پیام‌های هشدار ایجاد کنید.

هشدار

پیام‌های هشدار می‌توانند برای اطلاع‌رسانی به کاربر در مورد موضوعات خاص استفاده شوند: خطر، موفق، اطلاعات یا هشدار.

× خطر!نشان‌دهنده عملیات خطرناک یا ممکن است منجر به تأثیرات منفی شود.
× موفق!نشان‌دهنده عملیات موفق یا مثبت است.
× اطلاعات!نشان‌دهنده تغییرات اطلاعاتی خنثی یا عملیات است.
× هشدار!نشان‌دهنده هشداری است که ممکن است نیاز به توجه داشته باشید.

به طور مستقیم امتحان کنید

پیام هشدار ایجاد کنید

مرحله اول - اضافه کردن HTML:

<div class="alert">
  <span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
  این یک جعبه هشدار است.
</div>

اگر می‌خواهید بتوانید پیام هشدار را ببندید، یک علامت نگارشی با onclick ویژگی‌ها <span> عنصر، این ویژگی نشان‌دهنده این است که "وقتی روی من کلیک می‌کنید، عنصر والد من را پنهان کنید" - به عبارت دیگر، محفظه <div class="alert">.

توجه:از نمادهای HTML "×" برای ایجاد حرف "x" استفاده کنید.

مرحله دوم - اضافه کردن CSS:

Set the style of the alert box and close button:

/* Alert message box */
.alert {
  padding: 20px;
  background-color: #f44336; /* Red */
  color: white;
  margin-bottom: 15px;
}
/* Close button */
.closebtn {
  margin-left: 15px;
  color: white;
  font-weight: bold;
  float: right;
  font-size: 22px;
  line-height: 20px;
  cursor: pointer;
  transition: 0.3s;
}
/* When the mouse moves over the close button */
.closebtn:hover {
  color: black;
}

به طور مستقیم امتحان کنید

multiple alerts

If there are multiple alert messages on the page, you can add the following script to <span> of the onclick properties to close different alerts.

And if you want to fade out slowly when clicking on the alerts, add opacity and transition to alert In class:

Example

<style>
.alert {
  opacity: 1;
  transition: opacity 0.6s; /* Fade out in 600 milliseconds */
}
</style>
<script>
// Get all elements with class="closebtn"
var close = document.getElementsByClassName("closebtn");
var i;
// Loop through all close buttons
for (i = 0; i < close.length; i++) {
  // Traverse all close buttons
  close[i].onclick = function(){
    // Get the parent element of <span class="closebtn"> (<div class="alert">)
    var div = this.parentElement;
    // Set the opacity of div to 0 (transparent)
    div.style.opacity = "0";
    // 600 milliseconds after, hide div (same as fade out time)
    setTimeout(function(){ div.style.display = "none"; }, 600);
  }
}
</script>

به طور مستقیم امتحان کنید

صفحات مرتبط

آموزش:چگونه نوت‌برد ایجاد می‌کنیم