<html>
<head>
<title>chapter3</title>
<script>
function resizeRock(){
document.getElementById("rockImg").style.height =
(document.body.clientHeight -100) * 0.3; //根据客户端窗口尺寸,缩放rock的大小
}
function rockUser() {
alert("hello, i'm your pet rock.");
}
function touchRock() {
var userName = prompt("what's your name?");
if (userName){
alert("It is good to meet you," + userName + ".");
}
document.getElementById("rockImg").src = "rock_happy.png";
setTimeout("document.getElementById('rockImg').src = 'rock.png';", 5 * 1000);
//单次定时器setTimeout()函数需定时器停止时要运行的语句,和延迟的时间,这里延迟5秒
//还有一个是间隔定时器,setInterval()函数,这个是每一次间隔时间到期就运行语句(反复)
}
</script>
</head>
<body onload="resizeRock(); rockUser();" onresize="resizeRock()">
<!--调整浏览器大小时将触发onresize事件,调用resizeRock函数响应onresize事件-->
<div style="margin-top:100px; text-align:center">
<img id="rockImg" src="rock.png" onclick="touchRock()">
</div>
</body>
</html>
转载于:https://my.oschina.net/bigfunny/blog/540288