-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex2.html
More file actions
97 lines (84 loc) · 2.07 KB
/
Copy pathindex2.html
File metadata and controls
97 lines (84 loc) · 2.07 KB
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
<!--
* @Author: junchao
* @Date: 2021-01-07 13:46:06
* @LastEditTime: 2021-01-07 13:46:59
* @LastEditors: junchao
* @Description:
* @FilePath: /JavaScriptDesignPatterns/command/index2.html
* @可以输入预定的版权声明、个性签名、空行等
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>撤销操作的实例</title>
<style>
.demo {
width: 100%;
height: 100px;
position: relative;
}
.target {
width: 50px;
height: 50px;
position: absolute;
bottom: 0;
background-color: red;
}
</style>
</head>
<body>
<div class="demo">
<button class="move">移动</button>
<button class="undo">撤销</button>
<div class="target" style="left:0"></div>
</div>
</body>
<script>
//移动对象
var Animate = function (dom) {
this.dom = dom;
var self = this;
//移动函数
this.move = function () {
var left = parseInt(self.dom.style.left);
self.dom.style.left = left + 10 + 'px';
};
//取消移动函数
this.undo = function () {
var left = parseInt(self.dom.style.left);
self.dom.style.left = left - 10 + 'px';
};
};
//命令对象
var Command = function (receiver) {
var self = this;
this.receiver = receiver;
this.count = 0; //记录执行命令的次数
//执行命令
this.execute = function () {
self.receiver.move();
self.count += 1;
};
//撤销命令
this.unexecute = function () {
if (self.count === 0) return;
self.receiver.undo();
self.count -= 1;
}
};
//设置命令函数
function addEvent(dom, fn, Capture) {
dom.addEventListener("click", fn, !!Capture);
};
//client调用
var dom = document.querySelector(".target");
var moveBtn = document.querySelector(".move");
var unmoveBtn = document.querySelector(".undo");
var m = new Animate(dom);
var c = new Command(m);
addEvent(moveBtn, c.execute);
addEvent(unmoveBtn, c.unexecute);
</script>
</html>