【问题标题】:How to use scrollTop in jQuery when scrolling within a div?在 div 中滚动时如何在 jQuery 中使用 scrollTop?
【发布时间】:2012-11-01 16:16:51
【问题描述】:

左边是我要滚动的内容……它滚动一点然后停止。它似乎每次也滚动相同的量。

当我尝试滚动到的内容位于 div 中时,我正在尝试使用 jQuery 获取 scrollTop 以处理页面加载。当前的实现没有做任何事情

javascript

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

<script>
    $(document).ready(function (){
            //$(this).animate(function(){
                $('html, body').animate({
                    scrollTop: $("#test4").offset().top
                     }, 2000);
            //});
    });
</script>

html

<div class="row">
    <div class = "span12">
    
        <div class = "row">
        
            <div class = "span2">
            
                <div style="height:480px;font:12px Georgia, Garamond, Serif;overflow:auto;">
                
                    <div id = "test1">Test1</div>
                    <div id = "test2">Test2</div>
                    <div id = "test3">Test3</div>
                    <div id = "test4">Test4</div>
                
                </div>
            </div>
        
        
            <div class = "row">
                <div class = "span8">
                    Other content on the page
                </div>
            </div>
        
        </div>
    </div>
</div>

【问题讨论】:

  • 它在这里工作:jsfiddle.net/FYHXC。你确定你的页面有垂直滚动,因为它对于窗口来说太大了吗?
  • 你有足够的内容在你的页面中滚动。
  • 这确实有效:jsfiddle.net/FYHXC/1
  • @JaiPSah 我确实有足够的内容可以滚动。
  • @PulkitMittal 这似乎与我所拥有的相同,但它似乎不起作用。它每次滚动一点点(相同的量)然后停止。

标签: javascript jquery html scroll scrolltop


【解决方案1】:

我已经稍微修改了你的代码,你可以find the fiddle here

这是我用过的js代码:

$('.sss').animate({
    scrollTop: $(".test5").offset().top
}, 2000);​

这里

$('.sss')

是持有侧链的持有者

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 2020-03-10
    • 1970-01-01
    • 2011-06-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多