How do I scroll a row of a table into view (element.scrollintoView) using jQuery?

后端 未结 11 1605
刺人心
刺人心 2020-11-27 12:37

I\'m dynamically adding rows to a table using jQuery. The table is inside a div which has overflow:auto thus causing a vertical scroll

相关标签:
11条回答
  • 2020-11-27 13:12

    I couldn't add a comment to Abhijit Rao's answer above, so I am submitting this as an additional answer.

    I needed to have the table column scroll into view on a wide table, so I added the scroll left features into the function. As someone mentioned, it jumps when it scrolls, but it worked for my purposes.

    function scrollIntoView(element, container) {
      var containerTop = $(container).scrollTop();
      var containerLeft = $(container).scrollLeft();
      var containerBottom = containerTop + $(container).height();
      var containerRight = containerLeft + $(container).width();
    
      var elemTop = element.offsetTop;
      var elemLeft = element.offsetLeft;
      var elemBottom = elemTop + $(element).height();
      var elemRight = elemLeft + $(element).width();
    
      if (elemTop < containerTop) {
        $(container).scrollTop(elemTop);
      } else if (elemBottom > containerBottom) {
        $(container).scrollTop(elemBottom - $(container).height());
      }
    
      if(elemLeft < containerLeft) {
        $(container).scrollLeft(elemLeft);
      } else if(elemRight > containerRight) {
        $(container).scrollLeft(elemRight - $(container).width());
      }
    }
    
    0 讨论(0)
  • 2020-11-27 13:16

    much simpler:

    $("selector for element").get(0).scrollIntoView();
    

    if more than one item returns in the selector, the get(0) will get only the first item.

    0 讨论(0)
  • 2020-11-27 13:16

    This runnable example shows how to use scrollIntoView() which is supported in all modern browsers: https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView#Browser_Compatibility

    The example below uses jQuery to select the element with #yourid.

    $( "#yourid" )[0].scrollIntoView();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p id="yourid">Hello world.</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>

    0 讨论(0)
  • 2020-11-27 13:18

    $( "#yourid" )[0].scrollIntoView();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p id="yourid">Hello world.</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>

    0 讨论(0)
  • 2020-11-27 13:21

    If you just want to scroll, you could use jQuery's scrollTop method. http://docs.jquery.com/CSS/scrollTop

    var table = jQuery( 'table' );
    table.scrollTop( table.find( 'tr:last' ).scrollTop() );
    

    Something like that maybe?

    0 讨论(0)
提交回复
热议问题