QuestionWe would like to know how to move element position. Answer
The code above is rendered as follows: JavaScript move divThis section illustrates you how to move a div element on the window using JavaScript. In the following code, we have defined a div element consisting of text 'Hello World' in order to move it on the browser. For this, we have used onClick event handler to specifiy 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.top and the style.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> Output will be displayed as: If you click anywhere on the window, the element will move to that position. Download Source Code:
Learn how to create a draggable HTML element with JavaScript and CSS. Draggable DIV ElementCreate a Draggable DIV ElementStep 1) Add HTML:Example<!-- Draggable DIV --> Step 2) Add CSS:The only important style is Example #mydiv { #mydivheader { Step 3) Add JavaScript:Example // Make the DIV element draggable: function dragElement(elmnt) { function dragMouseDown(e) { function elementDrag(e) { function closeDragElement() { Try it Yourself » |