Skip to content

Instantly share code, notes, and snippets.

@vinaydotblog
Last active October 2, 2015 10:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinaydotblog/2224814 to your computer and use it in GitHub Desktop.
Save vinaydotblog/2224814 to your computer and use it in GitHub Desktop.
Smooth scroll with HTML bookmarks
// Menified Version of Smooth scrool Script given below:
(function(b){var c=window.location.hash;1<c.length&&(c=b(c),c.length&&b("html,body").animate({scrollTop:c.offset().top},400));b(document).on("click","a",function(c){var a=b(this).attr("href");if(a&&~a.indexOf("#")){var a=a.substr(a.indexOf("#")+1),d=b("a[name="+a+"]"),e=b("#"+a);console.log(a);d=d.length?d:e;d.length&&(c.preventDefault(),b("html,body").animate({scrollTop:d.offset().top},400))}})})(jQuery);
// HTML Bookmarks smooth scroll, clicked on any bookmarked anchor,
// do animated scroll to it's target element.
// jQuery Required
// Update 31st May to scroll for extrnal pages.
(function($){
var h = window.location.hash;
if( h.length > 1 ) {
var target = $(h);
if( target.length ) {
$('html,body').animate({ scrollTop: target.offset().top },400);
}
}
$(document).on('click','a',function(e){
var a = $(this), href = a.attr('href');
if(href && ~href.indexOf('#')){
var name = href.substr( href.indexOf('#') + 1 ), target = $('a[name='+ name +']'), target2 = $('#' + name);
console.log(name);
target = (target.length)? target : target2;
if(target.length){
e.preventDefault();
$('html,body').animate({
scrollTop: target.offset().top
},400);
}
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment