我有一个大表与垂直滚动条。 我想使用jQuery/JavaScript滚动到这个表中的特定行。

是否有内置的方法来做到这一点?

Here is a little example to play with. div { width: 100px; height: 70px; border: 1px solid blue; overflow: auto; } <div> <table id="my_table"> <tr id='row_1'><td>1</td></tr> <tr id='row_2'><td>2</td></tr> <tr id='row_3'><td>3</td></tr> <tr id='row_4'><td>4</td></tr> <tr id='row_5'><td>5</td></tr> <tr id='row_6'><td>6</td></tr> <tr id='row_7'><td>7</td></tr> <tr id='row_8'><td>8</td></tr> <tr id='row_9'><td>9</td></tr> </table> </div>


当前回答

我做了这个组合。这是我的工作。但如果点击,就会面临一个问题 移动该div尺寸太大,场景滚动不向下到这个 特定的div。

 var scrollDownTo =$("#show_question_" + nQueId).position().top;
        console.log(scrollDownTo);
        $('#slider_light_box_container').animate({
            scrollTop: scrollDownTo
            }, 1000, function(){
        });

        }

其他回答

你可以使用jQuery scrollTo插件插件:

$('div').scrollTo('#row_8');

我做了这个组合。这是我的工作。但如果点击,就会面临一个问题 移动该div尺寸太大,场景滚动不向下到这个 特定的div。

 var scrollDownTo =$("#show_question_" + nQueId).position().top;
        console.log(scrollDownTo);
        $('#slider_light_box_container').animate({
            scrollTop: scrollDownTo
            }, 1000, function(){
        });

        }

滚动元素到容器的中心

将元素移到容器的中心。

在CODEPEN上演示

JS

function scrollToCenter() {
  var container = $('.container'),
    scrollTo = $('.5');

  container.animate({
    //scrolls to center
    scrollTop: scrollTo.offset().top - container.offset().top + scrollTo.scrollTop() - container.height() / 2
  });
}

HTML

<div class="container">
   <div class="1">
    1
  </div>
  <div class="2">
    2
  </div>
  <div class="3">
    3
  </div>
  <div class="4">
    4
  </div>
  <div class="5">
    5
  </div>
  <div class="6">
    6
  </div>
  <div class="7">
    7
  </div>
  <div class="8">
    8
  </div>
  <div class="9">
    9
  </div>
  <div class="10">
    10
  </div>


</div>
<br>
<br>
<button id="scroll" onclick="scrollToCenter()">
  Scroll
</button>

css

.container {
  height: 60px;
  overflow-y: scroll;
  width 60px;
  background-color: white;
}

它不是精确的中心,但在更大的元素上你不会认出它。

你可以通过jQuery和JavaScript滚动 只需要两个元素的jQuery和这个JavaScript代码:

$(function() {
  // Generic selector to be used anywhere
  $(".js-scroll-to-id").click(function(e) {

    // Get the href dynamically
    var destination = $(this).attr('href');

    // Prevent href=“#” link from changing the URL hash (optional)
    e.preventDefault();

    // Animate scroll to destination
    $('html, body').animate({
      scrollTop: $(destination).offset().top
    }, 1500);
  });
});

$(function() { // Generic selector to be used anywhere $(".js-scroll-to-id").click(function(e) { // Get the href dynamically var destination = $(this).attr('href'); // Prevent href=“#” link from changing the URL hash (optional) e.preventDefault(); // Animate scroll to destination $('html, body').animate({ scrollTop: $(destination).offset().top }, 1500); }); }); #pane1 { background: #000; width: 400px; height: 400px; } #pane2 { background: #ff0000; width: 400px; height: 400px; } #pane3 { background: #ccc; width: 400px; height: 400px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="nav"> <li> <a href="#pane1" class=" js-scroll-to-id">Item 1</a> </li> <li> <a href="#pane2" class="js-scroll-to-id">Item 2</a> </li> <li> <a href="#pane3" class=" js-scroll-to-id">Item 3</a> </li> </ul> <div id="pane1"></div> <div id="pane2"></div> <div id="pane3"></div> <!-- example of a fixed nav menu --> <ul class="nav"> <li> <a href="#pane3" class="js-scroll-to-id">Item 1</a> </li> <li> <a href="#pane2" class="js-scroll-to-id">Item 2</a> </li> <li> <a href="#pane1" class="js-scroll-to-id">Item 3</a> </li> </ul>

死很简单。不需要插件。

var $container = $('div'),
    $scrollTo = $('#row_8');

$container.scrollTop(
    $scrollTo.offset().top - $container.offset().top + $container.scrollTop()
);

// Or you can animate the scrolling:
$container.animate({
    scrollTop: $scrollTo.offset().top - $container.offset().top + $container.scrollTop()
});​

下面是一个工作示例。

scrollTop的文档。