準備一組連續拍攝的產品圖片(如 36 張,每 10 度一張),根據觸控/滑鼠水平位移切換顯示對應幀,形成 360° 旋轉視覺效果。
<div class="viewer360" id="viewer">
<div class="loader">載入中...</div>
<img class="frame" src="frames/01.jpg" alt="產品視角 1">
</div>
<style>
.viewer360 { position: relative; width: 100%; max-width: 600px; aspect-ratio: 4/3; overflow: hidden; background: #f0f0f0; }
.viewer360 .frame { width: 100%; height: 100%; object-fit: contain; display: none; }
.viewer360 .frame.active { display: block; }
.viewer360 .loader { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,0.5); color: white; }
</style>
class Viewer360 {
constructor(containerSelector, frameCount = 36, framePath = "frames/{index}.jpg") {
this.container = document.querySelector(containerSelector);
this.frameCount = frameCount;
this.framePath = framePath;
this.currentFrame = 1;
this.startX = 0;
this.isDragging = false;
this.frames = [];
this.init();
}
async init() {
await this.preloadFrames();
this.bindEvents();
this.showFrame(1);
this.container.querySelector(".loader").remove();
}
async preloadFrames() {
const promises = [];
for (let i = 1; i <= this.frameCount; i++) {
const img = new Image();
img.src = this.framePath.replace("{index}", String(i).padStart(2, "0"));
this.frames[i] = img;
promises.push(new Promise(res => { img.onload = img.onerror = res; }));
}
await Promise.all(promises);
// 建立 img 元素並隱藏
this.frames.forEach((img, i) => {
if (!img) return;
const el = document.createElement("img");
el.src = img.src;
el.className = "frame";
el.dataset.index = i;
this.container.appendChild(el);
});
}
bindEvents() {
// Pointer Events 統一處理滑鼠/觸控/筆
this.container.addEventListener("pointerdown", e => this.onStart(e));
window.addEventListener("pointermove", e => this.onMove(e));
window.addEventListener("pointerup", e => this.onEnd(e));
window.addEventListener("pointerleave", e => this.onEnd(e));
}
onStart(e) {
if (e.target.closest("a, button")) return; // 忽略互動元素
this.isDragging = true;
this.startX = e.clientX;
this.container.setPointerCapture(e.pointerId);
}
onMove(e) {
if (!this.isDragging) return;
const deltaX = e.clientX - this.startX;
// 每移動 10px 切一幀(可調整靈敏度)
const frameDelta = Math.round(deltaX / 10);
if (frameDelta !== 0) {
this.currentFrame = this.wrapFrame(this.currentFrame + frameDelta);
this.showFrame(this.currentFrame);
this.startX = e.clientX; // 重置基準,避免累積誤差
}
}
onEnd() {
this.isDragging = false;
}
wrapFrame(index) {
if (index < 1) return this.frameCount;
if (index > this.frameCount) return 1;
return index;
}
showFrame(index) {
this.container.querySelectorAll(".frame").forEach(el => {
el.classList.toggle("active", parseInt(el.dataset.index) === index);
});
}
}
// 初始化
new Viewer360("#viewer", 36, "assets/product/frame_{index}.jpg");
class AutoRotateViewer360 extends Viewer360 {
constructor(...args) {
super(...args);
this.autoTimer = null;
this.container.addEventListener("mouseenter", () => this.stopAuto());
this.container.addEventListener("mouseleave", () => this.startAuto());
this.startAuto();
}
startAuto() {
this.stopAuto();
this.autoTimer = setInterval(() => {
this.currentFrame = this.wrapFrame(this.currentFrame + 1);
this.showFrame(this.currentFrame);
}, 100); // 10 幀/秒
}
stopAuto() { clearInterval(this.autoTimer); }
}
transform: translateZ(0) 或 will-change: transform。