-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullSizeBg.js
156 lines (108 loc) · 3.36 KB
/
FullSizeBg.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
import $ from 'jquery';
import { not, isNotDef, isBoolean, anyOf, notSingleEle, getSizeAspectFill } from './_util';
class FullSizeBg {
constructor(options) {
if (isNotDef(options)) throw new Error('FullSizeBg: require options object when create instance.');
const _ = this;
_._option = $.extend(
{
wrap: null, // wrap
imgWrap: null, // image wrap
imgWidth: 320, // natural image width
imgHeight: 240, // natural image height
alignX: 'center', // 'left' or 'center' or 'right'
alignY: 'center', // 'top' or 'center' or 'bottom'
global: window
},
options
);
_._global = _._option.global;
_._initialized = false;
_._uniqueId = Date.now();
_._$wrap = $(_._option.wrap);
_._$imgWrap = $(_._option.imgWrap);
_._$img = $('img', _._option.imgWrap);
_._proxy = {
resizeEventHandler: null
};
if (anyOf(notSingleEle(_._$img), notSingleEle(_._$imgWrap), notSingleEle(_._$img))) {
throw new Error('FullSizeBg: require options object has a single wrap, imgWrap, img.');
}
}
/*
* public methods
*/
init() {
const _ = this;
if (_._initialized) return _;
_._initialized = true;
_._proxy.resizeEventHandler = _.resize.bind(_);
_.setResizeEventHandler(true);
_.resize();
return _;
}
setResizeEventHandler(flag) {
if (not(isBoolean)(flag)) throw new TypeError('FullSizeBg: setResizeEventHandler require boolean parameter.');
const _ = this,
evtName = `resize.kihon.fullsizebg.${_._uniqueId}`;
$(_._global).off(evtName, _._proxy.resizeEventHandler);
if (flag) $(_._global).on(evtName, _._proxy.resizeEventHandler);
return _;
}
resize(evt = null) {
const _ = this,
size = _.getImageSizeAspectFill(_._option.imgWidth, _._option.imgHeight, _._global);
_._$img.css({ width: size.width, height: size.height });
_._setWrapAlign(_._option.alignX, _._option.alignY, size, _._global);
_._$wrap.css({ width: _._global.innerWidth, height: _._global.innerHeight });
return _;
}
getImageSizeAspectFill(srcWidth = 0, srcHeight = 0, global = window) {
return getSizeAspectFill(srcWidth, srcHeight, global.innerWidth, global.innerHeight);
}
destroy() {
const _ = this;
_._initialized = false;
_._$wrap = null;
_._$imgWrap = null;
_._$img = null;
_.setResizeEventHandler(false);
_._proxy.resizeEventHandler = null;
return _;
}
/*
* private methods
*/
_setWrapAlign(alignX = 'center', alignY = 'center', modifiedSize = { width: 0, height: 0 }, global = window) {
const _ = this,
winWidth = global.innerWidth,
winHeight = global.innerHeight;
let left = 0,
top = 0;
switch (alignX) {
case 'left':
left = 0;
break;
case 'center':
left = Math.round((winWidth - modifiedSize.width) / 2);
break;
case 'right':
left = Math.round(winWidth - modifiedSize.width);
break;
}
switch (alignY) {
case 'top':
top = 0;
break;
case 'center':
top = Math.round((winHeight - modifiedSize.height) / 2);
break;
case 'bottom':
top = Math.round(winHeight - modifiedSize.height);
break;
}
_._$imgWrap.css({ left: left, top: top });
return _;
}
}
export default FullSizeBg;