-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediaQueryEvents.jquery.js
More file actions
68 lines (53 loc) · 1.67 KB
/
mediaQueryEvents.jquery.js
File metadata and controls
68 lines (53 loc) · 1.67 KB
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
/* ====================================================
* jQuery Media Query Events.
* https://github.com/frontid/mediaQueryEvents
* Marcelo Iván Tosco (capynet)
* ==================================================== */
!function ($) {
'use strict';
let singleton;
const MediaQueryEventsClass = function ($el, options) {
singleton = this;
this.$el = $el;
this.options = $.extend({}, options);
this.map();
};
MediaQueryEventsClass.prototype = {
/**
* Map each defined breakpoint with a listener.
*/
map: function () {
const self = this;
for (let brkNameSpace in self.options.breakpoints) {
// Create the matchMedia and init.
const mql = window.matchMedia(self.options.breakpoints[brkNameSpace]);
const listener = function (e) {
if (e.matches) {
self._fire(brkNameSpace);
}
};
mql.addListener(listener);
// Since the listener will not get triggered
// on init we make an initial check.
if (mql.matches) {
// Wait document gets ready and all handlers are
// attached to the elements.
$(document).ready(function () {
self._fire(brkNameSpace);
});
}
}
},
_fire: function (brkNameSpace) {
const self = this;
self.$el.trigger('mq.' + brkNameSpace);
},
};
// Jquery plugin definition.
//-----------------------------------------------------------
$.fn.mediaQueryEvents = function (options) {
const $el = $(this);
const instance = new MediaQueryEventsClass($el, options);
$el.data('MediaQueryEventsClass', instance);
}
}(window.jQuery);