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

后端 未结 11 1604
刺人心
刺人心 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 12:54

    Plugin that scrolls (with animation) only when required

    I've written a jQuery plugin that does exactly what it says on the tin (and also exactly what you require). The good thing is that it will only scroll container when element is actually off. Otherwise no scrolling will be performed.

    It works as easy as this:

    $("table tr:last").scrollintoview();
    

    It automatically finds closest scrollable ancestor that has excess content and is showing scrollbars. So if there's another ancestor with overflow:auto but is not scrollable will be skipped. This way you don't need to provide scrollable element because sometimes you don't even know which one is scrollable (I'm using this plugin in my Sharepoint site where content/master is developer independent so it's beyond my control - HTML may change when site is operational so can scrollable containers).

    0 讨论(0)
  • 2020-11-27 12:59
    var elem=jQuery(this);
    elem[0].scrollIntoView(true);
    
    0 讨论(0)
  • 2020-11-27 13:05
    var rowpos = $('#table tr:last').position();
    
    $('#container').scrollTop(rowpos.top);
    

    should do the trick!

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

    I found a case (overflow div > table > tr > td) in which scrolling to the relative position of the tr does not work. Instead, I had to scroll the overflow container (div) using scrollTop to <tr>.offset().top - <table>.offset().top. Eg:

    $('#container').scrollTop( $('#tr').offset().top - $('#td').offset().top )
    
    0 讨论(0)
  • 2020-11-27 13:07

    This following works better if you need to scroll to an arbitrary item in the list (rather than always to the bottom):

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

    Same from above with little modification

    function focusMe() {
           var rowpos = $('#FocusME').position();
            rowpos.top = rowpos.top - 30;
            $('#container').scrollTop(rowpos.top);
        }
    <html>
    <script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
    <body>
        <input type="button" onclick="focusMe()" value="focus">
        <div id="container" style="max-height:200px;overflow:scroll;">
            <table>
                <tr>
                    <td>1</td>
                    <td></td>
                </tr>
                <tr>
                    <td>2</td>
                    <td></td>
                </tr>
                <tr>
                    <td>3</td>
                    <td></td>
                </tr>
                <tr>
                    <td>4</td>
                    <td></td>
                </tr>
                <tr>
                    <td>5</td>
                    <td></td>
                </tr>
                <tr>
                    <td>6</td>
                    <td></td>
                </tr>
                <tr>
                    <td>7</td>
                    <td></td>
                </tr>
                <tr>
                    <td>8</td>
                    <td></td>
                </tr>
                <tr>
                    <td>9</td>
                    <td></td>
                </tr>
                <tr id="FocusME">
                    <td>10</td>
                    <td></td>
                </tr>
                <tr>
                    <td>11</td>
                    <td></td>
                </tr>
                <tr>
                    <td>12</td>
                    <td></td>
                </tr>
                <tr>
                    <td>13</td>
                    <td></td>
                </tr>
                <tr>
                    <td>14</td>
                    <td></td>
                </tr>
                <tr>
                    <td>15</td>
                    <td></td>
                </tr>
                <tr>
                    <td>16</td>
                    <td></td>
                </tr>
                <tr>
                    <td>17</td>
                    <td></td>
                </tr>
                <tr>
                    <td>18</td>
                    <td></td>
                </tr>
                <tr>
                    <td>19</td>
                    <td></td>
                </tr>
                <tr>
                    <td>20</td>
                    <td></td>
                </tr>
            </table>
        </div>
    </body>
    
    </html>

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