Last active
March 19, 2025 21:42
-
-
Save liamshort/7a268565453103011d43c6c2d8b89478 to your computer and use it in GitHub Desktop.
This script dynamically creates and displays random text elements on a web-page at random positions. It retrieves keywords from the '<meta name="keywords">' tag, splits them into an array, and periodically selects a random keyword to display. Each text element is styled as a 'div' with the class 'dynamic-text', positioned randomly within the vie…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get meta keywords | |
const metaKeywords = document | |
.querySelector('meta[name="keywords"]') | |
.getAttribute("content"); | |
const words = metaKeywords.split(", "); | |
const body = document.body; | |
function createRandomText() { | |
const text = document.createElement("div"); | |
text.className = "dynamic-text"; | |
text.innerText = words[Math.floor(Math.random() * words.length)]; | |
text.setAttribute("aria-hidden", "true"); | |
body.appendChild(text); | |
// Wait for the text element to be rendered to get its dimensions | |
const textRect = text.getBoundingClientRect(); | |
// Calculate random positions | |
const maxTop = window.innerHeight - textRect.height; | |
const maxLeft = window.innerWidth - textRect.width; | |
text.style.top = Math.random() * maxTop + "px"; | |
text.style.left = Math.random() * maxLeft + "px"; | |
setTimeout(() => { | |
body.removeChild(text); | |
}, 5000); | |
} | |
setInterval(createRandomText, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment