Sto usando il seguente codice per passare ai punti di ancoraggio con jQuery:jQuery scorrimento per ancorare (determinata quantità meno di pixel)
$(document).ready(function() {
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');
$('a[href*=#]').each(function() {
var thisPath = filterPath(this.pathname) || locationPath;
if ( locationPath == thisPath
&& (location.hostname == this.hostname || !this.hostname)
&& this.hash.replace(/#/,'')) {
var $target = $(this.hash), target = this.hash;
if (target) {
var targetOffset = $target.offset().top;
$(this).click(function(event) {
event.preventDefault();
$(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
location.hash = target;
});
});
}
}
});
// use the first element that is "scrollable"
function scrollableElement(els) {
for (var i = 0, argLength = arguments.length; i <argLength; i++) {
var el = arguments[i],
$scrollElement = $(el);
if ($scrollElement.scrollTop()> 0) {
return el;
} else {
$scrollElement.scrollTop(1);
var isScrollable = $scrollElement.scrollTop()> 0;
$scrollElement.scrollTop(0);
if (isScrollable) {
return el;
}
}
}
return [];
}
});
Esiste un modo per farlo scorrere fino a che l'ancora, ma meno un certo lasso di pixel? (nel mio caso lo voglio andare -92px)
Grazie per qualsiasi aiuto.
Simile a http://stackoverflow.com/questions/832860/how-to-scroll-the-window-using-jquery-scrollto-function – robbrit
Come sono simili ??Il mio è scorrere fino a OGNI ancora tramite jquery, ciò che hai collegato a vuole scorrere quando sono vicino alla parte superiore della pagina. Il codice è molto diverso. – John
L'unica differenza riguarda il selettore a cui si desidera scorrere e l'offset. Guarda la risposta accettata e sostituisci "# id" con il selettore desiderato e sostituisci "100" con l'offset desiderato. – robbrit