HTML DOM MouseEvent screenY 属性
HTML DOM MouseEvent screenY 属性返回鼠标事件触发时鼠标指针相对于用户屏幕显示的垂直(y)坐标。与 screenY 搭配使用还可以获取水平坐标。
以下为语法 −
返回对screenY对象的引用
MouseEventObject.screenY
让我们来看一个有关MouseEvent screenY属性的示例 −
示例
<!DOCTYPE html> <html> <head> <title>MouseEvent screenY</title> <style> * { padding: 2px; margin:5px; } form { width:70%; margin: 0 auto; text-align: center; } #outer { width:70%; margin: 0 auto; padding: 0; text-align: center; border:1px solid black; height: 105px; background-color: #28a745; } input[type="button"] { border-radius: 10px; } #upper { border-bottom: 1px solid black; height: 40px; margin: 0 0 15px 0; background-color: #DC3545; } #lower { border-top: 1px solid black; height: 40px; margin: 15px 0 0 0; background-color: #DC3545; } </style> </head> <body> <form> <fieldset> <legend>MouseEvent-screenY</legend> <div id="outer"> <div id="upper"><h2>Danger</h2></div> <div id="lower"><h2>Danger</h2></div> </div> <input type="button" id="start" value="Start" onclick="gameStart()"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById('divDisplay'); var gameDisplay = document.getElementById('outer'); function playGame(event) { var x = event.screenX; var y = event.screenY; if(y > 330 && y < 345){ divDisplay.textContent = 'Keep Going!'; if(x === 1171){ divDisplay.textContent = 'Congrats! You Did it!'; gameDisplay.removeEventListener('mousemove', playGame); } } else{ divDisplay.textContent = 'You moved to DANGER area. You loose!'; gameDisplay.removeEventListener('mousemove', playGame); } } function gameStart(){ gameDisplay.addEventListener('mousemove',playGame); } </script> </body> </html>
输出
点击“开始”按钮后,光标在绿色(安全)区域 − 中
点击“开始”按钮后,光标在绿色(安全)区域末尾 −
点击“开始”按钮后,光标在红色(危险)区域 − 中
广告