HTML5如何实现拖放'N'拖放
在网络开发人员需要掌握的所有技术中,看起来最容易造成混淆和潜在问题的是拖拽。

创新互联专注于望谟网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供望谟营销型网站建设,望谟网站制作、望谟网页设计、望谟网站官网定制、小程序制作服务,打造望谟网络公司原创品牌,更为您提供望谟网站排名全网营销落地服务。
这不是一项新技术,它已存在多年,但许多开发人员仍然执着于基于jQuery的旧方法,以牵连复杂(有时很慢且不准确)的拖放模拟。
现在HTML5包含一个拖放API,您已经有了一个非常简单的方法来实现无拖放拖放。
理解力学
拖放很容易理解。你有一个物体和一个目标。目标是允许用户点击对象并将其拖动到目标,然后您的应用程序需要做出适当的响应。
创建一个网页模板
这很简单,但我们需要有一个地方来放置所有的代码项目。您可以创建的最基本的网页模板是:
<!DOCTYPE html>
< html >
< head >
head >
< body >
body >
html >
定义一个可拖动的对象
你可能期望在这里有一些复杂的类似Java的定义,但实际上它就像在你的body部分的一个元素上设置一个“可拖动”属性一样简单,就像这样:
< img id = “dragMe” src = “pic.jpg” draggable = “true” >
div >
正如你所看到的那样,这部分非常简单。事实上,对于某些浏览器,您甚至不需要为某些元素类型指定属性,但无论如何,仍然是一个好主意。
定义目标
目标是可以将对象拖到的区域。同样,这很简单,你只需要给目标一个CSS ID属性。
×××离开!
div >
添加一些CSS
我们应该确保一切都看起来应该看起来。将以下CSS样式信息添加到您的头部部分:
<风格>
#drop_target { border:3 px solid#000 ; 填充:10像素; 身高:100像素; 宽度:100像素;}
#dragMe { height:50 px; width:50 px;}
< / style>
实现JS功能
这一步只是为了确保你的应用程序知道对象的存在,并且它应该与它们交互。如果您需要创建一系列事件侦听器,则可以采取复杂的方法,但您并不需要任何精心处理基本拖放操作的内容。
真的,我们只需要定义两个函数来处理所有的移动,dragObject和dropObject。
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-05-27 07:37:34 HTTP/1.1 GET : /article/ieciph.html
- 运行时间 : 2.5549s ( Load:0.0065s Init:1.8738s Exec:0.6654s Template:0.0092s )
- 吞吐率 : 0.39req/s
- 内存开销 : 2,223.91 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=greh7qd4l8qel1bf1s1g6n9s54
- /www/wwwroot/tsicrk.com/index.php ( 1.09 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Mode/common.php ( 2.82 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Common/functions.php ( 53.56 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /www/wwwroot/tsicrk.com/App/Common/Conf/config.php ( 2.14 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /www/wwwroot/tsicrk.com/App/Home/Conf/config.php ( 0.31 KB )
- /www/wwwroot/tsicrk.com/App/Home/Common/function.php ( 3.33 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /www/wwwroot/tsicrk.com/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 17.71 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000030s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000291s ]
- [ app_begin ] --END-- [ RunTime:0.000314s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000060s ]
- [ template_filter ] --END-- [ RunTime:0.000081s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.006319s ]
- [ view_parse ] --END-- [ RunTime:0.006347s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000157s ]
- [ view_filter ] --END-- [ RunTime:0.000172s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 122 行.

2.5549s
