views:

35

answers:

3

Hello. I apologize if the title of this question is a bit misleading, I could not come up with a better name.

I got the following HTML:

<div style="height:200px;overflow-y:scroll;">
  <table>.....</table>
</div>

With this setup I'm somewhat mimicking the expanded <select> control with @size attribute defined. So, that a user could select a row in the table. Is there a way, to make the table "jump up", so that the selected row appears to be at the top of the div and the vertical scrollbar scrolled to its position. I don't need the actual scrolling effect. The table should change it's position right away on row click.

I feel like this isn't a very good explanation, I'm struggling to find the right words. Let me know if I should clarify anything, please.

Thank you.

+1  A: 

This might work:

$("#scrollableDiv").animate({
  scrollTop: 200 // scrolls to the bottom
}, 1000);
elektronikLexikon
see http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12
elektronikLexikon
@elektronikLexikon: This appears to work the best. How can I find out the magic pixel number to which it should scroll? Table rows are of uneven height. I'm thinking of summing row heights for every row preceding the clicked one. Is there an easier way?
Dimskiy
try offset method: $("tr#blah_blah_blah").offset().top
elektronikLexikon
@elektronikLexikon: Already tried. It doesn't work this way for table rows. Neither does the position().top :(
Dimskiy
oh... but see http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12, this site could be interesting for you
elektronikLexikon
@elektronikLexikon: Ok. I just got away with summing up heights and scrolling to that number. I'll check out your link. Thanks!
Dimskiy
+1  A: 

I suggest using scrollTop (or even animate it if you want).

$('div')[0].scrollTop = 200 //would set the scrollable div to 200px down.

http://jsfiddle.net/8mepH/

John Strickler
+1  A: 

Here is an modified extract of the code used in: http://www.balupton.com/sandbox/jquery-scrollto/demo/

To do what you want:

            // Fetch the scrollable div
            $container = $('#scrollable');
            // Fetch the target div
            $target = $('#target');

            // Prepare the Inline Element of the Container
            var $inline = $('<span/>').css({
                'position': 'absolute',
                'top': '0px',
                'left': '0px'
            });
            var position = $container.css('position');

            // Insert the Inline Element of the Container
            $container.css('position','relative');
            $inline.appendTo($container);

            // Determine the Offsets
            var startOffset = $inline.offset().top,
                targetOffset = $target.offset().top,
                offsetDifference = targetOffset - startOffset;

            // Perform the jump
            $container.css('scrollTop',offsetDifference+'px')

We add a inline here to ensure we get the correct start position within the scrollable area. We use a offset difference so if you want to do animations it animations from the start position, rather than jumping somewhere else.

balupton