← المستوى 2 – الساحر

الدرس 5 من 8 ⏱ 35 دقيقة

معرض صور مع عارض

النقر على صورة يكبّرها لتملأ الشاشة. ستتعلّم العمل مع مجموعة عناصر.

كيف سيعمل

سنُرقّي المعرض من المستوى 1: النقر على صورة صغيرة يفتح نسخة كبيرة فوق الصفحة كلها بخلفية داكنة (يسمى lightbox). النقر مرة أخرى يغلقه.

شاهد معي: lightbox

HTML – أضف “نافذة تكبير” تحت المعرض (مخفية في البداية):

<div class="gallery">
  <img src="images/p1.jpg" alt="First photo">
  <img src="images/p2.jpg" alt="Second photo">
  <img src="images/p3.jpg" alt="Third photo">
</div>

<div id="lightbox" class="hidden">
  <img id="lightbox-photo" src="" alt="">
</div>

CSS – نافذة تغطي الشاشة كلها:

#lightbox {
  position: fixed;          /* stuck over the whole window */
  inset: 0;                 /* 0 from every edge = the whole area */
  background-color: rgba(0, 0, 0, 0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

#lightbox img {
  max-width: 90%;
  max-height: 90%;
}

.hidden {
  display: none !important; /* hide completely */
}

JavaScript – وشيء جديد: نعمل مع كل الصور دفعة واحدة:

const photos = document.querySelectorAll(".gallery img");
const lightbox = document.getElementById("lightbox");
const lightboxPhoto = document.getElementById("lightbox-photo");

// For every photo in the gallery: show it big on click
photos.forEach((photo) => {
  photo.addEventListener("click", () => {
    lightboxPhoto.src = photo.src;
    lightboxPhoto.alt = photo.alt;
    lightbox.classList.remove("hidden");
  });
});

// Clicking anywhere in the lightbox closes it
lightbox.addEventListener("click", () => {
  lightbox.classList.add("hidden");
});

جديد:

دورك الآن 💪

  1. أضف lightbox إلى موقعك حسب المثال. لديك معرض من المستوى 1 – تأكد أن selector (.gallery img) يطابق classes.
  2. ترقية للشجعان – التنقل بالأسهم:
إضافة: التنقل للصورة التالية/السابقة

امتداد للسكربت – نتذكّر أي صورة مفتوحة:

const photos = document.querySelectorAll(".gallery img");
const lightbox = document.getElementById("lightbox");
const lightboxPhoto = document.getElementById("lightbox-photo");
let current = 0;

function showPhoto(number) {
  // Handle wraparound: after the last photo comes the first again
  if (number < 0) number = photos.length - 1;
  if (number >= photos.length) number = 0;

  current = number;
  lightboxPhoto.src = photos[current].src;
  lightboxPhoto.alt = photos[current].alt;
}

photos.forEach((photo, index) => {
  photo.addEventListener("click", () => {
    showPhoto(index);
    lightbox.classList.remove("hidden");
  });
});

lightbox.addEventListener("click", () => {
  lightbox.classList.add("hidden");
});

// Navigate with the keyboard
document.addEventListener("keydown", (event) => {
  if (lightbox.classList.contains("hidden")) return;

  if (event.key === "ArrowRight") showPhoto(current + 1);
  if (event.key === "ArrowLeft") showPhoto(current - 1);
  if (event.key === "Escape") lightbox.classList.add("hidden");
});

شيئان جديدان: function showPhoto(number) {...} function مسماة – حزمة كود يمكن تشغيلها من أي مكان بالاسم. وphotos[current] يسحب عنصراً من قائمة بموضعه (العد يبدأ من صفر!).

أفكار للمشروع: 🏫 صور من فعاليات المدرسة · 🙋 إبداعاتك ومغامراتك · 🌍 صور من الرحلات (هنا lightbox يتألق!) · ⚽ لحظات من المباريات.

تحقق من نفسك: تنقر صورة → تكبر على خلفية داكنة. تنقر مرة أخرى → تغلق. مع الإضافة، يمكنك التنقل بالأسهم والإغلاق بـ Escape. هكذا تعمل المعارض على المواقع المحترفة – والآن تعرف ما بداخلها.

ما ستخرج به من هذا الدرس