逃走的按鈕-javascript

來源:互聯網
上載者:User

一、 由來

    曾有一些“純屬娛樂”的網頁,有一些有獎問答題,但又故意讓你點不到正確的答案:當你滑鼠

一靠近按鈕,按鈕就逃開了。自己也試著做了一個,供大家學習娛樂。

 

二、 原理

    其實非常簡單:當滑鼠放到按鈕上面時,改變按鈕的位置(賦隨機值)。

 

三、 代碼

 

代碼

<!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,否則對於許多瀏覽器都不能正確設定該

值。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.