How to use scrollTop in jQuery when scrolling within a div?

时光怂恿深爱的人放手 提交于 2019-12-12 11:43:20

问题


The left is the what I'm trying to scroll...it scrolls just a little bit and then stops. It seems to scroll the same amount each time too.

I am trying to get the scrollTop using jQuery to work on page load when the content I am trying to scroll to is located in within a div. The current implementation doesn't do anything

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>

回答1:


I have changed your code little bit you can find the fiddle here.

this is the js code i have used:

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

here

$('.sss')

is the holder which holds the sidelinks



来源:https://stackoverflow.com/questions/13171191/how-to-use-scrolltop-in-jquery-when-scrolling-within-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!