为什么这个代码去掉window.onload = function(){}就会出现错误呢?
为什么这个代码去掉window.onload = function(){}就会出现错误呢?
<style type="text/css">
#contain{
height:477px;
position:relative;
margin:0 auto;
overflow: hidden;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
img{
display: block;
position: absolute;
left:0;
border-left: 1px solid #ccc;
}
</style>
</head>
<body>
<div id='contain'>
<img src="img/door1.png" />
<img src="img/door2.png" />
<img src="img/door3.png" />
<img src="img/door4.png" />
</div>
<script type="text/javascript">
// window.onload = function(){
var imgs = document.getElementsByTagName('img');
var box = document.getElementById('contain');
var exploseWidth = 160;
var imgWidth = imgs[0].offsetWidth;
var boxWidth = imgWidth + (imgs.length-1) * exploseWidth;
box.style.width = boxWidth + 'px';
function startMove(){
for(var i=1;i<imgs.length;i++){
imgs[i].style.left = imgWidth + exploseWidth * (i-1) + 'px';
}
}
startMove();
// }
</script>
</body>