I'm trying to make animations, and I want to be able to do this:
document.getElementById("item").move()
instead of this:
move(document.getElementById("item"))
I tried achieving that with Object.prototype.move, but that didn't work.
I'm trying to make animations, and I want to be able to do this:
document.getElementById("item").move()
instead of this:
move(document.getElementById("item"))
I tried achieving that with Object.prototype.move, but that didn't work.
A DOM HTMLElement
is an Object
so it should work, however, an Object
is not necessarily an HTMLElement
, so you should add that method to HTMLElement.prototype
instead:
HTMLElement.prototype.move = function() {
let position = 0;
const self = this;
function render() {
self.style = `left: ${position++}px`;
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
document.getElementById('square').move();
#square {
position: fixed;
height: 20px;
width: 20px;
background-color: blue;
}
<div id="square">
</div>