From ec414893e2f019f7e757d5ccc0be30c6e44c3a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3na=20Lore?= Date: Mon, 8 Feb 2016 10:02:21 +0100 Subject: [PATCH] Behavior to enable Smooth Scrolling if URL has a fragment. --- e107_web/js/core/front.jquery.js | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/e107_web/js/core/front.jquery.js b/e107_web/js/core/front.jquery.js index 65d0d6a4f..56eabe630 100644 --- a/e107_web/js/core/front.jquery.js +++ b/e107_web/js/core/front.jquery.js @@ -1,5 +1,48 @@ /* global $ */ +var e107 = e107 || {'settings': {}, 'behaviors': {}}; + +(function ($) +{ + // In case the page was opened with a hash, prevent jumping to it. + // http://stackoverflow.com/questions/3659072/how-to-disable-anchor-jump-when-loading-a-page + if(window.location.hash) + { + $('html, body').stop().animate({scrollTop: 0}); + } + + /** + * Behavior to initialize Smooth Scrolling on document, if URL has a fragment. + * TODO: create theme option on the admin panel to: + * - enable/disable smooth scrolling + * - change animation duration + * - set top-offset if theme has a fixed top navigation bar + * + * @type {{attach: Function}} + */ + e107.behaviors.initializeSmoothScrolling = { + attach: function (context, settings) + { + if(window.location.hash) + { + $(context).find('body').once('initialize-smooth-scrolling').each(function () + { + if($(window.location.hash).length !== 0) + { + $('html, body').stop().animate({ + scrollTop: $(window.location.hash).offset().top + }, 2000); + + return false; + } + }); + } + } + }; + +})(jQuery); + + $(document).ready(function() { $(":input").tooltip();