js判斷滑鼠位置是否在某個div中

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   sp   java   on   

div的onmouseout事件讓div消失時,會出現這樣的情況,就是當滑鼠移至div中的其它內容時,此時也判定為離開div,會觸發 onmouseout事件,這樣div中的內容就不能操作了。解決的辦法是當觸發onmouseout事件時,先判斷滑鼠是否在div內,如果在,說明鼠 標並沒有離開div,就不刪除div,否則,刪除之。OK,現在問題解決了。
就是找到該div左上方和右下角座標,判斷滑鼠的座標是否在這一地區就可以了。
[javascript] view plaincopyprint?

    div.onmouseout=function(event){ 
                        var div = document.getElementById("test"); 
                    <span style="color:#FF6600;">var x=event.clientX; 
                        var y=event.clientY; 
                        var divx1 = div.offsetLeft; 
                        var divy1 = div.offsetTop; 
                        var divx2 = div.offsetLeft + div.offsetWidth; 
                        var divy2 = div.offsetTop + div.offsetHeight;  </span> 
                        if( x < divx1 || x > divx2 || y < divy1 || y > divy2){ 
                                        //如果離開,則執行。。 
                                    } 

後面為一些常用屬性方便尋找:
clientHeight     擷取對象的高度,不計算任何邊距、邊框、捲軸,但包括該對象的補白。  
clientLeft     擷取    offsetLeft     屬性和用戶端區域的實際左邊之間的距離。
clientTop     擷取    offsetTop     屬性和用戶端區域的實際頂端之間的距離。
clientWidth     擷取對象的寬度,不計算任何邊距、邊框、捲軸,但包括該對象的補白。
offsetHeight     擷取對象相對於版面或由父座標    offsetParent     屬性指定的父座標的高度。
offsetLeft     擷取對象相對於版面或由    offsetParent     屬性指定的父座標的計算左側位置。
offsetParent     擷取定義對象    offsetTop     和    offsetLeft     屬性的容器物件的引用。
offsetTop     擷取對象相對於版面或由    offsetTop     屬性指定的父座標的計算頂端位置。
offsetWidth     擷取對象相對於版面或由父座標    offsetParent     屬性指定的父座標的寬度。
offsetX     設定或擷取滑鼠指標位置相對於觸發事件的對象的    x     座標。
offsetY     設定或擷取滑鼠指標位置相對於觸發事件的對象的    y     座標。
clientX,clientY   滑鼠當前相對於網頁的位置,當滑鼠位於頁面左上方時clientX=0, clientY=0
screenX, screenY是相對於使用者顯示器的位置
網頁可見地區寬: document.body.clientWidth
網頁可見地區高: document.body.clientHeight
網頁可見地區寬: document.body.offsetWidth    (包括邊線的寬)
網頁可見地區高: document.body.offsetHeight   (包括邊線的寬)
網頁本文全文寬: document.body.scrollWidth
網頁本文全文高: document.body.scrollHeight
網頁被捲去的高: document.body.scrollTop
網頁被捲去的左: document.body.scrollLeft
網頁本文部分上: window.screenTop
網頁本文部分左: window.screenLeft
螢幕解析度的高: window.screen.height
螢幕解析度的寬: window.screen.width
螢幕可用工作區高度: window.screen.availHeight
螢幕可用工作區寬度:window.screen.availWidth

 

本文出自:http://binbinwudi.iteye.com/blog/1915135

js判斷滑鼠位置是否在某個div中

聯繫我們

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