js獲取鼠標當前所在頁面位置


       //此方法對於嵌套在一個頁面A中的B頁面,獲取B頁面的位置在IE9和其他瀏覽器(包括IE其他系列瀏覽器)下有些不同,IE9是根據瀏覽器來定位的,FF及其他則是根據當前頁面也就是嵌套的頁面來定位的(真正兼容還待改進)

    function
getEvent() //同時兼容ie和ff的寫法 { if (document.all) return window.event; func = getEvent.caller; while (func != null) { var arg0 = func.arguments[0]; if (arg0) { if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) { return arg0; } } func = func.caller; } return null; } var __is_ff = (navigator.userAgent.indexOf("Firefox") != -1); //Firefox function getMouseLocation() { var e = getEvent(); var mouseX = 0; var mouseY = 0; if (__is_ff) { mouseX = e.layerX + document.body.scrollLeft; mouseY = e.layerY + document.body.scrollLeft; } else { mouseX = e.x + document.body.scrollLeft; mouseY = e.y + document.body.scrollTop; } return { x: mouseX, y: mouseY }; } //調用的方法,彈出當前所在頁面的位置 function show() { var test = getMouseLocation(); alert(test.x + ":" + test.y); }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM