2016-08-05 64 views
0

我试图做一个空的div与宽度和高度和背景颜色在屏幕上移动调用一个函数onclick。它不需要任何参数。但是当我点击的元素,它说:“遗漏的类型错误:未能执行的‘元素’‘动画’:?1点所需的参数,但只有0现在我失去了什么Uncaught TypeError:需要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>

+0

我没有在Chrome v52中遇到错误 – evolutionxbox

回答

3

尝试重命名animate功能到别的东西。好像你打电话不是你的功能,但元素的animate方法box1

+0

谢谢。这工作。我“打勾”并投票答复。我能看到它。但我不确定是否有其他人可以。它说...“因为我的声望低于15,你的反馈将被记录下来,但不会在屏幕上直播”。对不起,那个...... – xxkaiwaxx

+0

@xxkaiwaxx多数民众赞成在) – Maxx

0

首先,使用的addEventHandler!不要混合使用HTML和Javascript!

然后处理函数必须是函数,而不是函数的结果。 由于“animate”是一个函数,“animate()”是无效的。

+0

第二个是错误的。如果你使用onclick属性,你需要调用函数。 – Maxx

相关问题