
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
HTML DOM MouseEvent screenX Property
The HTML DOM MouseEvent screenX property returns the horizontal (x) coordinate of the mouse pointer relative to the users screen display if a mouse event was triggered. Use with screenY to get the vertical coordinate as well.
Following is the syntax −
Returning reference to the screenX object
MouseEventObject.screenX
Let us see an example of MouseEvent screenX property −
Example
<!DOCTYPE html> <html> <head> <title>MouseEvent screenX</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-screenX</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>
Output
After clicking ‘Start’ button and cursor in green (safe) area −
After clicking ‘Start’ button and cursor at end of green (safe) area −
After clicking ‘Start’ button and cursor in red (danger) area −
- Related Articles
- HTML DOM MouseEvent clientX Property
- HTML DOM MouseEvent clientY Property
- HTML DOM MouseEvent offsetX Property
- HTML DOM MouseEvent offsetY Property
- HTML DOM MouseEvent pageX Property
- HTML DOM MouseEvent pageY Property
- HTML DOM MouseEvent screenY Property
- HTML Window screenX Property
- HTML DOM MouseEvent relatedTarget
- HTML DOM MouseEvent Object
- HTML DOM tagName Property
- HTML DOM accessKey Property
- HTML DOM activeElement Property
- HTML DOM name Property
- HTML DOM nextElementSibling Property

Advertisements