-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (49 loc) · 1.65 KB
/
index.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
export default class Overlapt {
constructor(el) {
this.el = el
this.update()
window.addEventListener('resize', () => { this.update() })
document.querySelector('img').addEventListener('load', () => { this.update() })
}
update () {
if (!this.el) { return }
var section = this.el.dataset.overlapClosest ?
this.el.closest(this.el.dataset.overlapClosest) : this.el.parentNode
var neighbor = section.nextElementSibling
var amount = this.el.dataset.overlap ? this.el.dataset.overlap : '50'
if (this.el.dataset.overlapBreakpoints) {
try {
var breakpoints = {}
this.el.dataset.overlapBreakpoints
.split(';')
.forEach(function(values) {
var tmp = values.split(':')
breakpoints[tmp[0]] = tmp[1]
})
Object.keys(breakpoints)
.reverse()
.forEach(function(bp) {
if (window.innerWidth < parseInt(bp)) {
amount = breakpoints[bp]
}
})
} catch (e) {
console.error(e)
}
}
var percentage = !amount.includes('px')
var extraOffset = parseInt(window.getComputedStyle(section).paddingBottom)
amount = parseInt(amount)
if (percentage) {
amount = this.el.clientHeight * (amount / 100)
}
// Set neighbor CSS spacing
neighbor.style.paddingTop = ''
this.el.style.marginBottom = ''
var neighborTopPadding = parseInt(window.getComputedStyle(neighbor).paddingBottom)
if (amount > 0) {
neighbor.style.paddingTop = (neighborTopPadding + amount) + 'px'
this.el.style.marginBottom = (-1 * (amount + extraOffset)) + 'px'
}
}
}