Na CSS-tricks.com trafiłem na taki oto kod do utworzenia efektu Smooth Scrolling. Drugą linię kodu chyba rozumiem- łapiemy znaczniki a których wartość parametru href zawierają # ale nie te atrybuty href, które mają tylko wartość #. W trzeciej lini kodu nie wiem dlaczego znaki /^\// w ścieżce zastępuje się ' '. Ale największą niewiadomą jest dla mnie piąta linia kodu.
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
Co tutaj tak naprawdę się dzieje? Dlaczego łapiemy atrybut 'name' jeśli żaden znacznik html w kodzie strony go nie posiada? Dodaje też link do codepen gdzie przedstawione jest działanie tego kodu.
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});