scrollTop jquery,滚动到div的id? [英] scrollTop jquery, scrolling to div with id?

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

问题描述

所以这是目前的代码我有

So this is the current code I have

$(document).ready(function() {
    $('.abouta').click(function(){
        $('html, body').animate({scrollTop:308}, 'slow');
        return false;
    });
    $('.portfolioa').click(function(){
        $('html, body').animate({scrollTop:708}, 'slow');
        return false;
    });
    $('.contacta').click(function(){
        $('html, body').animate({scrollTop:1108}, 'slow');
        return false;
    });
});

'abouta'滚动到页面的特定部分。我宁愿做一个scrollTo,然后一个div的id,所以我不必继续改变scrollTo的位置,如果我改变padding等。有什么办法吗?
感谢

When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way? Thanks

推荐答案

而不是

$('html, body').animate({scrollTop:xxx}, 'slow');

使用

$('html, body').animate({scrollTop:$('#div_id').position().top}, 'slow');

这将返回您选择的任何元素的绝对顶部位置 #div_id

this will return the absolute top position of whatever element you select as #div_id

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

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