简体   繁体   English

jQuery滚动到顶部但不是很顶部

[英]jQuery scroll to top but not quite top

I'm not sure if this the correct title to ask but I'm trying to scroll to an element in the DOM on page load. 我不确定这是否是正确的标题,但是我试图在页面加载时滚动到DOM中的元素。

I'd like to have the DOM element centered for the user but this code I'm currently using forces the element to the top: 我想以用户为中心的DOM元素,但是我当前正在使用的这段代码将元素推到顶部:

$("body, html").animate({ 
   scrollTop: $('#' + url_parameter).offset().top 
});

My question is, how can I set a number of pixels for the element to be separated from the top? 我的问题是,如何为要与顶部分开的元素设置多个像素? eg 100px 例如100px

Define it, like you do, minus 100px; 像您一样定义它,减去100px;

var pos = $('#' + url_parameter).offset().top - 100;
$("body, html").animate({ 
   scrollTop: pos 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM