Make no mistake, this article is not about how to mop the floor. Friends who have read "javascriptEssence" should know that the process of implementing drag and drop is relatively complicated. Times are different now. We can use H5's new drag and dropAPI It is very convenient to realize the drag and drop effect. Recently, I met a fellow gardener in the garden who wrote an article "HTML5 Advanced Series: Drag-and-Drop API to Realize Drag-and-Drop Sorting". It is really the work of a master. Mr. Xiong, as a newbie (not a master), I can't compare with it, so I launched the basic chapter. I hope all garden friends will gain something from it.
1. A simple example--pick up a few pebbles on the ground <p></p>地上有石子,捡几个吧 我是篮子 我是地 石子 石子 石子 石子 石子 石子 石子 石子 石子 石子
properties and methods. What are the functions of those methods? What do those attributes mean? Come one by one below.
2. General steps to implement drag and drop1. Find a draggable element
Just as not everyone is called Big Bear, and Not all elements can be dragged. The img and a elements are draggable by default, and other elements are not draggable by default. At that time, you can add draggable=true to make it draggable. <p></p> <p></p><p></p>
2. Handle drag-and-drop related events
All related events are as follows: (This is taken from: http://www.cnblogs.com/linxin/p/6794542.html) <p></p> SourceObject :
- dragstart: The source object starts dragging and dropping. <p></p>
- drag: During the drag and drop process of the source object. <p></p>
- drag
- dragenter: The source object begins to enter the scope of the process object. <p></p>
- dragover: The source object moves within the scope of the process object. <p></p>
- dragleave: The source object leaves the scope of the procedure object. <p></p>
- drop: The source object is dragged and dropped into the target object. <p></p>
nbsp;html>testEvents <p></p> <p></p> <p></p> <script> window.onload=function(){ var source = document.getElementById("source"); var process = document.getElementById("process"); var dest = document.getElementById("dest"); var sourceEle; source.addEventListener("dragstart",function(e){ console.log("source dragstart"); console.log(e); sourceEle = e.target; var dt = e.dataTransfer; dt.effectedAllowed = "all"; },false); process.addEventListener("dragenter",function(e){ console.log("process dragenter"); console.log(e); },false); process.addEventListener("dragover",function(e){ console.log("process dragover"); console.log(e); },false); process.addEventListener("dragleave",function(e){ console.log("process dragleave"); console.log(e); },false); source.addEventListener("drag",function(e){ console.log("source drag"); console.log(e); },false); dest.addEventListener("dragend",function(e){ console.log("dest dragend"); console.log(e); e.preventDefault(); },false); dest.addEventListener("drop",function(e){ console.log("dest drop"); console.log(e); dest.appendChild(sourceEle); e.preventDefault(); e.stopPropagation(); },false); document.ondragover = function(e){e.preventDefault();} document.ondrop = function(e){e.preventDefault();} } </script>
About effectAllowed and dropEffect, here the former is set to effectAllowed and the latter is selected using the drop-down list so that different mouse styles can be seen.
<p></p>nbsp;html>testEvents <p></p> <p></p> <p></p> <script> window.onload=function(){ var source = document.getElementById("source"); var process = document.getElementById("process"); var dest = document.getElementById("dest"); var dpe = document.getElementById("dpe"); var dpev; dpe.onchange = function(){ dpev = this.value; } var sourceEle; source.addEventListener("dragstart",function(e){ console.log("source dragstart"); console.log(e); sourceEle = e.target; var dt = e.dataTransfer; dt.effectedAllowed = "all"; },false); dest.addEventListener("dragend",function(e){ console.log("dest dragend"); console.log(e); e.preventDefault(); },false); dest.addEventListener("drop",function(e){ console.log("dest drop"); console.log(e); dest.appendChild(sourceEle); e.preventDefault(); e.stopPropagation(); },false); document.ondragover = function(e){ e.dataTransfer.dropEffect = dpev; e.preventDefault(); } document.ondrop = function(e){e.preventDefault();} } </script>
About the setData() and getData() methods
These two are methods related to data exchange. The former passes two parameters, and the first parameter is a mime type.String, the second one is data; the latter passes a parameter, which is mime type. Available mime types are text/plain, text/html, text/xml, text/uri-list.
Test case, drag the menu item onto the notebook. <p></p> <p></p>nbsp;html> <meta> <title>点菜</title> <style> .menu{ width: 200px; height: 300px; border: 1px solid red; margin-right: 10px; float: left; } .record{ width: 200px; height: 300px; border: 1px solid black; margin-right: 10px; float: left; } </style>
- 糖醋排骨
- 青椒肉丝
- 武昌鱼
- 手撕包材
- 千叶豆腐
关于setDragImage(Element img,long x,long y)
这个方法是设置拖放时的图标的,第一个参数表是图标元素,第二个表示相对与光标的水平偏移,第三个是垂直的。
还是前面的例子,在dragstart事件添加下面的代码,拖动时你会发现一只很大的手(不要被吓到);
var img = document.createElement("img"); img.src = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1493802056263&di=232de2c30491e19f32833669ad5a67ae&imgtype=0&src=http%3A%2F%2Fstatic.freepik.com%2Ffree-photo%2Fone-finger_318-10333.jpg"; dt.setDragImage(img,10,10);
四、关于拖放数据传递
上面的例子已经谈到了拖放的数据传递方法,这里在总结一下。
1、通过dataTransfer的setData()和getData()方法传递
2、通过闭包的方法,请参考开篇的例子。
五、总结
HTML5的拖放api非常简洁实用,为我们省去了很多麻烦,如果没有它,我们可能需要通过mousedownmousemove等等事件才能实现上述功能。本文较为详细的介绍了相关api,希望对你有所帮助。关于拖放api的应用大家可以参看下面链接的文章,他做了一个拖放排序,这是一个比较常见的应用场景。
大~熊同学的粉丝数正在逼近三位数,感谢各位园友的支持,大~熊会继续努力的!
参考:
http://www.cnblogs.com/ijjyo/p/4316232.html
http://www.cnblogs.com/linxin/p/6794542.html
The above is the detailed content of H5 drag and drop API basics. For more information, please follow other related articles on the PHP Chinese website!

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software