From 8557f12fdff3a4ac026169cc3482a7be8743b68c Mon Sep 17 00:00:00 2001 From: Spedon Date: Thu, 9 Mar 2023 17:07:43 +0800 Subject: [PATCH] js setup --- assets/js/theme.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 assets/js/theme.js diff --git a/assets/js/theme.js b/assets/js/theme.js new file mode 100644 index 0000000..1fb7457 --- /dev/null +++ b/assets/js/theme.js @@ -0,0 +1,35 @@ +const images = document.getElementsByClassName("image"); + +let globalIndex = 0, + last = {x: 0, y: 0}; + +const activate = (image, x, y) => { + image.style.left = `${x}px`; + image.style.top = `${y}px`; + image.style.zIndex = globalIndex; + + image.dataset.status = "active"; + + last = {x, y}; +} + +const distanceFromLast = (x, y) => { + return Math.hypot(x - last.x, y - last.y); +} + +const handleOnMove = e => { + if (distanceFromLast(e.clientX, e.clientY) > (window.innerWidth / 20)) { + const lead = images[globalIndex % images.length], + tail = images[(globalIndex - 5) % images.length]; + + activate(lead, e.clientX, e.clientY); + + if (tail) tail.dataset.status = "inactive"; + + globalIndex++; + } +} + +window.onmousemove = e => handleOnMove(e); + +window.ontouchmove = e => handleOnMove(e.touches[0]); \ No newline at end of file