Now.js Framework Documentation

Now.js Framework Documentation

MediaViewer

TH 13 Nov 2026 06:18

MediaViewer

ภาพรวม

MediaViewer คือ component สำหรับแสดง media (images, videos, YouTube) แบบ slideshow ใน Now.js Framework รองรับ zoom, fullscreen และ touch gestures

ใช้เมื่อ:

  • ต้องการ image gallery/lightbox
  • ต้องการ video player
  • ต้องการ slideshow
  • ต้องการ media preview

ทำไมต้องใช้:

  • ✅ Multiple media types (image, video, YouTube)
  • ✅ Slideshow with thumbnails
  • ✅ Zoom และ pan
  • ✅ Touch gestures (swipe)
  • ✅ Keyboard controls
  • ✅ Fullscreen support
  • ✅ Accessibility (ARIA)

การใช้งานพื้นฐาน

HTML Declarative

<div data-component="media-viewer">
  <img src="/images/photo1.jpg" alt="Photo 1">
  <img src="/images/photo2.jpg" alt="Photo 2">
  <img src="/images/photo3.jpg" alt="Photo 3">
</div>

With Mixed Media

<div data-component="media-viewer">
  <img src="/images/photo.jpg" alt="Photo">
  <video src="/videos/clip.mp4"></video>
  <div data-youtube="dQw4w9WgXcQ"></div>
</div>

JavaScript API

const viewer = new MediaViewer({
  showThumbnails: true,
  loop: true
});

viewer.show([
  { type: 'image', url: '/photo1.jpg', caption: 'Photo 1' },
  { type: 'image', url: '/photo2.jpg', caption: 'Photo 2' },
  { type: 'video', url: '/video.mp4', caption: 'Video' },
  { type: 'youtube', videoId: 'dQw4w9WgXcQ', caption: 'YouTube' }
]);

Media Item Types

Type Properties
image url, caption, thumbnail
video url, caption, poster
youtube videoId หรือ url ของ youtube.com / youtu.be, caption
iframe url, caption
text content, title, caption
html content, caption

รายการที่ไม่ระบุ type เป็น image ยกเว้น url ของ youtube.com หรือ youtu.be ซึ่งเป็น youtube ·
รายการจาก data-api ใช้ src หรือ path แทน url และ thumb แทน thumbnail ได้

URL ของรายการ

รายการมาจาก API จาก data-src/href ในหน้า หรือจากโค้ด จึงถูกตรวจทุกครั้งที่ show():

  • url, thumbnail, poster ผ่าน SecurityManager.sanitizeUrl — path และ scheme ใน config.html.urlSchemes
    (http: https: mailto: tel:) ผ่าน · รูป รูปย่อ และ poster รับ data:image/... ได้ · javascript: และ data: อื่นถูกปฏิเสธ
  • iframe เปิดเฉพาะหน้า http(s) (path คิดจากหน้านี้) หน้าของเว็บนี้เปิดตรง ๆ หน้าจากเว็บอื่นเปิดใน sandbox
    ค่าเดียวกับ SecurityManager.config.html.iframeSandbox · ตั้ง iframeHosts (หรือ data-iframe-hosts) เพื่อจำกัดเว็บอื่น
  • รายการที่ url ถูกปฏิเสธแสดงข้อความผิดพลาดแทนเนื้อหา (text และ html แค่ไม่มี url) · thumbnail/poster ที่ถูกปฏิเสธถูกตัดทิ้ง
  • ข้อความผิดพลาด รวมถึงข้อผิดพลาดของการโหลด data-api แสดงเป็นข้อความ ไม่ตีความเป็น HTML
  • วิดีโอ YouTube ใช้ type: 'youtube' หรือ url ของ youtube.com — เล่นผ่าน www.youtube.com/embed/ ด้วย video ID
    ที่ตรวจแล้ว ไม่ต้องตั้ง iframeHosts
// iframe จากเว็บอื่นเปิดได้เฉพาะ Vimeo และ /embed/ ของ example.com (หน้าของเว็บนี้เปิดได้เสมอ)
new MediaViewer({iframeHosts: ['player.vimeo.com/video/', 'www.example.com/embed/']});

Data Attributes

บน container ที่โหลดรายการจาก API

Attribute คำอธิบาย
data-api endpoint ที่โหลดรายการสื่อ
data-api-method HTTP method ของคำขอ (GET เป็นค่าเริ่มต้น)

บนอีลิเมนต์ที่เปิด viewer

Attribute คำอธิบาย
data-src URL ของสื่อ ใช้แทน src ของรูปเมื่อต้องการไฟล์ต้นฉบับคนละไฟล์กับรูปย่อ
data-url URL ของสื่อ ใช้เมื่อไม่ได้ตั้ง data-src
data-thumbnail URL ของรูปย่อ ถ้าไม่ระบุจะใช้ src ของรูปข้างใน
data-caption คำบรรยายใต้สื่อ ถ้าไม่ระบุจะใช้ alt
data-type ชนิดสื่อ เช่น image, video, youtube (image เป็นค่าเริ่มต้น)
data-interval ระยะเวลาเล่นสไลด์อัตโนมัติ หน่วยมิลลิวินาที
data-max-zoom ระดับซูมสูงสุด
data-zoom-step ระยะที่ซูมเพิ่มต่อหนึ่งครั้ง
data-iframe-hosts host + path ที่ iframe จากเว็บอื่นต้องขึ้นต้นด้วย คั่นด้วยจุลภาค (ดู iframeHosts)

Options

