$(function() { $("a[href^='#']").on('click', function(e) { var hash = this.hash; if (hash) { e.preventDefault(); var targetName = hash.slice(1); targetName.replace(/'/g, "\\\'").replace(/"/g, "\\\""); var targetSelector = "*[id='" + targetName + "'], *[name='" + targetName + "']"; var matchingTargets = $(targetSelector); if (matchingTargets.length < 1) { return; } if (matchingTargets.length > 1) { console.warn("found more than one anchor to go to; will go to the first one"); } var matchingTarget = matchingTargets.first(); var scrollHeight = matchingTarget.offset().top; if (!matchingTarget.hasClass("fragment-anchor")) { scrollHeight -= 50; } $('html, body').animate( {scrollTop: scrollHeight}, 300, function () { window.location.hash = hash; } ); } }); $(".move-top").on("click",function() { $('html, body').animate( {scrollTop: 0}, 300 ); }); });