forked from FrontendMatter/breakpoints
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (62 loc) · 2.29 KB
/
Copy pathindex.js
File metadata and controls
73 lines (62 loc) · 2.29 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
69
70
71
72
73
class Breakpoints {
constructor (target, options = {}) {
this.target = target instanceof jQuery === true ? target : $(target)
this.lastSize = 0
this.options = $.extend({
distinct: true,
breakpoints: [320, 480, 768, 1024],
interval: 250
}, options)
this.interval = setInterval(() => {
let w = this.target.width()
let done = false
this.options.breakpoints.sort((a, b) => (b - a)).forEach((breakpoint, index) => {
// fire onEnter when a browser expands into a new breakpoint
// if in distinct mode, remove all other breakpoints first.
if (!done && w >= breakpoint && this.lastSize < breakpoint) {
if (this.options.distinct) {
for (var x in this.options.breakpoints.sort((a, b) => (b - a))) {
if (this.target.hasClass('breakpoint-' + this.options.breakpoints[x])) {
this.target.removeClass('breakpoint-' + this.options.breakpoints[x])
this.target.trigger('exitBreakpoint' + this.options.breakpoints[x])
}
}
done = true
}
this.target.addClass('breakpoint-' + breakpoint)
this.target.trigger('enterBreakpoint' + breakpoint)
}
// fire onExit when browser contracts out of a larger breakpoint
if (w < breakpoint && this.lastSize >= breakpoint) {
this.target.removeClass('breakpoint-' + breakpoint)
this.target.trigger('exitBreakpoint' + breakpoint)
}
// if in distinct mode, fire onEnter when browser contracts into a smaller breakpoint
if (
this.options.distinct && // only one breakpoint at a time
w >= breakpoint && // and we are in this one
w < this.options.breakpoints[index - 1] && // and smaller than the bigger one
this.lastSize > w && // and we contracted
this.lastSize > 0 && // and this is not the first time
!this.target.hasClass('breakpoint-' + breakpoint) // and we aren't already in this breakpoint
) {
this.target.addClass('breakpoint-' + breakpoint)
this.target.trigger('enterBreakpoint' + breakpoint)
}
})
// set up for next call
if (this.lastSize !== w) {
this.lastSize = w
}
}, this.options.interval)
this.target.data('breakpoints', this)
}
destroy () {
clearInterval(this.interval)
this.lastSize = 0
}
}
// EXPORT ES6
export default Breakpoints
// EXPORT ES5
module.exports = exports.default