new MediaViewer({
  showThumbnails: false,   // แถบรูปย่อ
  showControls: true,      // ปุ่มซูม
  autoplay: false,         // เล่นสไลด์อัตโนมัติ
  interval: 5000,          // ระยะเวลาต่อรายการ (มิลลิวินาที)
  loop: true,              // ต่อจากรายการสุดท้ายด้วยรายการแรก
  enableZoom: true,
  maxZoom: 3,
  zoomStep: 0.5,

  // host + path ที่ iframe จากเว็บอื่นต้องขึ้นต้นด้วย (รูปแบบเดียวกับ
  // SecurityManager.config.html.iframeHosts) · null = หน้า http(s) ใดก็ได้
  // หน้าของเว็บนี้เปิดได้เสมอ หน้าจากเว็บอื่นอยู่ใน sandbox เสมอ
  iframeHosts: null,

  // Callbacks
  onShow: (item) => {},
  onHide: () => {},
  onChange: (item, index) => {}
});

API อ้างอิง

new MediaViewer(options)

สร้าง MediaViewer instance

Returns: MediaViewer

viewer.show(items, startIndex?)

แสดง viewer

Parameter Type Description
items array Media items (required)
startIndex number Starting index (default: 0)
viewer.show(items);
viewer.show(items, 2);  // Start at index 2

viewer.hide()

ซ่อน viewer

viewer.next()

ไปรูปถัดไป

viewer.prev()

ไปรูปก่อนหน้า

viewer.goTo(index)

ไปยัง index ที่กำหนด

viewer.play()

เริ่ม slideshow

viewer.stop()

หยุด slideshow

Keyboard Shortcuts

Key Action
→ Next item
← Previous item
Space Play/Pause slideshow
Escape Close viewer
+ / = Zoom in
- Zoom out
0 Reset zoom
F Toggle fullscreen

Touch Gestures

Gesture Action
Swipe Left Next item
Swipe Right Previous item
Pinch Zoom
Double-tap Toggle zoom
Swipe Down Close viewer

การจัดรูปแบบ CSS

/* Viewer container */
.media-viewer {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.95);
  z-index: 9999;
  display: flex;
  flex-direction: column;
}

.media-viewer-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* Main media */
.media-viewer-main img,
.media-viewer-main video {
  max-width: 90%;
  max-height: 80vh;
  object-fit: contain;
}

/* Navigation */
.media-viewer-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255,255,255,0.1);
  color: white;
  border: none;
  padding: 16px;
  cursor: pointer;
}

.media-viewer-prev { left: 16px; }
.media-viewer-next { right: 16px; }

/* Thumbnails */
.media-viewer-thumbnails {
  display: flex;
  gap: 8px;
  padding: 16px;
  overflow-x: auto;
  justify-content: center;
}

.media-viewer-thumb {
  width: 60px;
  height: 60px;
  object-fit: cover;
  opacity: 0.5;
  cursor: pointer;
}

.media-viewer-thumb.active {
  opacity: 1;
  outline: 2px solid white;
}

/* Caption */
.media-viewer-caption {
  text-align: center;
  color: white;
  padding: 16px;
}

/* Close button */
.media-viewer-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
}

ตัวอย่างการใช้งานจริง

<div class="product-gallery">
  <div class="main-image" id="main-image">
    <img src="/products/shoe-1.jpg" alt="Main">
  </div>
  <div class="thumbnails" data-component="media-viewer">
    <img src="/products/shoe-1.jpg" alt="View 1">
    <img src="/products/shoe-2.jpg" alt="View 2">
    <img src="/products/shoe-3.jpg" alt="View 3">
    <video src="/products/shoe-video.mp4" poster="/products/shoe-poster.jpg"></video>
  </div>
</div>

Photo Album

// Open viewer from grid
document.querySelectorAll('.photo-grid img').forEach((img, index) => {
  img.addEventListener('click', () => {
    const items = Array.from(document.querySelectorAll('.photo-grid img')).map(img => ({
      type: 'image',
      url: img.src.replace('thumb', 'full'),
      caption: img.alt
    }));

    const viewer = new MediaViewer();
    viewer.show(items, index);
  });
});
const videos = [
  { type: 'youtube', videoId: 'video1', caption: 'Tutorial Part 1' },
  { type: 'youtube', videoId: 'video2', caption: 'Tutorial Part 2' },
  { type: 'video', url: '/local-video.mp4', caption: 'Demo' }
];

const viewer = new MediaViewer({
  showThumbnails: true
});

document.getElementById('watch-btn').onclick = () => viewer.show(videos);

เหตุการณ์

เหตุการณ์เหล่านี้เป็น DOM CustomEvent ที่ dispatch บนอีลิเมนต์ จึงต้องรับด้วย
element.addEventListener() และอ่านค่าจาก event.detail

Event เมื่อเกิด event.detail
mediaviewer:loaded โหลดรายการสื่อจาก API สำเร็จ {items, viewer}
mediaviewer:error โหลดรายการสื่อไม่สำเร็จ {error, viewer}

ข้อควรระวัง

⚠️ 1. YouTube Video ID

// ❌ Full URL
{ type: 'youtube', videoId: 'https://youtube.com/watch?v=abc123' }

// ✅ Video ID เท่านั้น
{ type: 'youtube', videoId: 'abc123' }

⚠️ 2. Image Paths

// ❌ Relative to current page
{ type: 'image', url: 'photo.jpg' }

// ✅ Absolute หรือ from root
{ type: 'image', url: '/images/photo.jpg' }

เอกสารที่เกี่ยวข้อง