-
Notifications
You must be signed in to change notification settings - Fork 33
/
jquery.stickytabs-multi.js
92 lines (81 loc) · 3.35 KB
/
jquery.stickytabs-multi.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
/**
* jQuery Plugin: Sticky Tabs
*
* @author Aidan Lister <[email protected]>
* @version 1.2.0
*/
(function ($) {
$.fn.stickyTabs = function (options) {
var context = this;
var settings = $.extend({
getHashCallback: function (hash, btn) {
return hash
},
selectorAttribute: "href",
selectorOperator: "=",
backToTop: false,
initialTab: $('li.active > a', context),
fragmentPathSeparator: '&'
}, options);
// Show the tab corresponding with the hash in the URL, or the first tab.
var showTabFromWindowLocationHash = function () {
var decodedHash = decodeURIComponent(window.location.hash);
decodedHash = settings.selectorAttribute === "href" ? decodedHash : decodedHash.substring(1);
showTabFromHash(decodedHash);
};
var showTabFromHash = function (hash) {
var selector = hash ? 'a[' + settings.selectorAttribute + settings.selectorOperator + '"' + hash.split(settings.fragmentPathSeparator)[0] + '"]' : settings.initialTab;
$(selector, context).tab('show');
if (settings.backToTop === true) {
setTimeout(backToTop, 1);
}
else if (hash) {
var indexOfFirstFragmentPathSeparator = hash.indexOf(settings.fragmentPathSeparator);
if (indexOfFirstFragmentPathSeparator > 0) {
var remainingHash = hash.substring(indexOfFirstFragmentPathSeparator + 1);
var element = document.getElementById(remainingHash);
if (element) {
scrollToElement($(element));
}
else {
showTabFromHash(remainingHash);
}
}
}
};
var scrollToElement = function (element) {
$('html, body').animate({
scrollTop: $(element).offset().top
}, 0);
};
// Use pushState if it is available so the page will not jump, otherwise a shim.
var changeHash = function (hash) {
if (history && history.pushState) {
history.pushState(null, null, window.location.pathname + window.location.search + '#' + hash);
} else {
var scrollVertical = document.body.scrollTop;
var scrollHorizontal = document.body.scrollLeft;
window.location.hash = hash;
document.body.scrollTop = scrollVertical;
document.body.scrollLeft = scrollHorizontal;
}
};
var backToTop = function () {
window.scrollTo(0, 0);
};
// Set the correct tab when the page loads
showTabFromWindowLocationHash();
// Set the correct tab when a user uses their back/forward button
$(window).on('hashchange', showTabFromWindowLocationHash);
// Change the URL when tabs are clicked
$('a', context).on('click', function (e) {
var hash = this.href.split('#')[1];
var adjustedHash = settings.getHashCallback(hash, this);
changeHash(adjustedHash);
if (settings.backToTop === true) {
setTimeout(backToTop, 1);
}
});
return this;
};
}(jQuery));