jQuery平滑滚动偏移 [英] jQuery Smooth Scroll Offset

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

问题描述

我有以下jQuery代码

I have the following jQuery code

$(document).ready(function(){
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();

        var target = this.hash,
        $target = $(target);

        $('html, body').stop().animate({
            'scrollTop': $target.offset().top
        }, 900, 'swing', function () {
            window.location.hash = target;
        });
    });
});

这是一个滚动插件,可以平滑地向下滚动到页面中的内部链接.我想用-140px偏移学校,因此它不会向右滚动到DIV.

This is a scrolling plugin which scrolls down smoothly to internal links in a page. I would like to offset the school by -140px so it does not scroll right to the DIV.

如何实现?

推荐答案

尝试...

$('html, body').stop().animate({
    'scrollTop': $target.offset().top-140
}, 900, 'swing', function () {
    window.location.hash = target;
});

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

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