2

次のHTMLを取得しました。

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

この設定では<select>、@size属性が定義された拡張コントロールをいくらか模倣しています。したがって、ユーザーはテーブル内の行を選択できます。テーブルを「ジャンプアップ」させて、選択した行がdivの上部に表示され、垂直スクロールバーがその位置までスクロールされるようにする方法はありますか。実際のスクロール効果は必要ありません。テーブルは、行をクリックするとすぐにその位置を変更する必要があります。

4

3 に答える 3

2

これはうまくいくかもしれません:

$("#scrollableDiv").animate({
  scrollTop: 200 // scrolls to the bottom
}, 1000);
于 2010-09-17T18:50:42.780 に答える
1

scrollTopを使用することをお勧めします(または、必要に応じてアニメーション化することもできます)。

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

http://jsfiddle.net/8mepH/

于 2010-09-17T18:53:32.283 に答える
1

これは、 http://www.balupton.com/sandbox/jquery-scrollto/demo/で使用されているコードの変更された抜粋です 。

あなたがやりたいことをするために:

            // 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')

ここにインラインを追加して、スクロール可能な領域内で正しい開始位置を取得できるようにします。オフセット差を使用するため、アニメーションを実行する場合は、別の場所にジャンプするのではなく、開始位置からアニメーションを作成します。

于 2010-09-17T18:58:13.380 に答える