-
Notifications
You must be signed in to change notification settings - Fork 19
/
pl_2.js
99 lines (96 loc) · 2.4 KB
/
pl_2.js
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
;(function(win, pl) {
var meta = {},
cbs = [],
element,
timer,
cstyle;
// 订阅与发布
var event = function(){
var fnlist = {},
listen,
trigger,
remove;
listen = function(e, fn){
if(!fnlist[e]){
fnlist[e] = [];
}
fnlist[e].push(fn);
};
trigger = function(e){
var key = [].shift.call(arguments),
fns = fnlist[key];
if(!fns || fns.length === 0){
return false;
}
for(var i = 0, fn; fn = fns[i++];){
fn.apply(this, arguments);
}
};
remove = function(e, fn){
var fns = fnlist[e];
if(!fns){
return false;
}
if(!fn){
fns && (fns.length = 0);
} else {
for(var j = 0, l = fns.length; j < l; j--){
if(fn === fns[j]){
fns.splice(j, 1);
}
}
}
}
return{
listen: listen,
trigger: trigger,
remove: remove
}
}();
// callback
var resizeCB = function(){
if(win.innerWidth > win.innerHeight){//初始化判断
meta.init = 'landscape';
meta.current = 'landscape';
} else {
meta.init = 'portrait';
meta.current = 'portrait';
}
return function(){
if(win.innerWidth > win.innerHeight){
if(meta.current !== 'landscape'){
meta.current = 'landscape';
event.trigger('__orientationChange__', meta);
}
} else {
if(meta.current !== 'portrait'){
meta.current = 'portrait';
event.trigger('__orientationChange__', meta);
}
}
}
}();
// 监听
win.addEventListener('resize', function(){
timer && win.clearTimeout(timer);
timer = win.setTimeout(resizeCB, 300);
}, false);
event.listen('__orientationChange__', function(event){
if(cbs.length === 0){
return false;
}
for(var i = 0, cb; cb = cbs[i++];){
if(typeof cb === 'function'){
cb.call(pl, event);
} else {
throw new Error('The accepted argument must be a function.');
}
}
});
// 接口
pl.orientation = meta;
pl.event = event;
pl.on = function(cb){
cbs.push(cb);
}
})(window, window['pl'] || (window['pl'] = {}));