-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_cover.js
284 lines (237 loc) · 8.15 KB
/
image_cover.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
(function($) {
/**
* [Image_cover description]
* @param {[Object|String]} id - It needs a target(id or ClassName)
* @param {[Object]} settings - CSS background settings
* @param {[String]} settings.position - background-position
* @param {[String]} settings.repeat - background-repeat
* @param {[String]} settings.size - background-size
* @param {[String]} settings.attachment - background-attachment
* @constructor
*/
function Image_cover(id, settings) {
// Default settings
this.id = id;
this.background = {};
this.background.position = 'center center';
this.background.repeat = 'no-repeat';
this.background.size = 'cover';
this.background.attachment = 'scroll';
/**
* CleanAllHandler
* Remove all binding event on this object
*
* @param {[String|Object]} selector - what selector do you bind event on it?
* @param {[String]} event - what event do you want to remove?
* @param {[String]} specific_id - This is a namespace which is called on binding event.
*/
this.CleanAllHandler = function(selector, event, specific_id) {
// get all prototype from 'this' object
var prototypes = Object.getPrototypeOf(this);
for (var prototype in prototypes) {
var event_namespace = event + '.' + prototype + specific_id;
$(selector).off(event_namespace);
}
};
// this.AddHandler = function (selector,event,prototype,specific_id) {
// var events = $(selector).data('events');
// var event_namespace = event + '.' + prototype + specific_id;
//
// $.each(events, function(key,value) {
// if(key === event) {
// var namespaces = value;
// $.each(namespaces , function(key,value) {
// if(value.namespace === event_namespace ) {
// return false;
// }
// });
// console.log('asdasd');
// }
// });
// };
// If it has custom settigns, then merging it.
$.extend(this.background, settings);
}
/**
* Cover
* Basic method that use original img height
* and settles all background properties.
*/
Image_cover.prototype.Cover = function() {
var settings = this.background;
var block = $(this.id);
var block_uuid = this.id;
var that = this;
var Cover_block_img = block.find('img');
this.specific_id = [];
Cover_block_img.each(function(index, element) {
var naturalHeight = this.naturalHeight;
that.specific_id[index] = block_uuid + index;
$(this).css('display', 'none').parent().not('.image-cover-processed').css({
'background-position': settings.position,
'background-repeat': settings.repeat,
'background-size': settings.size,
'background-attachment': settings.attachment,
'background-image': 'url(' + $(this).attr('src') + ')',
'display': 'block',
'height': naturalHeight
}).addClass('image-cover-processed');
});
};
/**
* SetHeight
*
* You can set custom height for your image.
*
* @param {[Object]} args - it is contains two parameters.
* @param {[number]} args.custom_Height - given number to apply on the image.
* @param {[number]} args.ratio - Whether custom_Height is greater than device height, it'll be used.
*/
Image_cover.prototype.SetHeight = function(args) {
var block = $(this.id);
var Cover_block_img = block.find('img');
var custom_Height = args.custom_Height;
var ratio = args.ratio;
var that = this;
that.Cover();
/**
* SetHeight_Responsive
*
* @param {[number]} a - given number to apply on the image.
* @param {[number]} b - Whether custom_Height is greater than device height, it'll be used.
* @param {[String|Object]} c - which image do you want to apply?
*/
that.SetHeight_Responsive = function(a, b, c) {
if (a > $(window).height()) {
var Height = a * b;
c.parent('.image-cover-processed').css({
'height': Height
});
} else {
c.parent('.image-cover-processed').css({
'height': a
});
}
};
Cover_block_img.each(function(index, element) {
var uuid = that.specific_id[index];
var img = $(this);
that.SetHeight_Responsive(custom_Height, ratio , img);
// Clean All handler
that.CleanAllHandler(window, 'resize', uuid);
$(window).on('resize.SetHeight' + uuid, function() {
that.SetHeight_Responsive(custom_Height, ratio , img);
});
});
};
/**
* DeviceHeight
*
* Using device height to set image height.
*
*/
Image_cover.prototype.DeviceHeight = function() {
var block = $(this.id);
var Cover_block_img = block.find('img');
var that = this;
that.Cover();
/**
* Device_Height_Responsive
*
* @param {[String|Object]} a - which image do you want to apply?
*/
that.Device_Height_Responsive = function(a) {
a.parent('.image-cover-processed').css({
'height': $(window).height(),
});
};
Cover_block_img.each(function(index, element) {
var uuid = that.specific_id[index];
var img = $(this);
that.Device_Height_Responsive(img);
// Clean All handler
that.CleanAllHandler(window, 'resize', uuid);
$(window).on('resize.DeviceHeight' + uuid, function() {
that.Device_Height_Responsive(img);
});
});
};
/**
* RemoveCover
*
* remove all background effect
*/
Image_cover.prototype.RemoveCover = function() {
var block = $(this.id);
var Cover_block_img = block.find('img');
var that = this;
that.Cover();
Cover_block_img.each(function(index, element) {
var uuid = that.specific_id[index];
$(this).css('display', 'block')
.parent('.image-cover-processed')
.removeAttr('style')
.removeClass('image-cover-processed');
// Clean All handler
that.CleanAllHandler(window, 'resize', uuid);
});
};
/**
* ElSameHeight
*
* You can let image equal height with specific element.
*
* @param {[Object]} args - it contains a parameter.
* @param {[String||Object]} args.El - A selector which let image is equal height with it can be a string or jQuery object.
*/
Image_cover.prototype.ElSameHeight = function(args) {
var block = $(this.id);
var Cover_block_img = block.find('img');
var El = args.El;
var that = this;
that.Cover();
/**
* ElSameHeight_Responsive
*
* @param {[String|Object]} a - which image do you want to apply?
* @param {[String|Object]} b - A selector which let image is equal height with it can be a string or jQuery object.
*/
that.ElSameHeight_Responsive = function(a, b) {
a.parent('.image-cover-processed').css({
'height': b.innerHeight(),
});
};
Cover_block_img.each(function(index, element) {
var target = $(El).eq(index) || $(El).eq(0);
var uuid = that.specific_id[index];
var img = $(this);
that.ElSameHeight_Responsive(img, target);
// Clean All handler
that.CleanAllHandler(window, 'resize', uuid);
$(window).on('resize.ElSameHeight' + uuid, function() {
that.ElSameHeight_Responsive(img, target);
});
});
};
// plugin defintion.
$.fn.ImageCover = function (bg_settings,method,args,fn) {
// If method is given wrong string, it'll set a default value.
if(typeof method !== 'string' || !Image_cover.prototype.hasOwnProperty(method)) {
method = 'Cover';
}
// Whether method has no argument, it'll let args had null.
// If args is not object or undefined, it'll throw a erroe message.
if(Image_cover.prototype[method].length === 0) {
args = null;
}else if(args === undefined || typeof args !=='object') {
throw 'Please give a correct args';
}
if(! this.length || ! this.get(0).classList) {
return this;
}
var selector = '.' + this[0].classList.value.replace(/\ /g,'.');
this.Image_cover = new Image_cover(selector,bg_settings);
this.Image_cover[method](args);
return $.isFunction(fn) ? this.each(fn) : this;
};
})(jQuery);