js判斷滑鼠是否停止移動

來源:互聯網
上載者:User

標籤:

本程式實現當滑鼠在一個特定的div內懸停n秒時,判斷出已經停止移動。

思路:

1.定義全域變數滑鼠移動狀態imouse,定時器timer。當滑鼠在div內移動時,imouse值為1,相反靜止時值為0;timer可以實現每過n秒就判斷滑鼠狀態,然後把imouse重設為0;

2.div監聽onmouseover。當滑鼠進入地區時,就設定定時器;

3.div監聽onmousemove。當滑鼠移動時,設定imouse值為1;

4.div監聽onmouseout。當滑鼠離開時,清除定時器timer

<html>    <head>        <meta http-equiv="charset" content="utf-8" />        <script type="text/javascript">            var tip;            var imouse = 0;            var timer;            function startup() {                tip = document.getElementById("tip");            }            function mouseIn() {                timer = window.setInterval(foo, 1000);            }            function mouseOut() {                window.clearInterval(timer);            }            function mouseMove() {                imouse = 1;                tip.innerHTML = "move";            }            var foo = function() {                if (imouse == 0) {                    tip.innerHTML = "still";                }                imouse = 0;            }        </script>    </head>    <body onload="startup()">        <div id="imgArea" onmousemove="mouseMove()" onmouseover="mouseIn()" onmouseout="mouseOut()">            <!-- 這裡隨便放一些東西 -->        </div>        <div id="tip"></div>    </body></html>

 

js判斷滑鼠是否停止移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.