我得到以下HTML:

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

使用此设置,我有点模仿扩展的 <select> 使用@Size属性定义的控制。因此,用户可以在表中选择一行。有办法使表“跳高”表,以使所选行似乎位于DIV的顶部,而垂直滚动条则滚动到其位置。我不需要实际的滚动效果。该表应立即在行上更改其位置。

有帮助吗?

解决方案

这可能有效:

$("#scrollableDiv").animate({
  scrollTop: 200 // scrolls to the bottom
}, 1000);

其他提示

我建议使用scrolltop(如果需要的话,甚至可以使其动画)。

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

http://jsfiddle.net/8meph/

这是: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')

我们在此处添加一个内联,以确保我们在可滚动区域内获得正确的启动位置。我们使用偏置差异,因此,如果您想从开始位置进行动画动画,而不是跳到其他地方。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top