Created
April 17, 2017 08:13
-
-
Save Robbertdk/add1cc3a4098f7e616ea1ec6511e7793 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* URL updates and the element focus is maintained | |
* | |
* originally found via in Update 3 on http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links | |
* Author: HEATHER MIGLIORISI | |
* URL: https://css-tricks.com/smooth-scrolling-accessibility/ | |
*/ | |
// filter handling for a /dir/ OR /indexordefault.page | |
function filterPath(string) { | |
return string | |
.replace(/^\//, '') | |
.replace(/(index|default).[a-zA-Z]{3,4}$/, '') | |
.replace(/\/$/, ''); | |
} | |
var locationPath = filterPath(location.pathname); | |
$('a[href*="#"]').each(function () { | |
var thisPath = filterPath(this.pathname) || locationPath; | |
var hash = this.hash; | |
if ($("#" + hash.replace(/#/, '')).length) { | |
if (locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/, '')) { | |
var $target = $(hash), target = this.hash; | |
if (target) { | |
$(this).click(function (event) { | |
event.preventDefault(); | |
$('html, body').animate({scrollTop: $target.offset().top}, 1000, function () { | |
location.hash = target; | |
$target.focus(); | |
if ($target.is(":focus")){ //checking if the target was focused | |
return false; | |
}else{ | |
$target.attr('tabindex','-1'); //Adding tabindex for elements not focusable | |
$target.focus(); //Setting focus | |
}; | |
}); | |
}); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment