Files
2026-12-01 15:23:42 +08:00

239 lines
9.6 KiB
JavaScript

(() => {
"use strict";
/* PHOTOS:START */
const photos = [
{ src: "assets/img/image_1793460848264.jpg" },
{ src: "assets/img/image_1793460860160.jpg" },
{ src: "assets/img/img-1793461002823ced2d91c9da728bb542c2bc058558b4408aa157643495cf5506880ad5df47bb4.jpg" },
{ src: "assets/img/img-179346101086003d303cdf2070b49e746a3dc977b85eddaf5cc9c263ec36782c8159e42225393.jpg" },
{ src: "assets/img/img-1793461014140ea2625d5252d11546fac841dfee9d62866f39afd60b4a1a0d89dc51cfae39b39.jpg" },
{ src: "assets/img/img-1793461022627f07484dd63b425f2bb137de3762d9d55d01133e3056d43b7a1d0e7b2d05295e8.jpg" },
{ src: "assets/img/img-1793461026364d156fea2ecf33d42e1f26ce6f36efdf1ba14f6ea64e86fc314ff3989fc3911ac.jpg" }
];
/* PHOTOS:END */
const gallery = document.getElementById("gallery");
if (!gallery || !photos.length) return;
const nameOf = (src) => src.split("/").pop() || src;
let activeIndex = -1;
let cardWidth = 168;
let cardGap = 20;
/* 列宽上限随照片数量与间距动态计算,避免固定 6 列假设 */
const fitCard = () => `min(var(--card-width), calc((100vw - ${photos.length - 1} * var(--gap)) / ${photos.length}))`;
function renderColumns() {
const base = fitCard();
gallery.style.gridTemplateColumns = photos
.map((_, i) => (i === activeIndex
? `min(calc(${base} * 2.4), 52vw)`
: base))
.join(" ");
}
const boxes = photos.map((photo, index) => {
const box = document.createElement("div");
box.className = "box";
box.style.setProperty("--img", `url("${photo.src}")`);
box.style.setProperty("--delay", `${Math.min(index, 12) * 60}ms`);
box.style.setProperty("--stagger", "0px");
box.tabIndex = 0;
box.setAttribute("role", "img");
box.setAttribute("aria-label", nameOf(photo.src));
const activate = () => {
if (activeIndex === index) return;
activeIndex = index;
boxes.forEach((item, i) => item.classList.toggle("is-active", i === index));
renderColumns();
};
box.addEventListener("mouseenter", activate);
box.addEventListener("focus", activate);
box.addEventListener("click", activate);
gallery.append(box);
return box;
});
/* ---------- Settings panel ---------- */
const toggle = document.getElementById("settings-toggle");
const panel = document.getElementById("settings-panel");
const close = document.getElementById("settings-close");
const card = document.getElementById("range-card");
const height = document.getElementById("range-height");
const gap = document.getElementById("range-gap");
const duration = document.getElementById("range-duration");
const stagger = document.getElementById("range-stagger");
const cardValue = document.getElementById("value-card");
const heightValue = document.getElementById("value-height");
const gapValue = document.getElementById("value-gap");
const durationValue = document.getElementById("value-duration");
const staggerValue = document.getElementById("value-stagger");
const swatches = document.getElementById("swatches");
const shapePicker = document.getElementById("shape-picker");
function setPanel(open) {
panel?.classList.toggle("is-open", open);
panel?.setAttribute("aria-hidden", String(!open));
toggle?.setAttribute("aria-expanded", String(open));
}
toggle?.addEventListener("click", () => setPanel(!panel?.classList.contains("is-open")));
close?.addEventListener("click", () => setPanel(false));
document.addEventListener("keydown", (event) => {
if (event.key === "Escape" && panel?.classList.contains("is-open")) setPanel(false);
});
const THEMES = {
default: { a: "#08060f", b: "#6366f1", c: "#d946ef" },
violet: { a: "#8357eb", b: "#6a3de0", c: "#a86ff5" },
space: { a: "#0d0b1e", b: "#1b1440", c: "#2a1d52" },
teal: { a: "#062a30", b: "#0a3d40", c: "#0e5560" },
sand: { a: "#f3e2c7", b: "#f7d9a8", c: "#e8b98a" },
black: { a: "#050507", b: "#101018", c: "#16161f" }
};
const THEME_LABELS = {
default: "默认", violet: "violet", space: "space",
teal: "teal", sand: "sand", black: "black"
};
const hexToRgb = (hex) => {
const raw = hex.replace("#", "");
const full = raw.length === 3 ? raw.split("").map((ch) => ch + ch).join("") : raw;
return `${parseInt(full.slice(0, 2), 16)}, ${parseInt(full.slice(2, 4), 16)}, ${parseInt(full.slice(4, 6), 16)}`;
};
function applyTheme(name) {
const theme = THEMES[name] || THEMES.default;
const root = document.documentElement.style;
root.setProperty("--bg", theme.a);
root.setProperty("--glow-a", hexToRgb(theme.b));
root.setProperty("--glow-b", hexToRgb(theme.c));
if (swatches) {
swatches.querySelectorAll(".swatch").forEach((item) => {
const active = item.dataset.theme === name;
item.classList.toggle("is-active", active);
item.setAttribute("aria-pressed", String(active));
});
}
}
if (swatches) {
Object.entries(THEMES).forEach(([name, theme]) => {
const button = document.createElement("button");
button.type = "button";
button.className = "swatch";
button.dataset.theme = name;
button.setAttribute("aria-label", `背景:${THEME_LABELS[name]}`);
button.setAttribute("aria-pressed", "false");
const dot = document.createElement("span");
dot.className = "swatch-dot";
dot.style.background = `linear-gradient(135deg, ${theme.a} 0 33%, ${theme.b} 33% 66%, ${theme.c} 66% 100%)`;
const label = document.createElement("span");
label.className = "swatch-label";
label.textContent = THEME_LABELS[name];
button.append(dot, label);
button.addEventListener("click", () => applyTheme(name));
swatches.append(button);
});
}
const SHAPES = [
{
name: "parallelogram",
label: "平行四边形",
path: "M5 4h15l-5 16H0z"
},
{
name: "quad",
label: "四边形",
path: "M2 3h16v18H2z"
},
{
name: "hexagon",
label: "六边形",
path: "M9 1h4l5 6v10l-5 6H4l-5-6V7z"
}
];
function applyShape(name) {
gallery.dataset.shape = name;
if (!shapePicker) return;
shapePicker.querySelectorAll(".shape-chip").forEach((item) => {
const active = item.dataset.shape === name;
item.classList.toggle("is-active", active);
item.setAttribute("aria-pressed", String(active));
});
}
if (shapePicker) {
SHAPES.forEach((shape) => {
const button = document.createElement("button");
button.type = "button";
button.className = "shape-chip";
button.dataset.shape = shape.name;
button.setAttribute("aria-label", `卡片形状:${shape.label}`);
button.setAttribute("aria-pressed", "false");
const icon = document.createElementNS("http://www.w3.org/2000/svg", "svg");
icon.setAttribute("viewBox", "0 0 20 24");
icon.setAttribute("aria-hidden", "true");
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", shape.path);
icon.append(path);
const label = document.createElement("span");
label.textContent = shape.label;
button.append(icon, label);
button.addEventListener("click", () => applyShape(shape.name));
shapePicker.append(button);
});
}
function applyCard() {
cardWidth = Number(card?.value ?? 200);
gallery.style.setProperty("--card-width", `${cardWidth}px`);
renderColumns();
if (cardValue) cardValue.textContent = `${cardWidth}px`;
}
function applyHeight() {
const value = Number(height?.value ?? 660);
gallery.style.setProperty("--card-height", `${value}px`);
if (heightValue) heightValue.textContent = `${value}px`;
}
function applyGap() {
cardGap = Number(gap?.value ?? 20);
gallery.style.setProperty("--gap", `${cardGap}px`);
renderColumns();
if (gapValue) gapValue.textContent = `${cardGap}px`;
}
function applyDuration() {
const value = Number(duration?.value ?? 960);
document.documentElement.style.setProperty("--duration", `${value}ms`);
if (durationValue) durationValue.textContent = `${value}ms`;
}
function applyStagger() {
const value = Number(stagger?.value ?? 0);
boxes.forEach((box, index) => {
const up = index % 2 === 0;
box.style.setProperty("--stagger", `${(up ? -1 : 1) * value}px`);
/* 向上偏移的卡片向左倾,向下偏移的卡片保持向右倾 */
box.classList.toggle("tilt-left", up);
});
if (staggerValue) staggerValue.textContent = `${value}px`;
}
card?.addEventListener("input", applyCard);
height?.addEventListener("input", applyHeight);
gap?.addEventListener("input", applyGap);
duration?.addEventListener("input", applyDuration);
stagger?.addEventListener("input", applyStagger);
applyCard();
applyHeight();
applyGap();
applyDuration();
applyStagger();
applyShape("hexagon");
applyTheme("default");
})();