-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_navbar.js
192 lines (157 loc) · 4.51 KB
/
fixed_navbar.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Description - Uses jQuery to make IPS 4's default theme's navbar fixed when you scroll down past the header on the page. Using for The Modding Community!
* Author - Christian Deacon (@gamemann)
* Open Source - https://github.com/modcommunity/Web-Open-Source
*
*
* The Modding Community
* Taking Modding To The Next Level!
* ModdingCommunity.com
**/
/* Configuration */
// If you've made a CSS class, insert it here. Otherwise, the code will manually apply what is needed each time which may slow down browser performance by a small margin.
fix_class = "";
unfix_class = "";
nav_addclass = "";
// Navbar(s) selection.
var navbar_sel = "#ipsLayout_header nav";
// Retrieve elements we need to change for The Modding Community's website.
var navbars = $(navbar_sel);
// Always do error checks :)
if (!navbars)
{
throw new Error("[TMC Fixed Navbar] Could not find IPS 4 navbars.");
}
// Header selection.
var header_sel = "#ipsLayout_header header";
// Icon image, class, or ID selector.
var icon_sel = "#nav-icon-fixed";
// IPS 4 header layout.
var header_layout = $("#ipsLayout_header");
// If an icon image exist, enablethat.
var icon = $(icon_sel);
// For header height calculation.
var header = $(header_sel);
var header_height;
if (header)
{
header_height = header.height();
}
if (header_height < 1)
{
// Header height in pixels.
header_height = 80;
}
// Navbar max height in pixels.
navbar_maxheight = navbars.first().height();
if (navbar_maxheight < 1)
{
navbar_maxheight = 52;
}
// Fixed transparently.
var unfixed_opacity = 0.7;
var fixed_opacity = 1.0;
// Check to see if we should just add a custom class to the navbar.
if (nav_addclass.length > 0)
{
navbars.addClass(nav_addclass);
}
else
{
// Set general navbar settings for fixed support.
navbars.css('z-index', '100');
navbars.css('width', '100%');
navbars.css('top', '0px');
navbars.css('left', '0px');
}
navbars.css('max-height', navbar_maxheight);
// Leaving commented function just because it was somewhat neat.
/*
navbars.each(function(i)
{
if ($(this).prop("data-controller") != "core.front.core.navBar")
{
return;
}
navbars.remove($(this));
});
*/
// Fixed toggle variable.
fixed = false;
// Position (Y).
var pos = 0;
// Loop through the full window's scroll events.
$(window).scroll(function(e)
{
// If the navbar is hidden, return so it doesn't impact mobile devices.
if (navbars.first().is(":hidden"))
{
// If our margin (bottom) is more than 0px, this indicates a user went from desktop to mobile theme. So set back margin bottom.
var margin = header_layout.css('margin-bottom');
if (margin && parseInt(margin) > 0)
{
header_layout.css('margin-bottom', navbar_maxheight);
}
return;
}
// Retrieve current Y position.
pos = window.scrollY;
// Check if we're under the header or not.
if (pos > header_height && !fixed)
{
if (fix_class.length < 1)
{
navbars.css('background', 'rgba(var(--theme-main_nav), ' + fixed_opacity + ')');
navbars.css('position', 'fixed');
}
else
{
navbars.addClass(fix_class);
navbars.removeClass(unfix_class);
}
if (icon)
{
icon.css('visibility', 'visible');
}
if (header_layout)
{
header_layout.css('margin-bottom', navbar_maxheight * 2); // We times it by two to include the second navbar.
}
fixed = true;
} else if (pos <= header_height && fixed)
{
// Revert back to non-fixed navbar.
if (unfix_class.length < 1)
{
navbars.css('background', 'rgba(var(--theme-main_nav), ' + unfixed_opacity + ')');
navbars.css('position', 'relative');
}
else
{
navbars.addClass(unfix_class);
navbars.removeClass(fix_class);
}
if (icon)
{
icon.css('visibility', 'hidden');
}
if (header_layout)
{
header_layout.css('margin-bottom', '0px');
}
fixed = false;
}
});
$(window).resize(function()
{
if (navbars.first().is(":visible"))
{
return;
}
// If our margin (bottom) is more than 0px, this indicates a user went from desktop to mobile theme. So set back margin bottom.
var margin = header_layout.css('margin-bottom');
if (margin && parseInt(margin) > 0)
{
header_layout.css('margin-bottom', '0px');
}
});