Commit 5cc9188e authored by Robert Lord's avatar Robert Lord

Fix tocify to reset cache on browser window resize

parent 9e84ebf2
...@@ -591,6 +591,14 @@ ...@@ -591,6 +591,14 @@
} }
}); });
// Reset height cache on scroll
$(window).on('resize', function() {
console.log("resizing" + self.cachedHeights);
self.calculateHeights();
console.log("done" + self.cachedHeights);
});
// Window scroll event handler // Window scroll event handler
$(window).on("scroll.tocify", function() { $(window).on("scroll.tocify", function() {
...@@ -668,7 +676,10 @@ ...@@ -668,7 +676,10 @@
closestAnchorIdx = null, closestAnchorIdx = null,
anchorText; anchorText;
self.calculateHeights(); // if never calculated before, calculate and cache the heights
if (self.cachedHeights.length == 0) {
self.calculateHeights();
}
// Determines the index of the closest anchor // Determines the index of the closest anchor
self.cachedAnchors.each(function(idx) { self.cachedAnchors.each(function(idx) {
...@@ -725,16 +736,14 @@ ...@@ -725,16 +736,14 @@
// ADDED BY ROBERT // ADDED BY ROBERT
calculateHeights: function() { calculateHeights: function() {
var self = this; var self = this;
if (self.cachedHeights.length == 0) { self.cachedHeights = [];
self.cachedHeights = []; self.cachedAnchors = [];
self.cachedAnchors = []; var anchors = $(self.options.context).find("div[data-unique]");
var anchors = $(self.options.context).find("div[data-unique]"); anchors.each(function(idx) {
anchors.each(function(idx) { var distance = (($(this).next().length ? $(this).next() : $(this)).offset().top - self.options.highlightOffset);
var distance = (($(this).next().length ? $(this).next() : $(this)).offset().top - self.options.highlightOffset); self.cachedHeights[idx] = distance;
self.cachedHeights[idx] = distance; });
}); self.cachedAnchors = anchors;
self.cachedAnchors = anchors;
}
}, },
// Show // Show
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment