.........net by ddr888
Email: erickdu888#gmail.com
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 90 文章- 1 评论- 251
类似google拖拽效果的原理实现(来自codeproject)
页面内容很简单 有三个div 一个用来拖拽的id是“a”,另外三个都是用来放置“a”的;
实现原理不难:
窗口打开后调用MakeElementDraggable函数。
MakeElementDraggable函数功能如下:
把a的鼠标按下事件的响应函数指向 InitiateDrag,它将重新定位拖拽窗口为鼠标所在位置,然后鼠标移动事件用drag函数响应,鼠标放开事件用drop函数响应,这两个函数自己看吧,就是实现动态拖拽和在指定区域放置id为a的窗口。
代码下载。
页面代码
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
title
>
Drag and Drop Demo
</
title
>
<
style
type
="text/css"
>
.dragElement
{
}
{
background-color
:
Green
;
position
:
absolute
;
z-index
:
5000
;
display
:
block
;
padding-top
:
30px
;
}
.dropZone
{
}
{
background-color
:
Blue
;
width
:
300px
;
height
:
300px
;
position
:
absolute
;
top
:
100px
;
right
:
200px
;
}
.highlightDropZone
{
}
{
background-color
:
Yellow
;
}
.DefaultDropZoneColor
{
}
{
background-color
:
Blue
;
}
</
style
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
div
id
="a"
class
="dragElement"
>
拖拽窗口
</
div
>
<
div
id
="dZone"
style
="position:absolute; top:100px; right:200; width:300px; height:300px"
class
="DefaultDropZoneColor"
>
放开区域 1
</
div
>
<
div
id
="dZone2"
class
="DefaultDropZoneColor"
style
="position:absolute; top:500px; right:200px; width:300px; height:300px"
>
放开区域 2
</
div
>
<
div
id
="dZone3"
class
="DefaultDropZoneColor"
style
="position:absolute; top:300;right:100px; width:100px; height:200px"
>
放开区域 3
</
div
>
</
div
>
</
form
>
</
body
>
</
html
>
<
script
language
="javascript"
type
="text/javascript"
>
var
dropZoneArray
=
new
Array(
5
);
dropZoneArray[
0
]
=
"
dZone
"
;
dropZoneArray[
1
]
=
"
dZone2
"
;
dropZoneArray[
2
]
=
"
dZone3
"
;
var
mouseState
=
'up';
function
MakeElementDraggable(obj)
{
var
startX
=
0
;
var
startY
=
0
;
function
InitiateDrag(e)
{
var
evt
=
e
||
window.event;
startX
=
parseInt(evt.clientX);
startY
=
parseInt(evt.clientY);
obj.style.top
=
parseInt(startY)
+
'px';
obj.style.left
=
parseInt(startX)
+
'px';
document.onmousemove
=
Drag;
document.onmouseup
=
Drop;
return
false
;
}
function
Drop(e)
{
var
evt
=
e
||
window.event;
//
check that if we are in the drop zone
if
(IsInDropZone(evt))
{
document.onmouseup
=
null
;
document.onmousemove
=
null
;
}
}
function
Drag(e)
{
//
only drag when the mouse is down
var
dropZoneObject;
var
evt
=
e
||
window.event;
obj.style.top
=
evt.clientY
+
'px';
obj.style.left
=
evt.clientX
+
'px';
//
Check if we are in the drop Zone
if
(IsInDropZone(evt))
{
dropZoneObject
=
evt.srcElement;
dropZoneObject.className
=
'highlightDropZone';
}
else
{
ResetColor();
}
}
a.onmousedown
=
InitiateDrag;
}
function
ResetColor()
{
document.getElementById(
"
dZone
"
).className
=
'DefaultDropZoneColor';
document.getElementById(
"
dZone2
"
).className
=
'DefaultDropZoneColor';
document.getElementById(
"
dZone3
"
).className
=
'DefaultDropZoneColor';
}
function
IsInDropZone(evt)
{
var
result
=
false
;
var
obj
=
evt.srcElement;
//
iterate through the array and find it the id exists
for
(i
=
0
; i
<
dropZoneArray.length; i
++
)
{
if
(obj.id
==
dropZoneArray[i])
{
result
=
true
;
break
;
}
}
return
result;
}
//
make the element draggable
window.onload
=
MakeElementDraggable(document.getElementById(
"
a
"
));
</
script
>
posted @ 2007-03-14 15:19
ddr888
阅读(946)
评论(6)
编辑
收藏
所属分类:
javascript代码
发表评论
回复
引用
查看
#1楼
2007-03-14 17:08 |
kilxy
把代码复制到 .html里面怎么不好用呢?
回复
引用
查看
#2楼
[
楼主
]2007-03-14 17:12 |
ddr888
@kilxy
这是我整理过的一个页面的整体代码,我这个html页面很正常啊。我传上去吧。
回复
引用
查看
#3楼
2007-03-14 18:08 |
∈鱼杆
我也是不好用。你的下载也不可以用。发EMAIL:pumaboyd@163.com
回复
引用
查看
#4楼
[
楼主
]2007-03-14 18:11 |
ddr888
不好意思 地址已经改。正常了 。
回复
引用
查看
#5楼
2007-11-14 22:00 |
Justin
不厚道啊!原文地址呢?
回复
引用
#6楼
2008-06-03 16:20 |
dmewy [未注册用户]
能否把source发给我一份啊...
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2007-03-14 18:11 编辑过
相关文章:
【整理】【最近更新日期:2005-10-15】【找工作必读!】来自IT公司速查手册的各大IT公司薪资和待遇内幕
实现土豆网的视频播放
来自CodeSmith的震撼
Ajax实现无刷新三联动下拉框
51ditu、清华地图以及Google地图
javascript实现类似google和msn space的拖拽
相关链接:
所属分类的其他文章:
类似google拖拽效果的原理实现(来自codeproject)
怎样用javascript操作ftb编辑区内容
图片滚动代码。
javascript常用脚本集锦
下拉列表控制文本框的个数思路
js提取url参数的几种方法。(搜集)
js实现弹出式菜单
js客户端日历控件
freetextbox常用ToolbarLayout按钮功能(3.1.6)
图片缩放js
最新IT新闻:
Google推出Android Market挑战App Store
美国年轻人最喜欢的15大网站
2008年8月30日IT博客精选
《极品飞车12》最新真人照片、游戏截图
IBM正在开发超强性能4TB固态硬盘阵列
博客园新闻频道
博客园首页
社区
公告
站长统计
我的最新闪存
我也闪亮一下下
8-28 08:25
<
2007年3月
>
日
一
二
三
四
五
六
25
26
27
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我管理的小组
灌水号
我参加的小组
创业交流
web标准设计
jQuery
我参与的团队
ASP.NET AJAX (Atlas)学习(0/1343)
昆明.NET俱乐部(0/115)
我的标签
subsonic(1)
随笔分类
(124)
AJAX(6)
(rss)
asp.net(37)
(rss)
css+xhtml(5)
(rss)
flash动画(3)
(rss)
html(6)
(rss)
javascript代码(14)
(rss)
扯淡淡(13)
(rss)
技术疑问(20)
(rss)
技术原创(3)
(rss)
数据库(7)
(rss)
项目日志(10)
(rss)
随笔档案
(90)
2008年5月 (1)
2008年4月 (1)
2008年3月 (1)
2007年11月 (1)
2007年10月 (1)
2007年9月 (2)
2007年8月 (1)
2007年7月 (3)
2007年6月 (3)
2007年5月 (2)
2007年3月 (5)
2007年2月 (4)
2007年1月 (13)
2006年12月 (22)
2006年11月 (13)
2006年10月 (17)
文章分类
(1)
项目日志(1)
(rss)
收藏夹
(2)
asp.net(2)
(rss)
asp.net
ddr888-asp.net
电子商务
破烂网
音乐娱乐
it产品
激光教鞭是什么?
积分与排名
积分 - 86245
排名 - 441
阅读排行榜
1. 双边可关闭对联广告!(5491)
2. 有人装vs2005的sp1补丁出问题的嚒?(5083)
3. 图片滚动代码。(4242)
4. 【META http-equiv="Content-Type" Content="text/html; Charset=*】意义详解(3275)
5. vs2005 sp1正式发行!(俗)(2536)
评论排行榜
1. 有人装vs2005的sp1补丁出问题的嚒?(25)
2. 送wallop 邀请 剩的不多,一天就5个(22)
3. 图片滚动代码。(17)
4. Asp.Net 学习资源列表(15)
5. gridview中实现客户端“删除确认”?(13)