|
13 | 13 | | [Using Event Delegation](#using-event-delegation) | |
14 | 14 | | [Triggering DOM Elements Programmatically](#triggering-dom-elements-programmatically) | |
15 | 15 | | [Event Handler Functions and `this`](#event-handler-functions-and-this) | |
| 16 | +| [Drag and Drop Events](#drag-and-drop-events) | |
16 | 17 |
|
17 | 18 | ## [Introduction to Events in JavaScript](https://drive.google.com/uc?export=view&id=1tfi-wZ9BYL2wISnyZ2JCcutRPApHpyCV) |
18 | 19 |
|
@@ -701,4 +702,60 @@ If an event handler is bound to the `div.outer` element, and the user clicks on |
701 | 702 |
|
702 | 703 | It is important to keep in mind the event bubbling and capturing phase as well when considering the value of `this` in nested elements. |
703 | 704 |
|
704 | | -It's important to note that when using arrow functions as event handler functions, the `this` keyword behaves differently. Arrow functions inherit the `this` value of the enclosing lexical scope, which is often the global object or the object that the arrow function is defined in. Therefore, if we use an arrow function as an event handler, the `this` keyword will not refer to the DOM element that the event was triggered on. |
| 705 | +It's important to note that when using arrow functions as event handler functions, the `this` keyword behaves differently. Arrow functions inherit the `this` value of the enclosing lexical scope, which is often the global object or the object that the arrow function is defined in. Therefore, if we use an arrow function as an event handler, the `this` keyword will not refer to the DOM element that the event was triggered on. |
| 706 | +
|
| 707 | +## [Drag and Drop Events](https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API) |
| 708 | +
|
| 709 | +<img src="https://drive.google.com/uc?export=view&id=1kkXlf52mdwARgPsNL_8UIbvpMOatWQfi" height="400" width="800" alt="academind slide"> |
| 710 | +
|
| 711 | +- To make elements draggable: |
| 712 | +
|
| 713 | + - Mark them by adding the [`draggable`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/draggable) attribute or setting the [`draggable`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/draggable) property on the DOM elements to `true`. |
| 714 | + - Both the attribute or the property need to be set to `true`. |
| 715 | +
|
| 716 | +- Listen to the [`dragstart`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragstart_event) event on the draggable element, which is triggered when a user starts dragging the element. |
| 717 | +
|
| 718 | + - In the event listener, you can interact with the event object to describe the drag operation, such as copying or moving. |
| 719 | + - You can also append data to the event to make sure that the drag and drop operation works together. |
| 720 | +
|
| 721 | +- To mark the areas where an item can be dropped, add an event listener to the element where the other element can be dropped: |
| 722 | + - Add a listener to the [`dragenter`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragenter_event) and [`dragover`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragover_event) events. |
| 723 | + - Call [`preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) in the event listeners to allow a drop operation. |
| 724 | + - Optionally listen to the [`dragleave`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragleave_event) event if you want to update the UI. |
| 725 | +
|
| 726 | +- Listen to the [`drop`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/drop_event) event on the same element where you listened to [`dragenter`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragenter_event) and [`dragover`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragover_event). |
| 727 | + - The drop event is only triggered if you prevented the default in dragenter and dragover, and the item is then dropped onto that same element. |
| 728 | + - In the [`drop`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/drop_event) event, you can do whatever you want to do upon a drop. |
| 729 | +
|
| 730 | +- Optionally, listen to the [`dragend`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragend_event) event on the dragged element itself, where you could update the UI or some data. |
| 731 | + - The [`dragend`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragend_event) event is always fired even if the drop is cancelled. |
| 732 | + - You will get some property on the event object which tells you whether the drop was successful or not. |
| 733 | +
|
| 734 | +You can check whether a [`drop`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/drop_event) operation succeeded or not by listening to the `drop` event and checking the `dataTransfer.dropEffect` property of the event object. |
| 735 | +
|
| 736 | +When the drop event fires, the `dataTransfer.dropEffect` property will contain one of the following string values: |
| 737 | +
|
| 738 | +- "none" if the drop operation was not allowed on the drop target |
| 739 | +- "copy" if the dragged item was copied to the drop target |
| 740 | +- "move" if the dragged item was moved to the drop target |
| 741 | +- "link" if the dragged item was linked to the drop target |
| 742 | +
|
| 743 | +You can use this value to determine whether the drop operation succeeded or not and take the appropriate action in your code. |
| 744 | +
|
| 745 | +Some Useful Attributes and Methods: |
| 746 | +
|
| 747 | +- [event.dataTransfer](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer) |
| 748 | +
|
| 749 | +- [DataTransfer: setData() method](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/setData) |
| 750 | +
|
| 751 | +- [DataTransfer: effectAllowed property](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/effectAllowed) |
| 752 | +
|
| 753 | +- [DataTransfer: getData() method](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/getData) |
| 754 | +
|
| 755 | +Readings: |
| 756 | +
|
| 757 | +- [Drag-And-Drop Events In JavaScript](https://blog.openreplay.com/drag-and-drop-events-in-javascript/) |
| 758 | +
|
| 759 | +- [JavaScript Drag and Drop](https://www.javascripttutorial.net/web-apis/javascript-drag-and-drop/) |
| 760 | +
|
| 761 | +- [Recommended Drag Types](https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Recommended_drag_types) |
0 commit comments