-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fixed-header.js
172 lines (162 loc) · 4.57 KB
/
jquery.fixed-header.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
;(function ($) {
/**
* Add fixed function to global scope to add the possibility
*
* Working example: *
* https://codepen.io/patrickkahl/pen/mRMBLo
*
* Example 1:
*
* // No options required
* $('#header').fixed();
*
* Example 2:
*
* // Overwriting default values
* $('#header').fixed({
* offset: 30, // Default is 50
* class: 'fixed-header', // Default is header--fixed
* objects: [
* {object: $('.logo'),class: 'logo--fixed'}
* ]
* });
*
* @ param {object} settings
* @ return {object} this
*/
$.fn.fixed = function (customer_opt) {
// Define objects and default values
var header = this,
w = $(window),
d = $(document),
document_height = d.height(),
header_height = header.outerHeight(),
scrollTop = 0,
lastScrollTop = 0,
// Define current header state for better performance
state = null,
// Default options
default_opt = {
toAnchor: 'scroll',
onLoadAnchor: 'jump',
onChangeAnchor: 'jump',
offset: 50,
class: 'header--fixed',
objects: {},
},
target = $(location.hash),
// Merge default and customer options
opt = $.extend({}, default_opt, customer_opt),
/**
* Toggle header and depended object classes
* with given action
* @param {string} action addClass || removeClass
*/
toggleClass = function (action) {
header[action](opt.class)
if (opt.objects.length > 0) {
$.each(opt.objects, function (i, e) {
if (e.object && e.class) {
$(e.object)[action](e.class)
}
})
}
var event = new CustomEvent('fixed-header-changed', {
detail: header[0],
})
document.dispatchEvent(event)
},
/**
* Scroll to given anchor target
* @param {string} anchor
*/
scrollToAnchor = function (target) {
$('html,body').animate(
{
scrollTop: target.offset().top - header_height,
},
500
)
},
/**
* Jump to given anchor target
* @param {string} anchor
*/
jumpToAnchor = function (target) {
$(document).scrollTop(target.offset().top - header_height)
}
// Do on window scroll and resize
w.on('resize scroll', function (e) {
header_height = header.outerHeight()
if (document_height - header_height + opt.offset > w.height()) {
// If fixed header
if (state !== false && w.scrollTop() > header_height + opt.offset) {
toggleClass('addClass')
state = false
// If no fixed header
} else if (
state !== true &&
w.scrollTop() <= header_height + opt.offset
) {
toggleClass('removeClass')
state = true
}
}
// Add direction classes
scrollTop = $(this).scrollTop()
if (scrollTop < lastScrollTop) {
header.addClass('swipe-up')
} else {
if (header.hasClass('swipe-up')) {
header.removeClass('swipe-up')
}
}
lastScrollTop = scrollTop
}).resize()
// Do anchor action on click
$(
'a[href*="#"]:not([href="#"]):not([href="#nav"]):not([href*="#mm-"]):not([class^="mm-"]'
).click(function (e) {
if (
location.pathname.replace(/^\//, '') ==
this.pathname.replace(/^\//, '') ||
location.hostname == this.hostname
) {
var target = $(this.hash)
if (target.length) {
if (opt.toAnchor === 'jump') {
jumpToAnchor(target)
} else if (opt.toAnchor === 'scroll') {
scrollToAnchor(target)
}
return false
}
}
})
// Do anchor action on page load
if (target.length && opt.onLoadAnchor !== false) {
setTimeout(function () {
window.scrollTo(0, 0)
if (opt.onLoadAnchor === 'jump') {
jumpToAnchor(target)
} else if (opt.onLoadAnchor === 'scroll') {
scrollToAnchor(target)
}
}, 1)
}
// Do anchor action after anchor change
if (opt.onChangeAnchor !== false) {
w.on('hashchange', function () {
var target = $(location.hash)
if (target.length) {
if (opt.onChangeAnchor === 'jump') {
jumpToAnchor(target)
} else if (opt.onChangeAnchor === 'scroll') {
scrollToAnchor(target)
}
}
})
}
return this
}
})(jQuery)