web: skip scroll check

This commit is contained in:
Le Tan 2018-10-12 20:20:43 +08:00
parent ba75c94f40
commit f85f2cc3a3
5 changed files with 36 additions and 2 deletions

View File

@ -14,6 +14,8 @@ marked.setOptions({
});
var updateHtml = function(html) {
startFreshRender();
// There is at least one async job for MathJax.
asyncJobsCount = 1;

View File

@ -133,6 +133,8 @@ var updateText = function(text) {
text = "[TOC]\n\n" + text;
}
startFreshRender();
// There is at least one async job for MathJax.
asyncJobsCount = 1;
metaDataText = null;

View File

@ -265,13 +265,22 @@ window.onwheel = function(e) {
}
}
var skipScrollCheckRange = null;
window.onscroll = function() {
if (g_muteScroll) {
return;
}
currentHeaderIdx = -1;
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset;
if (skipScrollCheckRange
&& skipScrollCheckRange.start <= scrollTop
&& skipScrollCheckRange.end > scrollTop) {
return;
}
currentHeaderIdx = -1;
skipScrollCheckRange = null;
var eles = document.querySelectorAll("h1, h2, h3, h4, h5, h6");
if (eles.length == 0) {
@ -279,7 +288,8 @@ window.onscroll = function() {
return;
}
var biaScrollTop = scrollTop + 50;
var bias = 50;
var biaScrollTop = scrollTop + bias;
for (var i = 0; i < eles.length; ++i) {
if (biaScrollTop >= eles[i].offsetTop) {
currentHeaderIdx = i;
@ -291,6 +301,17 @@ window.onscroll = function() {
var curHeader = null;
if (currentHeaderIdx != -1) {
curHeader = eles[currentHeaderIdx].getAttribute("id");
// Update the range which can be skipped to check.
var endOffset;
if (currentHeaderIdx < eles.length - 1) {
endOffset = eles[currentHeaderIdx + 1].offsetTop - bias;
} else {
endOffset = document.documentElement.scrollHeight;
}
skipScrollCheckRange = { start: eles[currentHeaderIdx].offsetTop - bias,
end: endOffset };
}
content.setHeader(curHeader ? curHeader : "");
@ -1827,3 +1848,8 @@ var clearMarkRectDivs = function() {
delete n;
}
};
// Clean up before a fresh render.
var startFreshRender = function() {
skipScrollCheckRange = null;
};

View File

@ -50,6 +50,8 @@ var updateText = function(text) {
text = "[TOC]\n\n" + text;
}
startFreshRender();
// There is at least one async job for MathJax.
asyncJobsCount = 1;

View File

@ -89,6 +89,8 @@ var updateText = function(text) {
text = "[TOC]\n\n" + text;
}
startFreshRender();
// There is at least one async job for MathJax.
asyncJobsCount = 1;