Skip to content

Commit

Permalink
Merge pull request #106 from telerik/scroll-bug-from-top
Browse files Browse the repository at this point in the history
fix: Incorrect scroll offset on initial page load and when starting from the top
  • Loading branch information
imtodor authored Aug 7, 2020
2 parents e68830b + 12279ac commit c29c54e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion _assets/js/api-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $(function () {
if (!!initialHash) {
setTimeout(function(){
animateScrolling(initialHash);
}, 100);
}, 200);
}

$("#markdown-toc")
Expand Down
15 changes: 14 additions & 1 deletion _assets/js/toc-base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
function animateScrolling(hash) {
var isApiSection = $("article.api-reference").length == 1;
var breadCrumbsElement = $("p.breadcrumbs");
var hasBreadCrumbs = breadCrumbsElement.length == 1 && breadCrumbsElement.text().trim() != "";
var currentScrollTop = $(window).scrollTop();
var offset = $(hash).offset() || { top: currentScrollTop };
var scrollOffsetCorrection = currentScrollTop == 0 ? HEADER_HEIGHT + NAVBAR_HEIGHT : NAVBAR_HEIGHT;

var scrollOffsetCorrection = NAVBAR_HEIGHT;
if (currentScrollTop == 0) {
scrollOffsetCorrection += HEADER_HEIGHT;
if (hasBreadCrumbs) {
scrollOffsetCorrection += BREADCRUMBS_HEIGHT;
}
if (isApiSection) {
scrollOffsetCorrection += API_SCROLL_FIX;
}
}

$('html, body').animate({
scrollTop: offset.top - scrollOffsetCorrection
Expand Down
2 changes: 1 addition & 1 deletion _assets/js/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ $(function() {
if (!!initialHash) {
setTimeout(function(){
animateScrolling(initialHash);
}, 100);
}, 200);
}

// animated scroll
Expand Down
4 changes: 3 additions & 1 deletion _assets/js/top-menu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var HEADER_HEIGHT = 100;
var HEADER_HEIGHT = 81;
var TELERIKBAR_HEIGHT = 70;
var NAVBAR_HEIGHT = 76;
var BREADCRUMBS_HEIGHT = 20;
var API_SCROLL_FIX = 40;
var SCROLLSPY_OFFSET = TELERIKBAR_HEIGHT + 10; // 10 compensates for the space above the anchored heading
var FOOTER_DISTANCE = 20;
var windowHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

0 comments on commit c29c54e

Please sign in to comment.