我试图让一个宽度和高度以及背景颜色的空 div 在屏幕上移动,调用一个函数 onclick。它不需要任何论据。但是当我单击该元素时,它说:“未捕获的类型错误:无法在'元素'上执行'动画':需要 1 个参数,但只有 0 个存在。我错过了什么?
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>
最佳答案
尝试重命名animate
作用于别的东西。好像你调用的不是你的函数,而是 animate
元素方法box1
.
关于javascript - 未捕获的 TypeError : 1 argument required, 但只有 0 存在...但是我的函数没有参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38784199/