-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scrollnav.js
72 lines (63 loc) · 2.69 KB
/
jquery.scrollnav.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
// (C) Dylan Knutson 2014
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(function($) {
$.fn.scrollNav = function(opts) {
//fix bootstrap bug when navbar is fixed positioned
if(opts) {
if(opts.bootstrap_mobile) {
$(document.body).append("<style type='text/css'> @media(max-width: 767px) { .navbar-fixed-top, .navbar-fixed-bottom, .navbar-static-top { margin-right: 0; margin-left: 0;} }</style>");
}
}
var
window_scroll = $(window).scrollTop(),
navbar = this,
navbar_height = navbar.height(),
scroll_last = window_scroll,
navbar_visible = navbar_height;
var resize_handler = function(event) {
navbar_height = navbar.height();
};
$(window).resize(resize_handler);
//bootstrap mobile compatibility
this.find(".nav-collapse").on("shown", resize_handler);
this.find(".nav-collapse").on("hidden", resize_handler);
$(window).scroll(function(event) {
//calculate how far the window was scrolled
//scrolling up the page is a positive delta
window_scroll = $(window).scrollTop();
var
scroll_delta = scroll_last - window_scroll,
navbar_visible_new = navbar_visible + scroll_delta;
if(scroll_delta < 0) {
//scrolling down the page
if(navbar_visible == navbar_height) {
//navbar currently is totally visible, and has fixed positioning set
//set to abs positioning so it begins to go out of frame
navbar.css({"position": "absolute", "top": Math.max(window_scroll, 0) + "px"});
}
//else:
//navbar will be partially visible, let abs positioning move it
}
else if(scroll_delta > 0) {
if(navbar_visible <= 0) {
//navbar was not visible, set abs positioning right above this
navbar.css({"position": "absolute", "top": Math.max(window_scroll - navbar_height, 0) + "px"});
}
//scrolling up the page
if(navbar_visible_new >= navbar_height) {
//navbar will be 100% visible
navbar.css({"position": "fixed", "top": "0px"});
}
}
//recalculate the amount the navbar is visible
navbar_visible = Math.min(Math.max(navbar_visible_new, 0), navbar_height);
scroll_last = window_scroll;
});
};
})(jQuery);