一、 由來
曾有一些“純屬娛樂”的網頁,有一些有獎問答題,但又故意讓你點不到正確的答案:當你滑鼠
一靠近按鈕,按鈕就逃開了。自己也試著做了一個,供大家學習娛樂。
二、 原理
其實非常簡單:當滑鼠放到按鈕上面時,改變按鈕的位置(賦隨機值)。
三、 代碼
代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>你中獎了!!!!</title>
<style>
body {
text-align:center;
}
#d {
margin-left:0 auto;
margin-right:0 auto;
text-align:center;
}
input {
width:100px;
height:40px;
}
</style>
</head>
<body>
<div id="d">
<h2>恭喜你得了一等獎,點擊領取:</h2>
<input type="button" onmouseover="move(this);" onclick="alert('騙你的=_=');" value="確定" />
</div>
</body>
<script type="text/javascript" >
var height = document.documentElement.clientHeight - 100; //按鈕可能上下移動的距離
var width = document.documentElement.clientWidth - 40; //按鈕可能左右移動的距離
function move(obj) {
obj.style.position = "absolute";
obj.style.top = Math.random() * height + "px";
obj.style.left = Math.random() * width + "px";
}
function cancel() {
alert("你已放棄領獎,謝謝!");
window.close();
}
</script>
</html>
四、 注意
(1) style下的屬性在被賦值之前為空白。
(2) 記得要在style.top/left等之後加上px,否則對於許多瀏覽器都不能正確設定該
值。