如何在div中滚动时在jQuery中使用scrollTop? [英] How to use scrollTop in jQuery when scrolling within a div?

查看:101
本文介绍了如何在div中滚动时在jQuery中使用scrollTop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


左边是我正在尝试滚动的内容.. .it滚动一点然后停止。它似乎每次都滚动相同的数量。

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.

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

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

<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



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>


推荐答案

我已经改变了你的代码,你可以在这里找到小提琴

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

这是js我用过的代码:

this is the js code i have used:

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

here

$('.sss')

是持有sidelinks

is the holder which holds the sidelinks

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

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