zoukankan
html css js c++ java
类似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; 300px; height:300px"
class
="DefaultDropZoneColor"
>
放开区域 1
</
div
>
<
div
id
="dZone2"
class
="DefaultDropZoneColor"
style
="position:absolute; top:500px; right:200px; 300px; height:300px"
>
放开区域 2
</
div
>
<
div
id
="dZone3"
class
="DefaultDropZoneColor"
style
="position:absolute; top:300;right:100px; 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
>
查看全文
相关阅读:
『Power AI by AI』 PAI-AutoML2.0重磅发布
基于 K8s 做应用发布的工具那么多, 阿里为啥选择灰姑娘般的 Tekton ?
编码方法论,赋能你我他
开发部署效率提升 12 倍,这款应用托管服务让云上运维更简单
全景还原报错现场 | 应用实时监控 ARMS 上线用户行为回溯功能
告别诊断烦恼 | 应用实时监控 ARMS 上线智能和实时诊断功能
阿里云CDN上线 WAF,一站式提供分发+安全能力
RDS for PostgreSQL 云盘加密功能使用方法
MySQL8.0.17
2370 小机房的树
原文地址:https://www.cnblogs.com/ddr888/p/674630.html
最新文章
CodeForces960F:Pathwalks (主席树+DP)
CodeForces
【转】BZOJ4066(kdtree)(占位)
SPOJ:Red John is Back(DP)
CodeForces
Gym101161:ACM Tax (主席树)(占位)
CodeForces
【转】区间颜色众数 (主席树)
CodeForces813E:Army Creation (主席树---上一题的加强版)
gift 分数规划的最大权闭合子图
热门文章
poj 3111 K Best
细胞(cell) 矩阵快速幂
poj 3070 Fibonacci 矩阵快速幂
大佬的难题
【模板】矩阵快速幂
暗牧 (m)
bzoj 2342: [Shoi2011]双倍回文
【USACO 5.3.1】量取牛奶 迭代
【NOIP2013】华容道 广搜+spfa
实战经验 | Cassandra Java堆外内存排查经历全记录
Copyright © 2011-2022 走看看