forked from FrontendMatter/fix-footer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (36 loc) · 1.33 KB
/
Copy pathindex.js
File metadata and controls
43 lines (36 loc) · 1.33 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
import './style'
import { handler } from 'dom-factory'
export const footerComponent = (element) => ({
element,
_reset: function () {
this.element.style.opacity = 1
var offsetTop = this.element.offsetTop
var offsetHeight = this.element.offsetHeight
var bottom = offsetTop + offsetHeight
var isFixed = window.getComputedStyle(this.element).position === 'fixed'
var isContentBelow = isFixed && offsetTop < document.documentElement.offsetHeight
this.element.classList[bottom <= window.innerHeight && !isContentBelow ? 'add' : 'remove']('fix-footer--fixed')
},
_debounceResize: function () {
clearTimeout(this._debounceResizeTimer)
this._debounceResizeTimer = setTimeout(function () {
if (this._resizeHeight !== window.innerHeight) {
this._resizeHeight = window.innerHeight
this._reset()
}
}.bind(this), 50)
},
init: function () {
this._debounceResize = this._debounceResize.bind(this)
window.addEventListener('load', function () {
this._reset()
this._resizeHeight = window.innerHeight
window.addEventListener('resize', this._debounceResize)
}.bind(this))
},
destroy: function () {
clearTimeout(this._debounceResizeTimer)
window.removeEventListener('resize', this._debounceResize)
}
})
handler.register('fix-footer', footerComponent)