-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.offsetanchors.js
executable file
·73 lines (60 loc) · 2.52 KB
/
jquery.offsetanchors.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
/**
* Offsets the scroll position of anchor links by the height of the element specified in the jQuery selector. This is a
* singleton and can only be initialised once.
*
* Usage:
* jQuery("#stalker").offsetAnchors();
*
* licence Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
jQuery.fn.offsetAnchors = function () {
var currentTargetName, isClick, offsetScroll, offsetElemSelector = this.selector;
offsetScroll = function() {
var targetElemName, targetScroll, targetElemOffset, offsetElem, offsetElemHeight, offsetElemY, targetElem;
targetElemName = window.location.href.replace(/.*#(.*)/,"$1");
if (targetElemName === currentTargetName && !isClick) {
return;
}
if (!/\w+/.test(targetElemName)) {
return;
}
offsetElem = jQuery(offsetElemSelector);
offsetElemHeight = offsetElem.outerHeight();
offsetElemY = offsetElem.offset().top;
targetElem = jQuery("#" + targetElemName);
if (targetElem.length === 0) {
targetElem = jQuery("a[name=" + targetElemName + "]");
}
if (!targetElem.is("visible")) {
targetElem = targetElem.parent();
}
targetElemOffset = targetElem.offset().top;
if (targetElem.length > 0 && (jQuery(window).scrollTop() > offsetElemY + offsetElemHeight)) {
targetScroll = targetElemOffset - 30 - offsetElemHeight;
if (jQuery.browser.safari) {
jQuery(function () {
window.setTimeout(function () {
jQuery(window).scrollTop(targetScroll);
}, 100);
});
} else {
jQuery(window).scrollTop(targetScroll);
}
currentTargetName = targetElemName;
}
};
if (/#.+/.test(window.location.href)) {
jQuery(window).one("scroll", offsetScroll);
}
jQuery(document).click(function () {
isClick = true;
jQuery(window).one("scroll", offsetScroll);
window.setTimeout(function () {
jQuery("html,body").unbind("scroll", offsetScroll);
isClick = null;
}, 20);
});
jQuery.fn.offsetAnchors = function () {
throw "@method jQuery.fn.offsetAnchors: Anchors can only be offset from a single element. Current offset elem is: " + offsetElemSelector;
};
};