jQuery realizes the scrolling effect of table row data

jQuery realizes the scrolling effect of table row data

This article example shares the specific code of jQuery to achieve the scrolling effect of table row data for your reference. The specific content is as follows

HTML code:

<div class="box">
  <div class="box-header">
    <div class="col">Test 1</div>
    <div class="col">Test 2</div>
    <div class="col">Test 3</div>
    <div class="col">Test 4</div>
  </div>
  <div id="font-scroll">
    <div class="box-body">
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
      <div class="row">
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
        <div class="col">Test text</div>
      </div>
    </div>
  </div>
</div>

CSS style code:

.box {
      width: 400px;
      text-align: center;
      font-size: 14px;
      border: 1px solid rgba(0, 0, 0, .3);
    }

    .box .box-header {
      display: flex;
      justify-content: space-evenly;
    }

    .box-body .row {
      display: flex;
      justify-content: space-evenly;
    }

    .box-header,
    .box-body .row {
      border-bottom: 1px dashed #404040;
    }

    .box .col {
      padding: 10px 0 10px 0;
    }

    .box .col:nth-child(1) {
      width: 80px;
    }

    .box .col:nth-child(2) {
      width: 60px;
    }

    .box .col:nth-child(3) {
      width: 80px;
    }

    .box .col:nth-child(4) {
      width: 60px;
    }

    /* Content scrolling */

    #font-scroll {
      /* Content scrolling visible height*/
      height: 199px;
      overflow: hidden;
    }

JS code:

(function ($) {
  $.fn.FontScroll = function (options) {
    let d = { time: 1000 }
    $.extend(d, options);

    // The div parent box that needs to be scrolled let box = this[0]
    // scroll interval let _time = d.time

    // This method is only suitable for the case where the height of each row of data is the same // // The height of each scroll (usually the height of a data line)
    // let _contentChildHeight = box.children[0].children[0].offsetHeight
    // // The total scroll height, that is, the total height of the content (the total height of all data)
    // let _contentTotalHeight = _contentChildHeight * box.children[0].children.length

    // This method is suitable for all cases, including the case where the height of each row of data is different // Get all row elements let all_row_el = box.children[0].children
    // Total row height let _contentTotalHeight = 0
    // The superposition height of each row of data and the height of all previous rows let _contentChildHeight = []
    for (let i in all_row_el) {
      if ((new RegExp("^\\d+$")).test(i)) {
        _itemHeight = all_row_el[i].offsetHeight
        _contentTotalHeight += _itemHeight
        i == 0 ? _contentChildHeight.push(_itemHeight) : _contentChildHeight.push(_contentChildHeight[i - 1] + _itemHeight)
      }
    }

    // The div subbox that needs to be scrolled let child1 = this.children('.box-body')
    // Clone the scrolling div child box // Clone method 1 // let child1 = this.children('.box-body')[0]
    // let child2 = this.children('.box-body')[1]
    // child2.innerHTML = child1.innerHTML
    // Clone method 2 if ((box.offsetHeight + 5) < _contentTotalHeight) {
      // If the data does not reach a certain height, the scrolling effect will not be performed child1.clone().insertAfter(child1)
      /*Start the timer*/
      let timer = setInterval(autoScrollLine, 30)
      /*Single line scroll up effect*/
      function autoScrollLine() {
        /* Determine whether the scrolling content has been scrolled, if it has been scrolled, reset the scroll value to 0
        Otherwise, scroll up 1px every 30 milliseconds*/
        if (box.scrollTop >= _contentTotalHeight) {
          box.scrollTop = 0;
        } else {
          box.scrollTop++;
        }
        /*Stop the timer when the scrolling distance is exactly the height of a data line.
        Restart the timer after _time to achieve the data scrolling effect*/
        if (_contentChildHeight.indexOf(box.scrollTop) >= 0) {
          clearInterval(timer)
          setTimeout(() => {
            timer = setInterval(autoScrollLine, 30)
          }, _time)
        }
      }
    }
  }
})(jQuery);

Directions:

$('#font-scroll').FontScroll({ time: 1000 });

Effect picture:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • jQuery achieves seamless scrolling of tables
  • Jquery natively implements the table header to scroll with the scroll bar
  • asp.net+jquery scroll bar to load data drop-down control
  • jQuery scrolling component (vticker.js) realizes the scrolling effect of dynamic data on the page
  • Based on jQuery, the function of automatically loading data when the page scrolls to the bottom is realized
  • js/jquery control page dynamic loading data sliding scroll bar automatic loading event method
  • jQuery scroll loading data method
  • Use jQuery or native js to load new data on the page by mouse scrolling
  • Jquery announcement scrolling + AJAX background to get data
  • jQuery code to load data by pulling the scroll bar

<<:  Summary of Linux ps and pstree command knowledge points

>>:  4 ways to modify MySQL root password (summary)

Recommend

HTTP and HTTP Collaboration Web Server Access Flow Diagram

A web server can build multiple web sites with in...

Basic knowledge of HTML: a preliminary understanding of web pages

HTML is the abbreviation of Hypertext Markup Langu...

MySQL 8.0.19 installation and configuration tutorial under Windows 10

I will be learning MySQL next semester. I didn...

Solution to the problem that VC6.0 cannot be used when installed on WIN10

VC6.0 is indeed too old VC6.0 is a development to...

Sharing of two website page translation plug-ins

TranslateThis URL: http://translateth.is Google T...

The whole process of configuring reverse proxy locally through nginx

Preface Nginx is a lightweight HTTP server that u...

Solve the error of installing VMware Tools on Ubuntu 18.04

1. According to the online tutorial, the installa...

Vue3 gets the current routing address

Correct answer Using useRouter : // router path: ...

A must-read career plan for web design practitioners

Original article, please indicate the author and ...

Detailed explanation of Docker Secret management and use

1. What is Docker Secret 1. Scenario display We k...

How to use cc.follow for camera tracking in CocosCreator

Cocos Creator version: 2.3.4 Demo download: https...

How to implement multiple parameters in el-dropdown in ElementUI

Recently, due to the increase in buttons in the b...