Jquery 在URL中定位时偏移顶部加载
我有平滑的锚滚动工作,但它不工作时,从其他页面来。由于固定的标题,如何偏移滚动位置 当前JS:Jquery 在URL中定位时偏移顶部加载,jquery,scrolltop,Jquery,Scrolltop,我有平滑的锚滚动工作,但它不工作时,从其他页面来。由于固定的标题,如何偏移滚动位置 当前JS: // to top right away if ( window.location.hash ) scroll(0,0); // void some browsers issue setTimeout( function() { scroll(0,0); }, 1); $(function() { // your current click function $('.scroll'
// to top right away
if ( window.location.hash ) scroll(0,0);
// void some browsers issue
setTimeout( function() { scroll(0,0); }, 1);
$(function() {
// your current click function
$('.scroll').on('click', function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $($(this).attr('href')).offset('100').top + 'px'
}, 1000, 'swing');
});
// *only* if we have anchor on the url
if(window.location.hash) {
// smooth scroll to the anchor id
$('html, body').animate({
scrollTop: $(window.location.hash).offset('100').top + 'px'
}, 1000, 'swing');
}
我找到了你的问题-这是解决方案:
// *only* if we have anchor on the url
if(window.location.hash) {
var offset = -100;
// smooth scroll to the anchor id
$('html, body').animate({
scrollTop: ($(window.location.hash).offset().top + offset) + 'px'
}, 1000, 'swing');
}
自定义偏移值以平滑滚动到所需位置