Javascript 确定鼠标位置

 

 

 

ev.pageY

ev.clientY

document.body.

scrollTop

document.body.

clientTop

 

scrollPosTop

 

mousePos.y

window.

pageYOffset

 

document.documentElement.

scrollTop

 

IE6

undefined

281

0

0

1028

281

undefined

1028

IE7

undefined

281

0

0

1028

281

undefined

1028

IE8

undefined

281

0

0

1028

281

undefined

1028

IE9

1306

378

0

0

928

378

928

928

Chrome

1416

408

1008

0

1008

1416

1008

0

Firefox

1132

336

0

0

796

1132

796

796

 

原先的脚本:

   function mouseCoords(ev) {

            var nVer = navigator.appVersion.toString();

            if (nVer.indexOf("MSIE 9.0") != -1) {

                //IE9

                return {

                    x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,

                    y: ev.clientY + document.body.scrollTop - document.body.clientTop

                };

            }

            else {

                if (ev.pageX || ev.pageY) {

                    return { x: ev.pageX, y: ev.pageY };

                }

                return {

                    x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,

                    y: ev.clientY + document.body.scrollTop - document.body.clientTop

                };

            }

        }

 

 后来发现应该改为:

   function mouseCoords(ev) {
                return {
                    x: ev.clientX,
                    y: ev.clientY
                };
            }

在IE6、IE7、Ie8、IE9、chrome 11、firefox 3.6下均正常。

 

你可能感兴趣的:(JavaScript,function,IE,chrome,脚本,firefox)