例子8:流氓浮动广告

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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title></title>
<style>
#adv {
width: 200px;
height: 200px;
color: yellow;
position: fixed;
right: 10px;
top: 10px;
background-color: blue;
}
#adv button {
float: right;
border: none;
outline: none;
color: white;
background-color: gray;
}
</style>
</head>
<body>
<div id="adv">
此广告位招租
<button>关闭</button>
</div>
<script>
+function() {
var advDiv = document.querySelector('#adv');
var button = document.querySelector('#adv button');
var counter = 0;
button.addEventListener('click', function() {
counter += 1;
if (counter < 3) {
var currentStyle =
document.defaultView.getComputedStyle(advDiv);
var newTop = parseInt(currentStyle.top) + 20;
var newRight = parseInt(currentStyle.right) + 20;
advDiv.style.top = newTop + 'px';
advDiv.style.right = newRight + 'px';
} else {
advDiv.style.display = 'none';
}
});
}();
// 鼠标按下 - mousedown
// 鼠标移动 - mousemove
// 鼠标松开 - mouseup
// clientX / clientY - 鼠标的横纵坐标
</script>
</body>
</html>