0
|
1 |
/* |
|
2 |
* jQuery One Page Nav Plugin |
|
3 |
* http://github.com/davist11/jQuery-One-Page-Nav |
|
4 |
* |
|
5 |
* Copyright (c) 2010 Trevor Davis (http://trevordavis.net) |
|
6 |
* Dual licensed under the MIT and GPL licenses. |
|
7 |
* Uses the same license as jQuery, see: |
|
8 |
* http://jquery.org/license |
|
9 |
* |
|
10 |
* @version 0.7 |
|
11 |
* |
|
12 |
* Example usage: |
|
13 |
* $('#nav').onePageNav({ |
|
14 |
* currentClass: 'current', |
|
15 |
* changeHash: false, |
|
16 |
* scrollSpeed: 750 |
|
17 |
* }); |
|
18 |
*/ |
|
19 |
;(function($) { |
|
20 |
$.fn.onePageNav = function(options) { |
|
21 |
var opts = $.extend({}, $.fn.onePageNav.defaults, options), |
|
22 |
onePageNav = {}; |
|
23 |
|
|
24 |
onePageNav.sections = {}; |
|
25 |
|
|
26 |
onePageNav.bindNav = function($el, $this, o) { |
|
27 |
var $par = $el.parent(), |
|
28 |
newLoc = $el.attr('href'), |
|
29 |
$win = $(window); |
|
30 |
|
|
31 |
if(!$par.hasClass(o.currentClass)) { |
|
32 |
if(o.begin) { |
|
33 |
o.begin(); |
|
34 |
} |
|
35 |
onePageNav.adjustNav($this, $par, o.currentClass); |
|
36 |
$win.unbind('.onePageNav'); |
|
37 |
$.scrollTo(newLoc, o.scrollSpeed, { |
|
38 |
offset: { |
|
39 |
top: -o.scrollOffset |
|
40 |
}, |
|
41 |
onAfter: function() { |
|
42 |
if(o.changeHash) { |
|
43 |
window.location.hash = newLoc; |
|
44 |
} |
|
45 |
$win.bind('scroll.onePageNav', function() { |
|
46 |
onePageNav.scrollChange($this, o); |
|
47 |
}); |
|
48 |
if(o.end) { |
|
49 |
o.end(); |
|
50 |
} |
|
51 |
} |
|
52 |
}); |
|
53 |
} |
|
54 |
}; |
|
55 |
|
|
56 |
onePageNav.adjustNav = function($this, $el, curClass) { |
|
57 |
$this.find('.'+curClass).removeClass(curClass); |
|
58 |
$el.addClass(curClass); |
|
59 |
}; |
|
60 |
|
|
61 |
onePageNav.getPositions = function($this, o) { |
|
62 |
$this.find('a').each(function() { |
|
63 |
var linkHref = $(this).attr('href'), |
|
64 |
divPos = $(linkHref).offset(), |
|
65 |
topPos = divPos.top; |
|
66 |
|
|
67 |
onePageNav.sections[linkHref.substr(1)] = Math.round(topPos) - o.scrollOffset; |
|
68 |
}); |
|
69 |
}; |
|
70 |
|
|
71 |
onePageNav.getSection = function(windowPos) { |
|
72 |
var returnValue = '', |
|
73 |
windowHeight = Math.round($(window).height() / 2); |
|
74 |
|
|
75 |
for(var section in onePageNav.sections) { |
|
76 |
if((onePageNav.sections[section] - windowHeight) < windowPos) { |
|
77 |
returnValue = section; |
|
78 |
} |
|
79 |
} |
|
80 |
return returnValue; |
|
81 |
}; |
|
82 |
|
|
83 |
onePageNav.scrollChange = function($this, o) { |
|
84 |
onePageNav.getPositions($this, o); |
|
85 |
|
|
86 |
var windowTop = $(window).scrollTop(), |
|
87 |
position = onePageNav.getSection(windowTop); |
|
88 |
|
|
89 |
if(position !== '') { |
|
90 |
onePageNav.adjustNav($this,$this.find('a[href=#'+position+']').parent(), o.currentClass); |
|
91 |
} |
|
92 |
}; |
|
93 |
|
|
94 |
onePageNav.init = function($this, o) { |
|
95 |
var didScroll = false; |
|
96 |
|
|
97 |
$this.find('a').bind('click', function(e) { |
|
98 |
onePageNav.bindNav($(this), $this, o); |
|
99 |
e.preventDefault(); |
|
100 |
}); |
|
101 |
|
|
102 |
onePageNav.getPositions($this, o); |
|
103 |
|
|
104 |
$(window).bind('scroll.onePageNav', function() { |
|
105 |
didScroll = true; |
|
106 |
}); |
|
107 |
|
|
108 |
setInterval(function() { |
|
109 |
if(didScroll) { |
|
110 |
didScroll = false; |
|
111 |
onePageNav.scrollChange($this, o); |
|
112 |
} |
|
113 |
}, 250); |
|
114 |
}; |
|
115 |
|
|
116 |
return this.each(function() { |
|
117 |
var $this = $(this), |
|
118 |
o = $.meta ? $.extend({}, opts, $this.data()) : opts; |
|
119 |
|
|
120 |
onePageNav.init($this, o); |
|
121 |
|
|
122 |
}); |
|
123 |
}; |
|
124 |
|
|
125 |
// default options |
|
126 |
$.fn.onePageNav.defaults = { |
|
127 |
currentClass: 'current', |
|
128 |
changeHash: false, |
|
129 |
scrollSpeed: 0, |
|
130 |
scrollOffset: 0, |
|
131 |
begin: false, |
|
132 |
end: false |
|
133 |
}; |
|
134 |
|
|
135 |
})(jQuery); |