-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnchorPoint.js
142 lines (128 loc) · 3.35 KB
/
AnchorPoint.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
(function ($) {
var initialized = false;
$.fn.AnchorPoint = function (args) {
if (initialized) {
console.warn("AnchorPoint is already initialized.\n Using settings from the first initialization");
return false;
}
initialized = true;
// Set Defaults
var opts = args || {};
var options = opts;
options.depth = opts.depth || 0.2;
options.anchorTimeout = opts.anchorTimeout || 500;
options.debug = opts.debug || false;
if (opts.depth === 0) {
options.depth = 0;
}
var $scope = $(this);
var $anchor;
var anchorGap;
var scrollTimeout = false;
// When resized
$(window).on("load scroll", function () {
if (scrollTimeout) {
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(function () {
$anchor = findAnchor($scope, options);
anchorGap = $anchor.offset().top - window.scrollY;
// Show anchor
if (options.debug) {
$("[data-AnchorPoint-anchor]").removeAttr("data-AnchorPoint-anchor");
$anchor.attr("data-AnchorPoint-anchor", true);
}
}, options.anchorTimeout);
} else {
scrollTimeout = true;
$anchor = findAnchor($scope, options);
anchorGap = $anchor.offset().top - window.scrollY;
}
});
$(window).on("resize", function () {
$(window).scrollTop($anchor.offset().top - anchorGap);
});
// Show anchor line
if (options.debug) {
var $line = $("<div>");
var lineAt = $(window).height() * options.depth;
$line.css({
position: "fixed",
top: lineAt,
left: 0,
width: "100%",
height: "1px",
background: "red"
});
$("body").append($line);
$(window).on("resize", function () {
var lineAt = $(window).height() * options.depth;
$line.css("top", lineAt);
});
}
// Allow chaining
return this;
};
function findAnchor($start, options) {
var $search = $start;
var $prev = $start;
var scrollTop = window.scrollY;
var depthOffset = $(window).height() * options.depth;
var anchorPoint = scrollTop + depthOffset;
var $children;
var $next;
var hasChildren;
var hasNext;
var offsetTop;
var offsetBottom;
var isAnchorPointAboveTop;
var isAnchorPointBelowTop;
var isAnchorPointAboveBottom;
var isAnchorPointBelowBottom;
var isAnchorPointWithin;
var isQualified;
while (true) {
$next = $search.next();
$children = $search.children();
hasNext = $next.length;
hasChildren = $children.length;
offsetTop = $search.offset().top;
offsetBottom = offsetTop + $search.outerHeight();
isQualified = $search.css("position") !== "fixed";
isAnchorPointAboveTop = anchorPoint < offsetTop;
isAnchorPointBelowTop = anchorPoint >= offsetTop;
isAnchorPointAboveBottom = anchorPoint <= offsetBottom;
isAnchorPointBelowBottom = anchorPoint > offsetBottom;
isAnchorPointWithin = isAnchorPointBelowTop && isAnchorPointAboveBottom;
if (!isQualified) {
if (hasNext) {
$search = $next;
continue;
}
return $prev;
}
if (isAnchorPointWithin) {
if (hasChildren) {
$prev = $search;
$search = $children.first();
continue;
} else {
return $search;
}
}
if (isAnchorPointAboveTop) {
return $search;
}
if (isAnchorPointBelowBottom) {
if (hasNext) {
$prev = $search;
$search = $search.next();
continue;
} else {
return $search;
}
}
// Unable to locate a valid anchor
return false;
}
}
}(jQuery));