简体   繁体   中英

How to get return value from mouse capturing function?

How to get return value from mouse capturing function?

        function getMousePosition(e) 
        {
            positionX = e.pageX;
            positionY = e.pageY;

            if (positionX < 0){positionX = 0;}
            if (positionY < 0){positionY = 0;} 

            return positionX + " " + positionY;
        }

as in var mousePositions = getMousePosition(e);

 function getMousePosition(e)  
        { 
            e = e || event //for cross browser implentation
            var positionX = e.pageX; 
            var positionY = e.pageY; 

            if (positionX < 0){positionX = 0;} 
            if (positionY < 0){positionY = 0;}  

            return {positionX: positionX, positionY: positionY}; 
        } 

Now you can do this

var mousePositions = getMousePositions(someEventObject);

alert(mousePositions.positionX);
alert(mousePositions.positionY);

您必须像以下示例中那样将onmousemove Event-Listener分配给您的函数:

document.onmousemove = getMousePosition();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM