以编程方式将可滚动div滚动到其顶部 [英] Scroll a scrollable div to its top programatically

查看:66
本文介绍了以编程方式将可滚动div滚动到其顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个固定高度的滚动div,里面有一个长长的列表.当我用长列表向下滚动时,我想滚动到可滚动div的顶部.我应该如何开始呢?

I have a scrollable div with a fixed height and a long list inside it. I want to scroll to top in the scrollable div when I scroll down with the long list. how should I start with this.

我试图找到任何答案,但所有答案都是使用scrollTop或offset来实现的,但我无法使它正常工作.我在可滚动的div上尝试了scrollTop,但始终未定义

I tried to find any answer but all refer to using scrollTop or offset, but I just can't get this to work. I tried scrollTop on the div that is scrollable, but it is always undefined

我正在使用JQuery-mobile.

I am using JQuery-mobile.

我有以下设置

<div id="scrollable">
  <div id="inner">
    <div id="content_wrapper">
       <div>...</div>
       <div>...</div>
       ...
    </div>
  </div>
<div>

scrollable是可滚动的容器,将溢出y应用于该容器.

scrollable is the scrollable container, which overflow-y is applied to.

我尝试了

$('#scrollable').scrollTop(fixedvalue);
$('#scrollable').scrollTop($('$scrollable').offset());
$('#scrollable').scrollTop($('firstdivelement').offset());
$('#scrollable').scrollTop($('firstdivelement').position().top);

我对javascript不太满意.

I am not very good with javascript.

推荐答案

要滚动到页面 body 的顶部:

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


要滚动到 div 的顶部:

$('#yourDivId').animate({ scrollTop: (0) }, 'slow');

希望这对您有帮助...

Hope this helps...

这篇关于以编程方式将可滚动div滚动到其顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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