jquery如何偏移滚动位置 [英] jquery how to offset scroll position

查看:33
本文介绍了jquery如何偏移滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以滚动到某个元素:

I have the following code that scrolls to a certain element:

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

但是,我在顶部有一个固定的导航栏,因此希望能够抵消这一点.如何将其偏移多个像素?

However, I have a fixed navbar at the top and so would like to be able to offset this. How can I offset it by a number of pixels?

推荐答案

尝试

$('html, body').animate({
    scrollTop: $("#maincontent").offset().top - 100 // Means Less header height
},400);

这篇关于jquery如何偏移滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