JavaScript move image
This section illustrates you how to move an image on the window using JavaScript.
In the following code, we have defined an image 'node.jpg' in order to move it on the browser. For this, we have used onClick event handler to specify what should happen when the mouse is clicked on the window. The properties clientX and clientY of event object indicates the cursor's horizontal and vertical position when the event occurs relative to the upper-left corner of the window and the pageX and pageY provide coordinates in the document's space. To set the element top position relative to the next element top edge we have used the property style.left and the syle.left sets the position of the left edge of an element relative to the left edge of the next element. This code works correctly on Internet Explorer.
Here is the code:
| <html> <h2>Move Image</h2> <script type="text/javascript"> var X, Y; window.document.onclick=moveImage; function moveImage(){ X = (document.layers) ? e.pageX : event.clientX Y = (document.layers) ? e.pageY : event.clientY document.getElementById('image').style.position="absolute"; document.getElementById('image').style.left=X; document.getElementById('image').style.top=Y; } </script> <img id="image" src="node.jpg"></img> </html> |
Output will be displayed as:
If you click anywhere on the window the image will moved to that position.
Tutorials
- Clear cookie example
- JavaScript getElementById innerHTML
- JavaScript getElementById Style
- Javascript Examples
- JavaScript add row dynamically to table
- New Page 1
- JavaScript Change link
- JavaScript Checkbox getElementById
- javascript clear textarea
- JavaScript Clock Timer
- JavaScript Cookies
- JavaScript Date Difference
- JavaScript duplicate string
- JavaScript Email Validation
- javascript focus input
- JavaScript get excel file data
- JavaScript getAttribute Href
- JavaScript getAttribute Style
- JavaScript getElementById div
- JavaScript getElementById Iframe
- JavaScript getElementById select
- JavaScript Hide Button
- JavaScript Hide Div
- JavaScript hide image
- JavaScript Hide Table Column
- JavaScript Hide Table Rows
- JavaScript Key Event
- JavaScript link
- JavaScript method location
- JavaScript move div
- JavaScript move file
- JavaScript move image
- JavaScript Navigate Back
- JavaScript navigate to page
- JavaScript Navigate to URL
- JavaScript indexOf substring
- JavaScript onkeypress event
- JavaScript Open file
- JavaScript Open link in new window
- JavaScript Open Modal Window


