首页 >web前端 >H5教程 >如何实时获取鼠标的当前坐标

如何实时获取鼠标的当前坐标

一个新手
一个新手原创
2018-05-11 14:18:434011浏览

本文来做一个简单的实时获取鼠标坐标的功能,在canvas动画开发中,获取鼠标的坐标,键盘的按键等等,都是常用的操作,我们就慢慢得把他们封装成一个公共库。

一、事件的兼容:

function bindEvent(obj, event, fn) {
        if (obj.attachEvent) { //ie
            obj.attachEvent('on' + event, function () {
                fn.call(obj);
            });
        } else {
            //chrome&ff
            obj.addEventListener(event, fn, false);
        }
    }

上面兼容ie8以及修正this关键字在ie低版本的指向,下面兼容chrome和ff。其他更常用的封装可以参考我的javascript开源框架gdom

二、用立即表达式搭建一个基本的库

添加获取鼠标坐标的方法

;(function (window) {
    window.G = {};
    function bindEvent(obj, event, fn) {
        if (obj.attachEvent) { //ie
            obj.attachEvent('on' + event, function () {
                fn.call(obj);
            });
        } else {
            //chrome&ff
            obj.addEventListener(event, fn, false);
        }
    }

    G.getPos = function( dom ){
        var oPos = { x : 0, y : 0 };
        bindEvent( dom, 'mousemove', function( ev ){
            var oEvent = ev || event, x, y;
            if ( oEvent.pageX || oEvent.pageY ){
                x = oEvent.pageX;
                y = oEvent.pageY;
            }else {
                x = oEvent.clientX + document.body.scrollLeft || document.documentElement.scrollLeft;
                y = oEvent.clientX + document.body.scrollTop || document.documentElement.scrollTop;
            }
            x -= dom.offsetLeft;
            y -= dom.offsetTop;
            oPos.x = x;
            oPos.y = y;
        } );
        return oPos;
    };

})(window);

三、引入封装的js库,绑定canvas为监听对象,打印当前鼠标的坐标

鼠标的坐标,我这里画了2根线,便于观察.



<script>
;(function (window) {
    window.G = {};
    function bindEvent(obj, event, fn) {
        if (obj.attachEvent) { //ie
            obj.attachEvent(&amp;#39;on&amp;#39; + event, function () {
                fn.call(obj);
            });
        } else {
            //chrome&amp;ff
            obj.addEventListener(event, fn, false);
        }
    }

    G.getPos = function( dom ){
        var oPos = { x : 0, y : 0 };
        bindEvent( dom, &#39;mousemove&#39;, function( ev ){
            var oEvent = ev || event, x, y;
            if ( oEvent.pageX || oEvent.pageY ){
                x = oEvent.pageX;
                y = oEvent.pageY;
            }else {
                x = oEvent.clientX + document.body.scrollLeft || document.documentElement.scrollLeft;
                y = oEvent.clientX + document.body.scrollTop || document.documentElement.scrollTop;
            }
            x -= dom.offsetLeft;
            y -= dom.offsetTop;
            oPos.x = x;
            oPos.y = y;
        } );
        return oPos;
    };

})(window);
</script>

<script>
window.onload = function(){
    var oCanvas = document.querySelector( "#canvas" ),
        oGc = oCanvas.getContext( &#39;2d&#39; ),
        width = oCanvas.width, height = oCanvas.height,
        oInfo = document.querySelector( "#info" ),
        oPos = G.getPos( oCanvas );
        oCanvas.addEventListener( "mousemove", function(){
            
            oGc.clearRect( 0, 0, width, height );
            oGc.beginPath();
            oGc.moveTo( oPos.x, 0 );
            oGc.lineTo( oPos.x, height );
            oGc.moveTo( 0, oPos.y );
            oGc.lineTo( width, oPos.y );
            oGc.closePath();
            oGc.strokeStyle = &#39;#09f&#39;;
            oGc.stroke();

            oInfo.innerHTML = &#39;鼠标的当前坐标是:(&#39; + oPos.x + &#39;,&#39; + oPos.y + &#39;)&#39;;
        }, false );
}
</script>



以上是如何实时获取鼠标的当前坐标 的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn