Make table auto scroll vertically via HTML & CSS

29,728

Solution 1

JS (or jQuery) is needed to get the actual element height and scrollHeight and perform the animation on those values

var $el = $(".table-responsive");
function anim() {
  var st = $el.scrollTop();
  var sb = $el.prop("scrollHeight")-$el.innerHeight();
  $el.animate({scrollTop: st<sb/2 ? sb : 0}, 4000, anim);
}
function stop(){
  $el.stop();
}
anim();
$el.hover(stop, anim);
.table-responsive{
  height:180px; width:50%;
  overflow-y: auto;
  border:2px solid #444;
}.table-responsive:hover{border-color:red;}

table{width:100%;}
td{padding:24px; background:#eee;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="table-responsive">
  <table class="table table-bordered table-hover">
    <thead>
      <tr><th>#</th></tr>
    </thead>
    <tbody>  
      <tr><td>1</td></tr>
      <tr><td>2</td></tr> 
      <tr><td>3</td></tr> 
      <tr><td>4</td></tr> 
      <tr><td>5</td></tr> 
      <tr><td>6</td></tr> 
      <tr><td>7</td></tr> 
      <tr><td>8</td></tr> 
      <tr><td>9</td></tr> 
      <tr><td>10</td></tr> 
    </tbody>
  </table>
</div>

Solution 2

I highly recommend using using javascript for this. I tried this using CSS only once and then promptly abandoned the idea, but it is theoretically possible. The following demo is not tested on all browsers, has huge compatibility issues, and was the jankiest thing ever on Safari. Moral of the story: use javascript.

@import url(https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css);

.table-responsive {
  overflow-x: hidden;
}

table.table {
  animation: ani linear 1s alternate infinite;
}

.table-responsive:hover {
  overflow: auto;
}
.table-responsive:hover table.table {
  animation: none ;
}

@keyframes ani {
	0% { margin-left: 0; transform: translate3d(0%, 0, 0);}
	25% { margin-left: 0; transform: translate3d(0%, 0, 0);}
	75% { margin-left: 100%; transform: translate3d(-100%, 0, 0);}
	100% { margin-left: 100%; transform: translate3d(-100%, 0, 0);}
}
<div class="table-responsive">
  <table class="table table-bordered table-hover">
    <thead>
      <tr>
        <th style="text-align: left; font-size: 23px;">#</th>
        <th style="text-align: left; font-size: 23px;">Name</th>
        <th style="text-align: left; font-size: 23px;">Description</th>
        <th style="text-align: left; font-size: 23px;">Date</th>
        <th style="text-align: left; font-size: 23px;">Date</th>
        <th style="text-align: left; font-size: 23px;">Date</th>
        <th style="text-align: left; font-size: 23px;">Date</th>
        <th style="text-align: left; font-size: 23px;">Date</th>
      </tr>
    </thead>
    <tbody>  
      <tr class="danger">
        <td style="text-align: left; font-size: 16px;">1213</td>
        <td style="text-align: left; font-size: 16px;">John Doe</td>
        <td style="text-align: left; font-size: 16px;">my short description</td>
        <td style="text-align: left; font-size: 16px;">Today's Date</td>
        <td style="text-align: left; font-size: 16px;">Today's Date</td>
        <td style="text-align: left; font-size: 16px;">Today's Date</td>
        <td style="text-align: left; font-size: 16px;">Today's Date</td>
        <td style="text-align: left; font-size: 16px;">Today's Date</td>
      </tr> 
    </tbody>
  </table>
</div> 

<div class="table-responsive">
  <table class="table table-bordered table-hover">
    <thead>
      <tr>
        <th style="text-align: left; font-size: 23px;">#</th>
        <th style="text-align: left; font-size: 23px;">Name</th>
        <th style="text-align: left; font-size: 23px;">Description</th>
        <th style="text-align: left; font-size: 23px;">Date</th>
      </tr>
    </thead>
    <tbody>  
      <tr class="danger">
        <td style="text-align: left; font-size: 16px;">1213</td>
        <td style="text-align: left; font-size: 16px;">John Doe</td>
        <td style="text-align: left; font-size: 16px;">my short description</td>
        <td style="text-align: left; font-size: 16px;">Today's Date</td>
      </tr> 
    </tbody>
  </table>
</div>

Solution 3

The code below will not reset and start back from the top the table as asked in the question, but this will help some future readers who trying to scroll and loop the table rows automatically with a fixed header.

<!DOCTYPE html>
    <html>
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    	    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    	<script type="text/javascript">
    	$.fn.infiniteScrollUp=function(){
    		var self=this,kids=self.children()
    		kids.slice(20).hide()
    		setInterval(function(){
    			kids.filter(':hidden').eq(0).fadeIn()
    			kids.eq(0).fadeOut(function(){
    				$(this).appendTo(self)
    				kids=self.children()
    			})
    		},1000)
    		return this
    	}
    	$(function(){
    		$('tbody').infiniteScrollUp()
    	})
    	</script>
    	<title>infiniteScrollUp</title>
    </head>
    <body>
    <table>
    <colgroup><col /><col /><col /><col /><col /><col /></colgroup>
    <thead>
    <tr><th>a</th><th>b</th><th>c</th><th>d</th><th>e</th><th>f</th></tr>
    </thead>
    <tbody>
    <tr><td>1a</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1b</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1c</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1d</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1e</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1f</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1g</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1h</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1i</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1j</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1k</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1l</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1m</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1n</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1o</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1p</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1q</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1r</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1s</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1t</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1u</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1v</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1w</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1x</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1y</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>1z</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
    </tbody>
    </table>
    </body>
    </html>

Share:
29,728

Related videos on Youtube

Code
Author by

Code

Updated on July 05, 2022

Comments

  • Code
    Code almost 2 years

    I have a simple bootstrap table that I am trying to have vertically scroll automatically. The reason for this, is that the table will be displayed on a screen and the table could have 10 items in it or 100. So I would like it to auto scroll vertically so the user does not have to do it manually. After it reaches the end, it will just reset and start back from the top...

    This is my markup thus far:

    <div class="table-responsive" style="height: 700px; overflow: auto;">
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th style="text-align: left; font-size: 23px;">#</th>
                    <th style="text-align: left; font-size: 23px;">Name</th>
                    <th style="text-align: left; font-size: 23px;">Description</th>
                    <th style="text-align: left; font-size: 23px;">Date</th>
                </tr>
            </thead>
            <tbody>  
                <tr class="danger">
                    <td style="text-align: left; font-size: 16px;">1213</td>
                    <td style="text-align: left; font-size: 16px;">John Doe</td>
                    <td style="text-align: left; font-size: 16px;">my short description</td>
                    <td style="text-align: left; font-size: 16px;">Today's Date</td>
                </tr> 
           </tbody>
       </table>
    </div> 
    

    NOTE: I am hoping this can be achevied with only HTML and CSS.

    Any suggestions?

    • Roko C. Buljan
      Roko C. Buljan almost 8 years
      CSS? no way. JS should be used, or jQuery (for xbrowser stuff & simplicity of code)
  • Roko C. Buljan
    Roko C. Buljan almost 8 years
    :( Sadly this won't work if you want to have the actual overflow: auto; > in the means that, on hover, you allow a user to take control over the scrolling. I tried to expand on your general idea to no avail... Always got to the point that the scrollbar was behaving squishy...
  • Joseph Marikle
    Joseph Marikle almost 8 years
    @RokoC.Buljan Not disagreeing that this shouldn't be used (yet. Maybe in the future it'll be easier to do), but you could easily add :hover rules that pause or disable animations and enable scrolling. That's the easy part. Getting it all to work the same across all the major browsers is where the nightmare begins.
  • Roko C. Buljan
    Roko C. Buljan almost 8 years
    Even making the scrollbas visible on hover animation pause. The scrollbar will never be in the right position due the change in the translate values. +'d for the nice idea.
  • Joseph Marikle
    Joseph Marikle almost 8 years
    @RokoC.Buljan I find it funny that you use the term "right position". Although it may be possible to adapt the code to do that too, I specifically engineered it to behave the way it does. I like things like this to reset at the beginning and allow the user to scroll the whole data. I see this as similar to a "read more". You're still going to want to see all the data, so why would you pop the user wherever the animation happens to be at the time? Regardless, all that wasn't even specified by OP. There are areas of web dev that have a "right" way to do things. IMO, this isn't one of them.
  • Vinod Balakrishnan
    Vinod Balakrishnan over 4 years
    Is there a possibility to pause on mouse over in this code ?
  • Asad Naeem
    Asad Naeem over 2 years
    putting this line in style="overflow: auto;" is working for me.