Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  152] [ 5]  / answers: 1 / hits: 23305  / 8 Years ago, fri, august 5, 2016, 12:00:00

Im trying to make an empty div with a width and height and background-color move across the screen calling a function onclick. It takes no arguments. But when i click the element it says: Uncaught TypeError: Failed to execute 'animate' on 'Element': 1 argument required, but only 0 present. What am i missing?





var container = document.getElementById('container');
var box1 = document.getElementById('box1');
var containerWidth = container.clientWidth;
var box1Width = box1.clientWidth;

function animate() {
box1.style.left = '0px';
setTimeout(function() {
box1.style.transition = 'left 1000ms ease';
box1.style.left = containerWidth - box1Width + 'px';
}, 1);
}

body {
margin: 0;
}

#container {
position: relative;
height: 100vh;
width: 100vw;
background-color: aqua;
}

#box1 {
position: relative;
height: 100px;
width: 100px;
left: 0px;
background-color: yellow;
}

<div id=container>
<div id=box1 onclick=animate()></div>
</div>




More From » animation

 Answers
19

Try to rename animate function to something else. Seems like you call not your function, but animate method of element box1.


[#61139] Wednesday, August 3, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alexandreah

Total Points: 720
Total Questions: 85
Total Answers: 90

Location: Central African Republic
Member since Fri, Jun 5, 2020
4 Years ago
alexandreah questions
Mon, Aug 9, 21, 00:00, 3 Years ago
Wed, Jan 8, 20, 00:00, 4 Years ago
Mon, Dec 16, 19, 00:00, 5 Years ago
Fri, Sep 27, 19, 00:00, 5 Years ago
;