


A detailed introduction to APIs related to HTML5 element drag and drop (pictures and text)
In fact, HTML5 just adds some useful APIs
Let us develop more easily
So that we can focus more on business logic
The use of these APIs is also very simple
But I My memory is not very good
So I still record it in the form of a blog (manual funny)
Let’s write about this drag and drop API today
Default drag
Speaking of drag In fact, the earliest implementation of drag-and-drop function was IE (IE4)
H5 is the drag-and-drop specification specified based on the IE instance
In the browser, there is a default drag
For example, pictures Drag
Drag the selected text
It is also possible to drag other elements
Only on the element tag Add an attribute
1 |
|
One type is the event triggered by the dragged element
The other type is the event triggered by the drag and drop target element
1 |
|
1 |
|
dragstart
drag
dragend
The drag event will be continuously triggered during the dragging process (analogous to mousemove)
The dragend event will be triggered when the mouse is released to cancel the drag (analogous to mouseup)
1 2 3 4 5 6 7 |
|
dragenter
dragover
dragleave
drop
When the dragged element is in the target element, the dragover event will continue to be triggered.
Leave the target element and trigger the dragleave event (analogous to mouseout)
If the element is dragged and dropped into the target element (release the mouse in the target element), the drop event will be triggered but the dragleave event will not be triggered
1 2 3 4 5 6 7 8 9 |
|
At this time we will find that the drop event is not triggered when the element is dragged and dropped into the target element
We saw a special cursor (circle + backslash)
So the drop event is not triggered
That is to say, the element cannot be dragged and dropped by default
As long as we cancel the default event
in the dragover event of the
target element, the problem can be solved
1 2 3 |
|
Just a simple drag and drop is meaningless
We need Perform data exchangeThe object of this term data exchange is the attribute of the event object
dataTransfer
The two core methods of dataTransfer are setData() and getData() setData() is used Set data, use getData() to receive data
1 2 |
|
If we drag the selected textThe browser will call dataTransfer.setData by default to set the corresponding text data
setData() and getData() is a string of data type
H5 has extended it and can specify various MIME types
But for Backwards compatible, it also supports "text" and "URL"
They will be mapped to "text/plain" and "text/uri-list" respectively
If the data is saved as URL, the browser It will do special processing and treat it as a web link
1 2 3 4 5 6 7 8 9 10 |
|
Drag settings
There are two important properties in dataTransfer
dropEffect
andeffectAlloweddropEffect
The dropEffect attribute value is a string, indicating which placement behavior the dragged element can perform
To use this attribute, it must be set in the dragenter event handler function- none Elements cannot be dragged and dropped here (default value for all elements except text boxes)
- move Move to the target
- copy Copy to target
link The target opens the drag element (the drag element must be a link and have a URL)
effectAllowed
The effectAllowed attribute value is also a character String, indicating which dropEffect is allowed to drag elements
To use this attribute, it must be set in the dragst event handler function
uninitialized No drag-and-drop behavior is set
none cannot be caused by any behavior
copy Only dropEffect values are allowed to be copy
link Only dropEffect values are allowed to be link
move Only allows dropEffect value to be move
copyLink Allows dropEffect value to be copy and link
copyMove allows dropEffect values to be copy and move
linkMove allows dropEffect values to be link and move
all allows any dropEffect
The above is the detailed content of A detailed introduction to APIs related to HTML5 element drag and drop (pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
