Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

رسالة Toast

جزء من قسم JavaScript in Action في رحلة HTML على Coddy. الدرس 8 من 27.

رسالة toast هي إشعار صغير يظهر لفترة وجيزة على الشاشة لتقديم ملاحظات للمستخدم. وعادةً ما يتلاشى بعد بضع ثوانٍ.

أنشئ بنية HTML أساسية:

<div class="container">
    <button id="showToast">Show Toast</button>
    <div id="toast" class="hidden">This is a toast message!</div>
</div>

أضف بعض CSS لتنسيق الإشعار المنبثق. نستخدم فئتين: .hidden لإبقائه غير مرئي، و.show لجعله يظهر.

.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    background-color: #333;
    color: white;
    padding: 15px 25px;
}
.show {
    opacity: 1;
}
.hidden {
    display: none;
}

أضف الآن JavaScript لإظهار وإخفاء الإشعار المنبثق:

const showToastBtn = document.getElementById('showToast');
const toast = document.getElementById('toast');

showToastBtn.addEventListener('click', function() {
    toast.classList.remove('hidden');
    toast.classList.add('show');

    setTimeout(function() {
        toast.classList.remove('show');
        toast.classList.add('hidden');
    }, 3000);
});

يجعل هذا الكود toast مرئيًا عند click على button، ويخفيه تلقائيًا بعد 3 seconds.

challenge icon

التحدي

سهل

هدفك هو جعل toast يبدو أنيقًا وجاهزًا للظهور والاختفاء.

  1. ضع رسالة toast في center at the bottom من الصفحة باستخدام position: fixed مع bottom وleft.
  2. امنحها background color وغيّر text color لجعلها قابلة للقراءة.
  3. أضف بعض padding حتى لا تبدو الرسالة مكتظة.
  4. اجعل الزوايا تبدو ناعمة.
  5. اضبط opacity to 0 بحيث تكون مخفية افتراضيًا (قبل أن تعرضها JavaScript).

جرّب بنفسك

<!DOCTYPE html>
<html>
<head>
  <title>Toast Notification Example</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <header>
    <h1>My Profile Settings</h1>
  </header>

  <main>
    <p>Update your profile and save changes. A toast message will confirm your action.</p>
    <button id="showToast">Save Changes</button>
  </main>

  <!-- توست -->
  <div id="toast" class="toast hidden">✅Your changes have been saved!</div>

  <script src="script.js"></script>
</body>
</html>
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس JavaScript in Action

تدرّب بنفسك: Playground لـ Web