滚动到div - jquery - 具体位置 [英] scroll to div - jquery - specific position

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

问题描述

我正在尝试此代码:

  function goToByScroll(id){

        id = id.replace("link", "");

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

    $("#sidebar > ul > li > a").click(function(e) {

        e.preventDefault();

        goToByScroll($(this).attr("id"));           
    });

问题是,当我点击列表的特定元素时,滚动就会上升到顶部的窗口。但在我的情况下,我在顶部有一个固定的div,所以内容被这个div隐藏。好吧,我想在div之前停止滚动。

The problem is that when i click in a specific element of list, the scroll go up to the top of the window. But in my case i have a fixed div in the top, so the content is hidden by this div. Well, I want stops the scroll before the div.

任何想法?

演示

推荐答案

你需要给顶部栏一个id(例如id =header),然后从scrollTop属性中减去它:

You need to give the top bar an id (e.g. id="header") and then subtract that from the scrollTop attribute:

$('html,body').animate({
    scrollTop: ($("#"+id).offset().top-$('#header').outerHeight(true))+'px'},
    'slow');

这是一个有效的例子

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

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