-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.addwidthclass.js
58 lines (49 loc) · 1.97 KB
/
jquery.addwidthclass.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
/*!
* jQuery Add Width Class Plugin v1.0.0
* https://github.com/yura-x/addwidthclass
*
* Copyright 2015 Yuri X
* Released under the MIT license
*/
/*
* jQuery Plugin for adding CSS classes depending from element width
*/
(function ( $ ) {
$.fn.addWidthClass = function( options ) {
// Default options.
var settings = $.extend({
breakpoints: [200,400,600,1000,1200]
}, options );
//string which contains all of the possible CSS classes (needs for remove classes on windows resizing)
var availableClassesString = '';
for (var i = settings.breakpoints.length - 1; i >= 0; i--) {
availableClassesString += " " + "width-lt-" + settings.breakpoints[i] + " " + "width-gt-" + settings.breakpoints[i];
};
return this.each(function(){
var $this = jQuery(this);
var newClassesString = '';
for (var i = settings.breakpoints.length - 1; i >= 0; i--) {
if( $this.width() <= settings.breakpoints[i] ) {
newClassesString += ' width-lt-' + settings.breakpoints[i]
} else {
newClassesString += ' width-gt-' + settings.breakpoints[i]
}
};
$this.addClass(newClassesString);
//processing window resize event
jQuery(window).resize(function(){
//removing all classes
newClassesString = '';
$this.removeClass(availableClassesString);
for (var i = settings.breakpoints.length - 1; i >= 0; i--) {
if( $this.width() <= settings.breakpoints[i] ) {
newClassesString += ' width-lt-' + settings.breakpoints[i]
} else {
newClassesString += ' width-gt-' + settings.breakpoints[i]
}
};
$this.addClass(newClassesString);
});
});
};
}( jQuery ));