Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here 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>
like image 704
sharataka Avatar asked Nov 01 '12 04:11

sharataka


People also ask

How do I scroll to an element within an overflowed div?

The scrollIntoView method: The scrollIntoView() is used to scroll to the specified element in the browser. Example: Using scrollIntoView() to scroll to an element.

How do you scroll automatically to the bottom of the div using jQuery?

To auto scroll a page from top to bottom we can use scrollTop() and height() method in jquery. In this method pass the document's height in scrollTop method to scroll.

How do I horizontally scroll a div using jQuery?

jQuery scrollLeft() Method The scrollLeft() method sets or returns the horizontal scrollbar position for the selected elements. Tip: When the scrollbar is on the far left side, the position is 0.


1 Answers

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

like image 104
Jai Avatar answered Sep 30 '22 21:09

Jai