Skip to content

Instantly share code, notes, and snippets.

@Swivelgames
Last active October 3, 2018 19:13
Show Gist options
  • Save Swivelgames/a3b39d812c6c9e05f92a2562f0b088df to your computer and use it in GitHub Desktop.
Save Swivelgames/a3b39d812c6c9e05f92a2562f0b088df to your computer and use it in GitHub Desktop.
Dance elements on a webpage
const randBetween = (min, max) => Math.floor(Math.random() * max) + min;
const dance = () => {
const all = document.body.querySelectorAll('*');
return Array.prototype
.slice.apply(all)
.reduce(
(promise, e) => promise.then(
new Promise((resolve, reject) => setTimeout(() => {
const top = randBetween(0, window.visualViewport.height);
const left = randBetween(0, window.visualViewport.width);
e.style.position = 'fixed';
e.style.top = top + 'px';
e.style.left = left + 'px';
e.style.backgroundColor = 'transparent';
e.style.overflow = 'visible';
resolve();
}, 10))
), Promise.resolve()
).then(() =>
new Promise((resolve, reject) => {
setTimeout(() => dance(), all * 15)
})
);
};
dance();
Array.prototype.slice.apply(
document.querySelectorAll('a, button')
).forEach(
(e) => e.addEventListener('click',
(e) => e.preventDefault()
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment