如何使用jQuery滚动到div的顶部?

问题描述 投票:46回答:6

我在div里面有一个gridview ..我想使用jquery从div底部滚动到div的顶部..任何建议..

<div id="GridDiv">
// gridview inside..
</div>

我的gridview将自定义分页生成链接按钮...我将滚动到链接按钮底部的div顶部点击...

protected void Nav_OnClick(object sender, CommandEventArgs e)
    {
        LinkButton lb1 = (LinkButton)sender;
        //string s = lb1.ID;
        ScriptManager.RegisterClientScriptBlock(lb1, typeof(LinkButton), 
 "scroll", "javascript:document.getElementById('GridDiv').scrollTop = 0;", true);

在javascript的位置,我将调用jquery函数...任何建议......

编辑:

完全像每个用户页面的Stackoverflow问题...当更改页面nos时,它滚动到顶部,效果流畅......我想实现这一点......

jquery html scroll scrolltop
6个回答
4
投票

你可以使用:

<div id="GridDiv">
// gridview inside...
</div>

<a href="#GridDiv">Scroll to top</a>

168
投票

以下是使用jquery可以执行的操作:

$('#A_ID').click(function (e) { //#A_ID is an example. Use the id of your Anchor
    $('html, body').animate({
        scrollTop: $('#DIV_ID').offset().top - 20 //#DIV_ID is an example. Use the id of your destination on the page
    }, 'slow');
});

36
投票

或者,为了减少代码,在您的点击内放置:

setTimeout(function(){ 

$('#DIV_ID').scrollTop(0);

}, 500);

1
投票

我不知道为什么,但你必须添加一个至少200毫秒的setTimeout:

setTimeout( function() {$("#DIV_ID").scrollTop(0)}, 200 );

使用Firefox / Chrome / Edge进行测试。


1
投票

特别感谢Stoic

   $("#miscCategory").animate({scrollTop: $("#miscCategory").offset().top});

0
投票

使用以下功能

window.scrollTo(xpos, ypos)

这里xpos是必需的。要沿x轴(水平)滚动到的坐标(以像素为单位)

ypos也是必需的。要沿y轴(垂直)滚动到的坐标,以像素为单位

© www.soinside.com 2019 - 2024. All rights reserved.