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