-
Notifications
You must be signed in to change notification settings - Fork 19
/
pl_orientation.js
159 lines (153 loc) · 5.51 KB
/
pl_orientation.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
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
153
154
155
156
157
158
159
/**
* pl library : orientation detection
* 横竖屏检测库
*/
;
(function(win, pl) {
var meta = {},
cbs = [],
timer;
var eventType = 'orientationchange';
// 是否支持orientationchange事件
var isOrientation = ('orientation' in window && 'onorientationchange' in window);
meta.isOrientation = isOrientation;
// font-family
var html = document.documentElement,
hstyle = win.getComputedStyle(html, null),
ffstr = hstyle['font-family'];
meta.font = ffstr;
// 订阅与发布
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
}
}();
// automatically load css script
function loadStyleString(css) {
var _style = document.createElement('style'),
_head = document.head ? document.head : document.getElementsByTagName('head')[0];
_style.type = 'text/css';
try {
_style.appendChild(document.createTextNode(css));
} catch (ex) {
// lower IE support, if you want to know more about this to see http://www.quirksmode.org/dom/w3c_css.html
_style.styleSheet.cssText = css;
}
_head.appendChild(_style);
return _style;
}
// callback
var orientationCB = function(e) {
if (win.orientation === 180 || win.orientation === 0) {
meta.init = 'portrait';
meta.current = 'portrait';
}
if (win.orientation === 90 || win.orientation === -90) {
meta.init = 'landscape';
meta.current = 'landscape';
}
return function() {
if (win.orientation === 180 || win.orientation === 0) {
meta.current = 'portrait';
}
if (win.orientation === 90 || win.orientation === -90) {
meta.current = 'landscape';
}
event.trigger(eventType, meta);
}
};
var resizeCB = function() {
var pstr = "portrait, " + ffstr,
lstr = "landscape, " + ffstr,
cssstr = '@media (orientation: portrait) { .orientation{font-family:' + pstr + ';} } @media (orientation: landscape) { .orientation{font-family:' + lstr + ';}}';
// 载入样式
loadStyleString(cssstr);
// 添加类
html.className = 'orientation' + html.className;
if (hstyle['font-family'] === pstr) { //初始化判断
meta.init = 'portrait';
meta.current = 'portrait';
} else {
meta.init = 'landscape';
meta.current = 'landscape';
}
resizeCB = function() {
if (hstyle['font-family'] === pstr) {
if (meta.current !== 'portrait') {
meta.current = 'portrait';
event.trigger(eventType, meta);
}
} else {
if (meta.current !== 'landscape') {
meta.current = 'landscape';
event.trigger(eventType, meta);
}
}
}
};
var callback = isOrientation ? orientationCB() : (function() {
resizeCB();
return function() {
timer && win.clearTimeout(timer);
timer = win.setTimeout(resizeCB, 300);
}
})();
// 监听
win.addEventListener(isOrientation ? eventType : 'resize', callback, false);
event.listen(eventType, 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 of pl.on must be a function.');
}
}
});
// 接口
pl.neworientation = meta;
pl.event = event;
pl.on = event.listen;
pl.add = function(cb) {
cbs.push(cb);
}
})(window, window['pl'] || (window['pl'] = {}));