Skip to content

Instantly share code, notes, and snippets.

@liamshort
Last active March 19, 2025 21:42
Show Gist options
  • Save liamshort/7a268565453103011d43c6c2d8b89478 to your computer and use it in GitHub Desktop.
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…
// 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