Javascript学习笔记(13_2) --js事件案例(小人快跑)



	
		
		小人快跑
		
	
	
		
		
	

var imgs = document.getElementById("imgs");
var i = 0;
document.onkeydown = function(e){
	
	var e = window.event || e;
	
	switch(e.keyCode){
		
		case 37: //左
			
			imgs.style.left = imgs.offsetLeft - 10+"px";
			run("2");
			
		break;
		case 38: //上
			imgs.style.top = imgs.offsetTop- 10+"px";
			run("4");
			
		break;
		case 39: //右
			imgs.style.left = imgs.offsetLeft + 10+"px";
			run("3");
			
		break;
		case 40: //下
			imgs.style.top = imgs.offsetTop + 10+"px";
			run("1");
			
		break;
	}
}

function run(value){
	i++;
	if(i > 9){
		i = 1;
	}
	imgs.setAttribute("src","images/"+value+i+".png")
}


你可能感兴趣的:(JavaScript)