เรื่องอื่นๆ
ที่คุณอาจชอบ

document.addEventListener("DOMContentLoaded", function () { const goToTop = document.querySelector(".go-to-top"); // ใช้ .go-to-top แทน container const circle = document.querySelector("circle"); let lastScrollY = window.scrollY; // ติดตามการเลื่อนก่อนหน้า if (!goToTop) return; // ถ้าไม่เจอ .go-to-top ให้หยุดทำงาน function updateProgress() { let scrollY = window.scrollY; let maxScroll = document.documentElement.scrollHeight - window.innerHeight; let radius = circle.r.baseVal.value; let circumference = 2 * Math.PI * radius; circle.style.strokeDasharray = circumference; circle.style.strokeDashoffset = circumference - (scrollY / maxScroll) * circumference; // เปลี่ยนตำแหน่ง bottom และ right ตามการเลื่อน if (scrollY > lastScrollY) { // เลื่อนลง goToTop.style.bottom = "10px"; // กลับไปที่ตำแหน่งเดิม goToTop.style.right = "11px"; // ค่าเริ่มต้น } else if (scrollY < lastScrollY) { // เลื่อนขึ้น goToTop.style.bottom = "65px"; // ตั้งค่า bottom เป็น 65px goToTop.style.right = "10px"; // ตั้งค่า right เป็น 10px } lastScrollY = scrollY; // อัปเดต lastScrollY if (scrollY > 10) { goToTop.classList.add("show"); } else { goToTop.classList.remove("show"); } } document.addEventListener("scroll", updateProgress); window.addEventListener("resize", updateProgress); goToTop.addEventListener("click", function () { window.scrollTo({ top: 0, behavior: "smooth" }); }); updateProgress(); });