當我們單擊導航選單項時,我使用以下代碼平滑滾動到部分:
// handle links with @href started with '#' only
$(document).on('click', 'a[href^="#"]', function(e) {
// target element id
var id = $(this).attr('href');
// target element
var $id = $(id);
if ($id.length === 0) {
return;
}
// prevent standard hash navigation (avoid blinking in IE)
e.preventDefault();
// top position relative to the document
var pos = $id.offset().top;
// animated top scrolling
$('body, html').animate({scrollTop: pos}, 2000);
});
但是,我需要將標題放在固定的位置,高度為 90 像素,現在當我們滾動到該部分時,該部分的開頭隱藏在該部分的后面。是否可以保留我的代碼并包括例如距頂部一定的距離?
我試圖在 offset 方法上放一個引數,但沒有用:
// handle links with @href started with '#' only
$(document).on('click', 'a[href^="#"]', function(e) {
// target element id
var id = $(this).attr('href');
// target element
var $id = $(id);
if ($id.length === 0) {
return;
}
// prevent standard hash navigation (avoid blinking in IE)
e.preventDefault();
// top position relative to the document
var pos = $id.offset(top:120).top;
// animated top scrolling
$('body, html').animate({scrollTop: pos}, 2000);
});
難道我做錯了什么?
uj5u.com熱心網友回復:
var pos = $id.offset().top - 90;
是你的意思嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/336746.html
標籤:javascript 查询 动画片 滚动
