-
Notifications
You must be signed in to change notification settings - Fork 22.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ondragstart: convert static example to live example #15318
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,7 +16,7 @@ A {{domxref("GlobalEventHandlers","global event handler")}} for the | |||||
## Syntax | ||||||
|
||||||
```js | ||||||
var dragstartHandler = targetElement.ondragstart; | ||||||
targetElement.ondragstart = dragstartHandler; | ||||||
``` | ||||||
|
||||||
### Return value | ||||||
AnilSeervi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
@@ -27,63 +27,116 @@ var dragstartHandler = targetElement.ondragstart; | |||||
## Example | ||||||
|
||||||
This example demonstrates using the | ||||||
{{domxref("GlobalEventHandlers.ondragstart","ondragstart")}} attribute handler to set an | ||||||
element's {{event("dragstart")}} event handler. | ||||||
{{domxref("GlobalEventHandlers.ondragstart","ondragstart")}} global event handler to set an | ||||||
element's `dragstart` event handler. | ||||||
|
||||||
```js | ||||||
<!DOCTYPE html> | ||||||
<html lang=en> | ||||||
<title>Examples of using the ondrag Global Event Attribute</title> | ||||||
<meta content="width=device-width"> | ||||||
<style> | ||||||
div { | ||||||
margin: 0em; | ||||||
### HTML | ||||||
|
||||||
```html | ||||||
<div> | ||||||
<p id="source" draggable="true"> | ||||||
Select this element, drag it to the Drop Zone and then release the selection to move the element. | ||||||
</p> | ||||||
</div> | ||||||
<div id="target">Drop Zone</div> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
As we are on an event page, the 'target' and 'event.target' can become source of confusion. |
||||||
|
||||||
<textarea readonly id="event-log"></textarea> | ||||||
<button id="reload">Reload</button> | ||||||
``` | ||||||
|
||||||
### CSS | ||||||
|
||||||
```css | ||||||
div, #event-log { | ||||||
margin: 1em; | ||||||
} | ||||||
|
||||||
#source, #target { | ||||||
padding: 2em; | ||||||
} | ||||||
#source { | ||||||
color: blue; | ||||||
border: 1px solid black; | ||||||
} | ||||||
#target { | ||||||
border: 1px solid black; | ||||||
} | ||||||
</style> | ||||||
</head> | ||||||
<script> | ||||||
function drag_handler(ev) { | ||||||
console.log("Drag"); | ||||||
} | ||||||
|
||||||
#source { | ||||||
color: blue; | ||||||
} | ||||||
|
||||||
#event-log { | ||||||
width: 25rem; | ||||||
height: 4rem; | ||||||
AnilSeervi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
margin-bottom: 0; | ||||||
padding: .2rem; | ||||||
} | ||||||
``` | ||||||
|
||||||
### JavaScript | ||||||
|
||||||
```js | ||||||
const source = document.getElementById("source"); | ||||||
const target = document.getElementById("target"); | ||||||
const event_log = document.getElementById("event-log"); | ||||||
|
||||||
function dragstart_handler(ev) { | ||||||
console.log("dragStart"); | ||||||
ev.dataTransfer.setData("text", ev.target.id); | ||||||
event_log.textContent += "dragStart\n"; | ||||||
// Change the source element's background color to signify drag has started | ||||||
ev.currentTarget.style.border = "dashed"; | ||||||
ev.dataTransfer.setData("text", ev.target.id); | ||||||
} | ||||||
|
||||||
function dragover_handler(ev) { | ||||||
event_log.textContent += "dragOver\n"; | ||||||
// Change the target element's border to signify a drag over event | ||||||
// has occurred | ||||||
ev.currentTarget.style.background = "lightblue"; | ||||||
ev.preventDefault(); | ||||||
} | ||||||
|
||||||
function drop_handler(ev) { | ||||||
console.log("Drop"); | ||||||
ev.currentTarget.style.background = "lightyellow"; | ||||||
event_log.textContent += "Drop\n"; | ||||||
AnilSeervi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
ev.preventDefault(); | ||||||
const data = ev.dataTransfer.getData("text"); | ||||||
ev.target.appendChild(document.getElementById(data)); | ||||||
} | ||||||
|
||||||
ev.preventDefault(); | ||||||
var data = ev.dataTransfer.getData("text"); | ||||||
ev.target.appendChild(document.getElementById(data)); | ||||||
function dragenter_handler(ev) { | ||||||
event_log.textContent += "dragEnter\n"; | ||||||
// Change the source element's background color for enter events | ||||||
ev.currentTarget.style.background = "yellow"; | ||||||
} | ||||||
|
||||||
function dragover_handler(ev) { | ||||||
console.log("dragOver"); | ||||||
ev.preventDefault(); | ||||||
function dragleave_handler(ev) { | ||||||
event_log.textContent += "dragLeave\n"; | ||||||
// Change the source element's border back to white | ||||||
ev.currentTarget.style.background = "white"; | ||||||
} | ||||||
|
||||||
function dragend_handler(ev) { | ||||||
event_log.textContent += "dragEnd\n"; | ||||||
// Change the target element's background color to visually indicate | ||||||
// the drag ended. | ||||||
target.style.background = "pink"; | ||||||
} | ||||||
</script> | ||||||
<body> | ||||||
<h1>Examples of <code>ondrag</code>, <code>ondrop</code>, <code>ondragstart</code>, <code>ondragover</code></h1> | ||||||
<div> | ||||||
<p id="source" ondrag="drag_handler(event);" ondragstart="dragstart_handler(event);" draggable="true"> | ||||||
Select this element, drag it to the Drop Zone and then release the selection to move the element.</p> | ||||||
</div> | ||||||
<div id="target" ondrop="drop_handler(event);" ondragover="dragover_handler(event);">Drop Zone</div> | ||||||
</body> | ||||||
</html> | ||||||
|
||||||
// Set handlers for the source's drag - start/enter/leave/end events | ||||||
source.ondragstart = dragstart_handler; | ||||||
source.ondragenter = dragenter_handler; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think these(also ondragleave) should be registered on drop target[ref]. On drag leave the |
||||||
source.ondragleave = dragleave_handler; | ||||||
source.ondragend = dragend_handler; | ||||||
|
||||||
// Set handlers for the target's drop and dragover events | ||||||
target.ondrop = drop_handler; | ||||||
target.ondragover = dragover_handler; | ||||||
|
||||||
// Set click event listener on button to reload the example | ||||||
const button = document.getElementById("reload"); | ||||||
button.addEventListener("click", () => { | ||||||
document.location.reload(); | ||||||
}); | ||||||
``` | ||||||
|
||||||
### Result | ||||||
|
||||||
{{ EmbedLiveSample('Example', '100', '450') }} | ||||||
|
||||||
## Specifications | ||||||
|
||||||
{{Specifications}} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a better model to copy would be this one: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick#syntax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @AnilSeervi but I would literally copy the structure, using value rather than return type - so it would look like this