Skip to content

Commit 0c65c6a

Browse files
authored
Create 控制div移动_拖拽.html
1 parent e799b34 commit 0c65c6a

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

控制div移动_拖拽.html

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
<style>
7+
*{
8+
margin: 0;
9+
padding: 0;
10+
border: none;
11+
text-decoration: none;
12+
list-style: none;
13+
font-family: "微软雅黑";
14+
}
15+
body{
16+
background: #41EECB;
17+
18+
}
19+
.warp{
20+
position: relative;/*如果要设置 全屏 必须 加上相对定位*/
21+
width: 100%;
22+
border-radius: 10px;
23+
}
24+
.div_1,
25+
.div_2{
26+
position: absolute;
27+
top: 0px;
28+
left:0px;
29+
30+
width: 100px;
31+
height: 100px;
32+
background: #FCD78E;
33+
border-radius: 5px;
34+
cursor:pointer;
35+
}
36+
.div_2{
37+
background: #FC5185;
38+
}
39+
</style>
40+
</head>
41+
<body>
42+
<div class="warp">
43+
<div class="div_1"></div>
44+
<div class="div_2"></div>
45+
</div>
46+
</body>
47+
48+
<script type="text/javascript">
49+
window.onload = function()
50+
{
51+
var oDiv = document.querySelector(".div_1");
52+
var oDiv2 = document.querySelector(".div_2");
53+
var left = false;
54+
var top = false;
55+
var right = false;
56+
var bottom = false;
57+
var speed = 3;
58+
59+
setInterval(oMove,30);
60+
function oMove()
61+
{
62+
if(right)
63+
{
64+
oDiv.style.left = oDiv.offsetLeft + speed +"px";
65+
console.log(1);
66+
}
67+
if(left)
68+
{
69+
oDiv.style.left = oDiv.offsetLeft - speed +"px";
70+
console.log(2);
71+
}
72+
if(top)
73+
{
74+
oDiv.style.top = oDiv.offsetTop - speed +"px";
75+
console.log(3);
76+
}
77+
if(bottom)
78+
{
79+
oDiv.style.top = oDiv.offsetTop + speed +"px";
80+
console.log(4);
81+
}
82+
}
83+
84+
/*
85+
*按键检测 用docment
86+
*ev = ev||event;
87+
*/
88+
document.onkeydown = function(ev)
89+
{
90+
var ev = ev || event;
91+
var evCode = ev.keyCode; //37 38 39 40
92+
//switch 语句
93+
switch(evCode)
94+
{
95+
case 37: left=true; break;
96+
case 38: top=true; break;
97+
case 39: right=true; break;
98+
case 40: bottom=true; break;
99+
}
100+
}
101+
document.onkeyup = function(ev)
102+
{
103+
var ev = ev || event;
104+
var evCode = ev.keyCode;
105+
switch(evCode)
106+
{
107+
case 37: left=false;break;
108+
case 38: top = false;break;
109+
case 39: right = false;break;
110+
case 40: bottom =false;break;
111+
}
112+
}
113+
run(oDiv);
114+
run(oDiv2);
115+
/*谷歌里面的 自我保护 出现 禁止现象*/
116+
function run(ball)
117+
{
118+
ball.onmousedown = function(ev)
119+
{
120+
var _this = this;
121+
var ev =ev||event;
122+
var x =ev.clientX -this.offsetLeft;
123+
var y =ev.clientY -this.offsetTop ;
124+
window.onmousemove = function(ev){
125+
ball.style.left =ev.clientX -x + "px";
126+
ball.style.top =ev.clientY -y + "px";
127+
}
128+
window.onmouseup = function(ev)
129+
{
130+
window.onmousemove=null;
131+
}
132+
}
133+
}
134+
}
135+
</script>
136+
137+
</html>

0 commit comments

Comments
 (0)