-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path发布订阅.html
152 lines (130 loc) · 4.36 KB
/
发布订阅.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE HTML>
<html>
<head>
<title>index</title>
<meta charset="utf-8">
<style type='text/css'>
* { margin: 0; padding: 0; }
.elem {
position: absolute;
transition: 1s;
}
#stone { top: 300px; left: 100px; width: 100px; height: 100px; background-color: #336688; }
#trees { top: 100px; left: 500px; width: 20px; height: 300px; background-color: #41ad57; }
#cloud { top: 50px; left: 200px; width: 40px; height: 30px; background-color: #bdb9b9; border-radius: 30px 20px 10px 10px/20px 30px 10px 10px; }
#touch { top: 400px; left: 400px; width: 80px; height: 40px; background-color: #e13f34; text-align: center; line-height: 40px; color: #ffffff; border-radius: 30px 30px 20px 20px; box-shadow: 0 1px 2px 3px #95292c; cursor: pointer; }
</style>
</head>
<body>
<div id="stone" class="elem"></div>
<div id="trees" class="elem"></div>
<div id="cloud" class="elem"></div>
<div id="touch" class="elem">点我</div>
<script>
/*
element = {
ele:"#stone",
behavior:
}
clientlist{
DOM:[dom1,dom2,dom3],
fn:[fn1,fn2,fn3]
}
*/
var oTouch = document.querySelector('#touch');
var Scene = {
clientlist: {
'DOM': [],
'callBack': []
}, //缓存列表
addlisten: function (ele, fn) {
var index = 0;
for (var key in this.clientlist) {
this.clientlist[key].push(arguments[index]);
index++;
}
},
trigger: function () {
for (var key in this.clientlist) {
if (this.clientlist[key].length === 0) {
return false;
}
this.clientlist['DOM'].forEach((item, idx) => {
this.clientlist['callBack'][idx].apply(document.querySelector(item), arguments);
})
}
}
}
Scene.addlisten('#stone', function () {
this.style.left = this.offsetLeft + 20 + 'px';
this.style.top = this.offsetTop + 50 + 'px';
});
Scene.addlisten('#trees', function () {
this.style.left = this.offsetLeft - 10 + 'px';
this.style.top = this.offsetTop + 10 + 'px';
this.style.backgroundColor = '#' + (~~(Math.random() * (1 << 24))).toString(16);
});
Scene.addlisten('#cloud', function () {
this.style.left = this.offsetLeft + 20 + 'px';
this.style.top = this.offsetTop + 10 + 'px';
this.style.backgroundColor = '#' + (~~(Math.random() * (1 << 24))).toString(16);
});
oTouch.addEventListener('click', function () {
Scene.trigger();
}, false)
/*
var Scene = {
/!*
* 增加订阅者
* @ ele{string}类型 #stone
* @ fn{function} 回调函数
* clientlist:[
* {
* DOM:'#stone',
* fn:function(){....}
* },
* {
* DOM:'#trees',
* fn:function(){....}
* }
* ]
*
* *!/
clientlist: [], //缓存列表
addlisten: function (DOM, fn) {
fn = fn || this.defaultFn.bind(DOM);
var result = this.selectDom.apply(this, arguments);
if (result !== false) {
console.log('名称为' + DOM + '的条目已经存在,序号为:' + result);
return false;
}
this.clientlist.push({
DOM: DOM,
callBack: fn
});
},
selectDom: function (DOM, fn) {
if (!this.clientlist || this.clientlist.length == 0) return false;
for (var i = 0, len = this.clientlist.length; i < len; i++) {
var item = this.clientlist[i];
if (item['DOM'] === DOM) {
item['callBack'] = fn;
return i;
} else {
return false;
}
}
},
defaultFn: function () {
console.log(this.substr(1) + ' 迷茫的望向四周,不知所措');
},
trigger: function () {
this.clientlist.forEach(function (item, idx, arr) {
item['callBack'].apply(document.querySelector(item['DOM']), arguments);
})
}
}
*/
</script>
</body>
</html>