-
Notifications
You must be signed in to change notification settings - Fork 37
/
jquery.jumpto.js
160 lines (142 loc) · 5.31 KB
/
jquery.jumpto.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
/* ===========================================================
* jquery-jumpto.js v1
* ===========================================================
* Copyright 2013 Pete Rojwongsuriya.
* http://www.thepetedesign.com
*
* Create a smooth jump to sub navigational sidebar
* with one js call
*
* https://github.com/peachananr/jumpto
*
* ========================================================== */
!function($){
var defaults = {
firstLevel: "> h2",
secondLevel: false,
innerWrapper: ".jumpto-block",
offset: 400,
animate: 1000,
navContainer: false,
anchorTopPadding: 20,
showTitle: "Jump To",
closeButton: true
};
function isScrolledIntoView(elem)
{
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + ($(window).height() /4);
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
$.fn.jumpto = function(options){
var settings = $.extend({}, defaults, options),
el = $(this),
html = "",
block = $(settings.innerWrapper),
selectors = "",
title = "",
close ="";
el.addClass("jumpto-cotainer");
redrawMenu = function(){
$(selectors.slice(0,-2)).each(function( index ) {
if (isScrolledIntoView($(this))) {
$(".jumpto-subnav a").removeClass("active").parent().find(" a[href='#"+$(this).attr("id")+"']").addClass("active")
if($("a[href='#"+$(this).attr("id")+"']").parent().parent().hasClass("jumpto-second")) {
$("a[href='#"+$(this).attr("id")+"']").closest(".jumpto-second").show()
}
if($("a[href='#"+$(this).attr("id")+"']").parent().parent().hasClass("jumpto-first")) {
$("a[href='#"+$(this).attr("id")+"']").closest(".jumpto-first").find(".jumpto-second").hide()
}
if($("a[href='#"+$(this).attr("id")+"']").parent().find(".jumpto-second")) {
$("a[href='#"+$(this).attr("id")+"']").parent().find(".jumpto-second").show()
}
}
});
if($(document).scrollTop() > settings.offset) {
$(".jumpto-subnav").removeClass("bottom").addClass("fixed");
} else {
$(".jumpto-subnav").removeClass("bottom fixed");
}
if($(document).scrollTop() > el.outerHeight(true)) {
$(".jumpto-subnav").addClass("bottom fixed");
}
}
block.find(settings.firstLevel).each(function( index ) {
var b = $(this),
i = index,
inner_html = "";
if ( b.parent().find(settings.secondLevel).length > 0) {
inner_html += "<ul class='jumpto-second'>"
b.parent().find(settings.secondLevel).each(function( index ) {
var id = "jumpto_" + i + "_" + index;
$(this).attr("id", id);
link_to = "<a href='#" + id + "'>" + $(this).text() + "</a>"
inner_html += "<li>" + link_to + "</li>"
selectors += "#"+id + ", ";
});
inner_html += "</ul>"
var id = "jumpto_" + i;
b.attr("id", id);
link_to = "<a href='#" + id + "'>" + b.text() + "</a>"
selectors += "#"+id + ", ";
html += "<li>" + link_to + inner_html + "</li>"
} else {
var id = "jumpto_" + i;
link_to = "<a href='#" + id + "'>" + b.text() + "</a>"
b.attr("id", id);
selectors += "#"+id + ", ";
html += "<li>" + link_to + "</li>"
}
});
if (settings.showTitle != false) {
var title = "<div class='jumpto-title'>"+settings.showTitle+"</div>"
}
if (settings.closeButton != false) {
var close = "<div class='jumpto-close'><a href='#' id='jumpto-close'>Close</a></div>"
}
if(settings.navContainer == false) {
$(this).append("<nav class='jumpto-subnav'>"+ title +"<ul class='jumpto-first'>" + html + "</ul>"+ close +"</nav>")
}else{
$(settings.navContainer).addClass("jumpto-subnav").html(title +"<ul class='jumpto-first'>" + html + "</ul>"+ close)
}
$('.jumpto-subnav a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - settings.anchorTopPadding
}, settings.animate, 'swing');
return false;
}
}
});
$(window).scroll(function() {
redrawMenu()
});
$(".jumpto-subnav #jumpto-close").click(function() {
var btn = $(this)
btn.parent().parent().find("> .jumpto-first").slideToggle( "slow", function() {
if ($(this).is(":visible")) {
btn.html("Close");
} else {
btn.html("Open");
}
});
return false;
});
setInterval(function() {
var track = [];
$(selectors.slice(0,-2)).each(function( index ) {
track.push(isScrolledIntoView($(this)))
});
if($.inArray(true, track) == -1) {
$(".jumpto-subnav a").removeClass("active")
$(".jumpto-subnav .jumpto-second").hide()
}
}, 500);
}
}(window.jQuery);