向下滚动到div +一定的边距 [英] Scroll down to div + a certain margin

查看:81
本文介绍了向下滚动到div +一定的边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此脚本滚动到页面上的某个.div:

I am using this script to scroll to a certain .div on my page:

$(".button").click(function() {
    $('html, body').animate({
        scrollTop: $(".scrolltothis").offset().top
    }, 500);
});

完美运行.这会将页面滚动到"scrolltothis" div的顶部.现在这是问题所在: 我有一个菜单栏,该菜单栏在页面顶部具有固定位置.向下滚动时,菜单栏位于页面顶部.

Works perfectly. This scrolls the page to the top of the "scrolltothis" div. Now here's the problem: I have a menu bar which has a fixed position at the top of the page. It's the kind of menu bar that stays on top of the page when scrolling down.

因此,当脚本向下滚动到"scrolltothis" div时,该div的一部分将落在菜单栏的后面.

So when the script scrolls down to the "scrolltothis" div, a part of the div falls behind the menu bar.

我需要的是一种告诉浏览器向下滚动到"scrolltothis div" +向下50像素的方法,以便该div在菜单栏下完全可见.

希望你们能帮助我!

推荐答案

在设置它时,只需将50个像素添加到scrollTop即可.

Just add 50 pixels to scrollTop while setting it.Try this:

$(".button").click(function() {
 $('html, body').animate({
    scrollTop: $(".scrolltothis").offset().top + 50;
 }, 500);
});

这篇关于向下滚动到div +一定的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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