[英]Scroll to ID on page not calculating offset correctly
$('.home-slide-button').on('click', function (e) {
var matches = window.location.hash.match(/^#([0-9]+)$/);
if (matches) {
var number = matches[1];
$('html, body').animate({
scrollTop: $('#' + number).offset().top -150
}, 1500);
}
});
單擊“主頁”頁面( http://buchbinderei.it/ )中的產品鏈接時,應將其重定向到“產品”頁面( http://buchbinderei.it/produkte/ ),並向下平滑滾動至所單擊的頁面。產品。 一些如何不計算最高的-150偏移量的建議?
實際錯誤的偏移位置: https : //www.dropbox.com/s/m3cbinuyl4d3pgv/Screenshot%202016-01-24%2022.47.55.png?dl=0
所需的偏移位置: https : //www.dropbox.com/s/y0lhldjvult9kdg/Screenshot%202016-01-24%2022.48.49.png?dl=0
這解決了我的問題:
if (location.hash) {
setTimeout(function() {
var matches = window.location.hash.match(/^#([0-9]+)$/);
if (matches) {
var number = matches[1];
$('html, body').animate({
scrollTop: $('#' + number).offset().top -180
}, 500);
console.log('enter');
}
}, 1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.