-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsticky-menu.js
27 lines (27 loc) · 1.24 KB
/
sticky-menu.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
// menu sticky
jQuery( document ).ready( function ( $ ) {
// grab the initial top offset of the navigation
var sticky_navigation_offset_top = $( '.rsrc-main-menu' ).offset().top;
// our function that decides weather the navigation bar should have "fixed" css position or not.
var sticky_navigation = function () {
var scroll_top = $( window ).scrollTop(); // our current vertical position from the top
var $admin_bar = $( '#wpadminbar' ); // check the admin bar
if ( $admin_bar.length ) {
$top = 32;
} else {
$top = 0;
}
// if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
if ( scroll_top > sticky_navigation_offset_top ) {
$( '.rsrc-main-menu' ).css( { 'position': 'fixed', 'top': $top, 'z-index': '500', 'width': '100%', 'transition': 'all 0.5s linear' } );
} else {
$( '.rsrc-main-menu' ).css( { 'position': 'relative', 'top': 0, 'transition': 'inherit' } );
}
};
// run our function on load
sticky_navigation();
// and run it again every time you scroll
$( window ).scroll( function () {
sticky_navigation();
} );
} );