- hero: rotating use-case line, count-up stats, GPU spec marquee - new #why section: why dorm residents should own a PC (cloud/account risk, perpetual license collapse, subscription rental) with cited sources (NYT/The Verge 2022, Microsoft Q&A/Lifecycle) and a rent-vs-own calculator - motion: 380ms reveals, hover lift, reading progress bar, reduced-motion paths - nav drawer breakpoint 960px, JS-off fallback, section renumbering
311 lines
12 KiB
JavaScript
311 lines
12 KiB
JavaScript
/* ==========================================================================
|
|
SFF-01 · Thin Case — interaction
|
|
--------------------------------------------------------------------------
|
|
ตั้งค่าช่องทางรับออเดอร์ที่ ORDER_EMAIL ด้านล่าง
|
|
หน้าเว็บเป็น static ไม่มี backend: ฟอร์มจะสร้างข้อความสรุปแล้วให้ผู้ใช้ส่งเอง
|
|
========================================================================== */
|
|
|
|
(function () {
|
|
"use strict";
|
|
|
|
var ORDER_EMAIL = "[email protected]"; // TODO: เปลี่ยนเป็นอีเมลร้านจริง
|
|
var reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
|
|
/* ------------------------------------------------------ header / nav */
|
|
var header = document.querySelector(".site-header");
|
|
var navLinks = document.getElementById("nav-links");
|
|
var navToggle = document.querySelector(".nav-toggle");
|
|
var progress = document.getElementById("progress");
|
|
|
|
function onScroll() {
|
|
if (header) header.classList.toggle("is-stuck", window.scrollY > 8);
|
|
|
|
if (progress) {
|
|
var root = document.documentElement;
|
|
var max = root.scrollHeight - window.innerHeight;
|
|
var ratio = max > 0 ? Math.min(1, Math.max(0, window.scrollY / max)) : 0;
|
|
progress.style.transform = "scaleX(" + ratio.toFixed(4) + ")";
|
|
}
|
|
}
|
|
window.addEventListener("scroll", onScroll, { passive: true });
|
|
window.addEventListener("resize", onScroll, { passive: true });
|
|
onScroll();
|
|
|
|
if (navToggle && navLinks) {
|
|
navToggle.addEventListener("click", function () {
|
|
var open = navLinks.classList.toggle("is-open");
|
|
navToggle.setAttribute("aria-expanded", open ? "true" : "false");
|
|
});
|
|
navLinks.addEventListener("click", function (e) {
|
|
if (e.target.tagName === "A") {
|
|
navLinks.classList.remove("is-open");
|
|
navToggle.setAttribute("aria-expanded", "false");
|
|
}
|
|
});
|
|
}
|
|
|
|
/* ------------------------------------------------------------- reveal */
|
|
/* ตรวจด้วยตำแหน่งจริงบนจอ — ทนกว่า IntersectionObserver เมื่อมีการเลื่อนแบบทันที */
|
|
var pendingReveal = Array.prototype.slice.call(document.querySelectorAll(".reveal"));
|
|
|
|
function checkReveals() {
|
|
if (!pendingReveal.length) return;
|
|
var vh = window.innerHeight || document.documentElement.clientHeight;
|
|
pendingReveal = pendingReveal.filter(function (el) {
|
|
var r = el.getBoundingClientRect();
|
|
if (r.top < vh * 0.95 && r.bottom > 0) {
|
|
el.classList.add("is-in");
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/* ----------------------------------------------------------- counters */
|
|
function formatNumber(value, dec) {
|
|
if (dec) return value.toFixed(dec);
|
|
return Math.round(value).toLocaleString("th-TH");
|
|
}
|
|
|
|
function countUp(el) {
|
|
var target = parseFloat(el.getAttribute("data-count"));
|
|
var dec = parseInt(el.getAttribute("data-dec") || "0", 10);
|
|
var suffix = el.getAttribute("data-suffix") || "";
|
|
var duration = 900;
|
|
|
|
if (reduceMotion || !isFinite(target)) {
|
|
el.textContent = formatNumber(target, dec) + suffix;
|
|
return;
|
|
}
|
|
|
|
var start = null;
|
|
function step(ts) {
|
|
if (start === null) start = ts;
|
|
var t = Math.min(1, (ts - start) / duration);
|
|
var eased = 1 - Math.pow(1 - t, 3);
|
|
el.textContent = formatNumber(target * eased, dec) + suffix;
|
|
if (t < 1) requestAnimationFrame(step);
|
|
else el.textContent = formatNumber(target, dec) + suffix;
|
|
}
|
|
requestAnimationFrame(step);
|
|
// กันกรณีเฟรมไม่เดิน (แท็บพื้นหลัง/เครื่องช้า) ให้ค่าสุดท้ายลงเสมอ
|
|
setTimeout(function () {
|
|
el.textContent = formatNumber(target, dec) + suffix;
|
|
}, duration + 250);
|
|
}
|
|
|
|
var pendingCount = Array.prototype.slice.call(document.querySelectorAll("[data-count]"));
|
|
|
|
function checkCounters() {
|
|
if (!pendingCount.length) return;
|
|
var vh = window.innerHeight || document.documentElement.clientHeight;
|
|
pendingCount = pendingCount.filter(function (el) {
|
|
var r = el.getBoundingClientRect();
|
|
if (r.top < vh * 0.9 && r.bottom > 0) {
|
|
countUp(el);
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
/* เรียกทั้งสองตัวเมื่อเลื่อน/ย่อขยาย และครั้งแรกหลังโหลด */
|
|
var ticking = false;
|
|
function onFrame() {
|
|
checkReveals();
|
|
checkCounters();
|
|
ticking = false;
|
|
}
|
|
function requestCheck() {
|
|
if (ticking) return;
|
|
ticking = true;
|
|
window.requestAnimationFrame(onFrame);
|
|
}
|
|
window.addEventListener("scroll", requestCheck, { passive: true });
|
|
window.addEventListener("resize", requestCheck, { passive: true });
|
|
window.addEventListener("load", requestCheck);
|
|
requestCheck();
|
|
|
|
/* ------------------------------------------------------------ rotator */
|
|
var rotator = document.querySelector("[data-rotator]");
|
|
if (rotator) {
|
|
var slides = Array.prototype.slice.call(rotator.children);
|
|
if (slides.length > 1 && !reduceMotion) {
|
|
var idx = 0;
|
|
slides[0].classList.add("is-on");
|
|
setInterval(function () {
|
|
var current = slides[idx];
|
|
idx = (idx + 1) % slides.length;
|
|
var next = slides[idx];
|
|
current.classList.remove("is-on");
|
|
current.classList.add("is-out");
|
|
next.classList.remove("is-out");
|
|
next.classList.add("is-on");
|
|
setTimeout(function () {
|
|
current.classList.remove("is-out");
|
|
}, 340);
|
|
}, 2200);
|
|
} else if (slides.length) {
|
|
// ลดการเคลื่อนไหว: แสดงคำแรกคำเดียว ไม่ซ้อนกัน
|
|
slides[0].classList.add("is-on");
|
|
}
|
|
}
|
|
|
|
/* --------------------------------------------------------- calculator */
|
|
var calc = document.querySelector("[data-calc]");
|
|
if (calc) {
|
|
var CASE_PRICE = 3590; // ต้องตรงกับราคาใน index.html
|
|
var PER_YEAR = 1900 * 3; // 3 โปรแกรม × 1,900 บาท/ปี (ตัวเลขตัวอย่าง)
|
|
|
|
var slider = document.getElementById("calc-years");
|
|
var yearsOut = document.getElementById("calc-years-out");
|
|
var sumEl = document.getElementById("calc-sum");
|
|
var subEl = document.getElementById("calc-sub");
|
|
var barRent = document.getElementById("bar-rent");
|
|
var barCase = document.getElementById("bar-case");
|
|
|
|
var renderCalc = function () {
|
|
var years = Number(slider.value) || 1;
|
|
var total = PER_YEAR * years;
|
|
var ratio = total / CASE_PRICE;
|
|
|
|
if (yearsOut) yearsOut.textContent = years + " ปี";
|
|
if (sumEl) sumEl.textContent = total.toLocaleString("th-TH") + " บาท";
|
|
if (subEl) {
|
|
subEl.textContent =
|
|
"ค่าสมัครต่อเนื่อง " + years + " ปี เทียบเท่าเครื่องนี้ " +
|
|
ratio.toFixed(1) + " เครื่อง";
|
|
}
|
|
if (barRent) barRent.style.transform = "scaleX(1)";
|
|
if (barCase) barCase.style.transform = "scaleX(" + Math.max(0.02, 1 / ratio).toFixed(4) + ")";
|
|
};
|
|
|
|
slider.addEventListener("input", renderCalc);
|
|
renderCalc();
|
|
}
|
|
|
|
/* --------------------------------------------------------------- year */
|
|
var yearEl = document.getElementById("year");
|
|
if (yearEl) yearEl.textContent = String(new Date().getFullYear());
|
|
|
|
/* -------------------------------------------------------------- order */
|
|
var form = document.getElementById("order-form");
|
|
var summary = document.getElementById("order-summary");
|
|
var textEl = document.getElementById("order-text");
|
|
var copyBtn = document.getElementById("copy-btn");
|
|
var mailBtn = document.getElementById("mail-btn");
|
|
|
|
function money(n) {
|
|
return n.toLocaleString("th-TH");
|
|
}
|
|
|
|
function selectedPrice() {
|
|
var kit = document.getElementById("f-kit");
|
|
if (!kit) return 3590;
|
|
var opt = kit.options[kit.selectedIndex];
|
|
return Number(opt.getAttribute("data-price") || 3590);
|
|
}
|
|
|
|
function buildOrder() {
|
|
var name = document.getElementById("f-name").value.trim();
|
|
var contact = document.getElementById("f-contact").value.trim();
|
|
var color = document.getElementById("f-color").value;
|
|
var kit = document.getElementById("f-kit").value;
|
|
var qty = Math.max(1, Math.min(10, Number(document.getElementById("f-qty").value) || 1));
|
|
var gpu = document.getElementById("f-gpu").value.trim();
|
|
var note = document.getElementById("f-note").value.trim();
|
|
var unit = selectedPrice();
|
|
|
|
var lines = [
|
|
"สั่งจอง SFF-01 THIN",
|
|
"---------------------------",
|
|
"ชื่อ: " + (name || "-"),
|
|
"ติดต่อ: " + (contact || "-"),
|
|
"สี: " + color,
|
|
"ชุด: " + kit,
|
|
"จำนวน: " + qty,
|
|
"การ์ดจอ: " + (gpu || "-"),
|
|
"หมายเหตุ: " + (note || "-"),
|
|
"---------------------------",
|
|
"ราคาต่อเครื่อง: " + money(unit) + " บาท",
|
|
"รวมทั้งสิ้น: " + money(unit * qty) + " บาท (ยังไม่รวมค่าส่ง)"
|
|
];
|
|
|
|
return { text: lines.join("\n"), name: name, contact: contact, total: unit * qty };
|
|
}
|
|
|
|
function setMailto(order) {
|
|
if (!mailBtn) return;
|
|
var subject = "สั่งจอง SFF-01 THIN — " + (order.name || "ลูกค้าใหม่");
|
|
mailBtn.href =
|
|
"mailto:" + ORDER_EMAIL +
|
|
"?subject=" + encodeURIComponent(subject) +
|
|
"&body=" + encodeURIComponent(order.text);
|
|
}
|
|
|
|
if (form) {
|
|
form.addEventListener("submit", function (e) {
|
|
e.preventDefault();
|
|
var nameEl = document.getElementById("f-name");
|
|
var contactEl = document.getElementById("f-contact");
|
|
var missing = null;
|
|
|
|
if (!nameEl.value.trim()) missing = nameEl;
|
|
else if (!contactEl.value.trim()) missing = contactEl;
|
|
|
|
if (missing) {
|
|
missing.focus();
|
|
missing.style.borderColor = "#d08a63";
|
|
setTimeout(function () {
|
|
missing.style.borderColor = "";
|
|
}, 1600);
|
|
return;
|
|
}
|
|
|
|
var order = buildOrder();
|
|
textEl.textContent = order.text;
|
|
setMailto(order);
|
|
summary.classList.add("is-open");
|
|
summary.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
|
});
|
|
}
|
|
|
|
if (copyBtn) {
|
|
copyBtn.addEventListener("click", function () {
|
|
var text = textEl ? textEl.textContent : "";
|
|
if (!text) return;
|
|
|
|
var done = function () {
|
|
var old = copyBtn.textContent;
|
|
copyBtn.textContent = "คัดลอกแล้ว";
|
|
setTimeout(function () {
|
|
copyBtn.textContent = old;
|
|
}, 1600);
|
|
};
|
|
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
navigator.clipboard.writeText(text).then(done, fallback);
|
|
} else {
|
|
fallback();
|
|
}
|
|
|
|
function fallback() {
|
|
var ta = document.createElement("textarea");
|
|
ta.value = text;
|
|
ta.setAttribute("readonly", "");
|
|
ta.style.position = "fixed";
|
|
ta.style.left = "-9999px";
|
|
document.body.appendChild(ta);
|
|
ta.select();
|
|
try {
|
|
document.execCommand("copy");
|
|
done();
|
|
} catch (err) {
|
|
/* ปล่อยผ่าน — ผู้ใช้เลือกข้อความเองได้ */
|
|
}
|
|
document.body.removeChild(ta);
|
|
}
|
|
});
|
|
}
|
|
})();
|