From c16b124c7053fd269b46ad57274398002ecbea86 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Je=CC=81re=CC=81my=20Fabre?= Date: Wed, 26 Oct 2016 15:54:18 +0200 Subject: [PATCH] Fix summary scroll spy --- app/static/src/js/modules/summary.js | 27 +++++++++++++++++++--- app/templates/includes/footer_scripts.html | 1 + bower.json | 3 ++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/static/src/js/modules/summary.js b/app/static/src/js/modules/summary.js index 41a555d7..9db79ef8 100644 --- a/app/static/src/js/modules/summary.js +++ b/app/static/src/js/modules/summary.js @@ -37,6 +37,27 @@ Summary.prototype.init = function() { $element.attr('id', "section-" + sectionCount); sectionCount++; + $element.waypoint(function(direction) { + that.$links.forEach(function (elem) { + elem.removeClass('active'); + }); + $('[href="#' + $(this.element).attr('id') + '"]').addClass('active'); + }, { + offset: '200' + }); + + $element.waypoint(function(direction) { + that.$links.forEach(function (elem) { + elem.removeClass('active'); + }); + var sectionNumber = parseInt($(this.element).attr('id').substr(8)); + sectionNumber--; + console.log(sectionNumber); + $('[href="#section-' + sectionNumber + '"]').addClass('active'); + }, { + offset: '50%' + }); + }); }); @@ -44,7 +65,7 @@ Summary.prototype.init = function() { $template.remove(); // Scrollspy - $(document).on("scroll", that.onScroll.bind(that)); + //$(document).on("scroll", that.onScroll.bind(that)); // Row height if($('.page__sidebar .nav-tree--level-0').height() > $('.page__content').height()) { @@ -63,13 +84,13 @@ Summary.prototype.onScroll = function(e) { var scrollPos = $(document).scrollTop(), that = this, - currentTitle, minDiff = 9999999999999; + currentTitle, minDiff = 200; that.$links.forEach(function (elem) { var currLink = elem; var refElement = $(elem.attr("href")); var diff = refElement.offset().top - scrollPos; - if(diff < minDiff && diff > 0) { + if(diff < minDiff && diff < 200) { minDiff = diff; currentTitle = refElement; } diff --git a/app/templates/includes/footer_scripts.html b/app/templates/includes/footer_scripts.html index f0da2e92..3d8c5818 100644 --- a/app/templates/includes/footer_scripts.html +++ b/app/templates/includes/footer_scripts.html @@ -12,6 +12,7 @@ + {% block extra_js %}{% endblock %} {% editable_loader %} diff --git a/bower.json b/bower.json index 371b5424..70000d74 100644 --- a/bower.json +++ b/bower.json @@ -28,6 +28,7 @@ "audiojs": "^1.0.1", "video.js": "^5.12.5", "videojs-playlist": "^2.4.0", - "jquery_lazyload": "jquery.lazyload#^1.9.7" + "jquery_lazyload": "jquery.lazyload#^1.9.7", + "waypoints": "^4.0.1" } } -- 2.39.5