From c15181ea2bdeb4b080762d08260d927ad1fff9f5 Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Fri, 14 Feb 2025 10:40:44 -0600 Subject: Valentine's Day 2025 --- scripts/valentine.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 scripts/valentine.js (limited to 'scripts') diff --git a/scripts/valentine.js b/scripts/valentine.js new file mode 100644 index 0000000..6347b87 --- /dev/null +++ b/scripts/valentine.js @@ -0,0 +1,37 @@ +const btn = document.getElementById("moveBtn"); + +btn.addEventListener("mouseover", () => { + let randX = (Math.random() * 600) - 300; // Move randomly left/right up to 200px + let randY = (Math.random() * 600) - 300; // Move randomly up/down up to 200px + + // Get viewport dimensions + const viewportWidth = window.innerWidth; + const viewportHeight = window.innerHeight; + + // Get button dimensions + const btnRect = btn.getBoundingClientRect(); + + // Ensure button stays within viewport + randX = Math.min(Math.max(randX, -btnRect.left), viewportWidth - btnRect.right); + randY = Math.min(Math.max(randY, -btnRect.top), viewportHeight - btnRect.bottom); + + btn.style.transform = `translate(${randX}px, ${randY}px)`; +}); + +btn.addEventListener("click", () => { + let randX = (Math.random() * 600) - 300; // Move randomly left/right up to 200px + let randY = (Math.random() * 600) - 300; // Move randomly up/down up to 200px + + // Get viewport dimensions + const viewportWidth = window.innerWidth; + const viewportHeight = window.innerHeight; + + // Get button dimensions + const btnRect = btn.getBoundingClientRect(); + + // Ensure button stays within viewport + randX = Math.min(Math.max(randX, -btnRect.left), viewportWidth - btnRect.right); + randY = Math.min(Math.max(randY, -btnRect.top), viewportHeight - btnRect.bottom); + + btn.style.transform = `translate(${randX}px, ${randY}px)`; +}); \ No newline at end of file -- cgit v1.2.3