Last active
October 2, 2015 10:07
-
-
Save vinaydotblog/2224814 to your computer and use it in GitHub Desktop.
Smooth scroll with HTML bookmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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