Layui 动态表格单条循环滚动的实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Layui动态表格单条循环滚动</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link href="/static/css/font.css" rel="stylesheet">
    <link href="/static/css/weadmin.css" rel="stylesheet">
    <link href="/lib/layui/css/layui.css" rel="stylesheet" media="all">
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <!-- 让IE8/9支持媒体查询,从而兼容栅格 -->
    <script src="/lib/layui/html5.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/layui/respond.min.js" type="text/javascript" charset="utf-8"></script>
    <link id="changelink" rel="stylesheet" href="" />
    <script src="/static/js/app.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!-- 添加自定义CSS样式 -->
<style>
    .layui-table-box .layui-table-header th {
        background-color: #01AAED; /* 设置表头背景颜色 */
        color: #FAFAFA;  /* 设置字体颜色 */
        /*color: #B4D5FF;  !* 设置字体颜色 *!*/
        border-left: none;
    }
    h1 {
        text-align: center;
    }
</style>

    <div>       
        <div id="alarmTableContainer" style="height: calc(100% - 30px); margin: 10px;">
            <table id="alarmTable" class="layui-hide" lay-filter="alarmTable"></table>
        </div>
    </div>

  <script>
      function TableScroller(tableContainer, interval) {
          // 响应鼠标事件
          var that = this;
          tableContainer.on('mouseover', function () {
              that.pause();
          });
          tableContainer.on('mouseleave', function () {
              that.resume();
          });

          // 隐藏表格滚动条
          var bodyContainer = tableContainer.find('.layui-table-body');
          bodyContainer.css('overflow-x', 'hidden');
          bodyContainer.css('overflow-y', 'hidden');

          this.timerID = null;
          this.interval = interval;
          this._bodyTable = bodyContainer.find('table');
          this._tbody = this._bodyTable.find('tbody');

          this.start = function () {
              var that = this;
              that.timerID = setInterval(function () {
                  that._scroll(that._bodyTable, that._tbody, that.interval);
              }, that.interval);
          };
          this.pause = function () {
              var that = this;
              if (that.timerID === null) {
                  return;
              }

              clearInterval(that.timerID);
              that.timerID = null;
          };
          this.resume = function () {
              var that = this;
              if (that.timerID !== null || that.callback === null || that.interval === null) {
                  return;
              }

              that.timerID = setInterval(function () {
                  that._scroll(that._bodyTable, that._tbody, that.interval);
              }, that.interval);
          };
          this.stop = function () {
              var that = this;
              if (this.timerID === null) {
                  return;
              }

              clearInterval(that.timerID);
              that.callback = null;
              that.interval = null;
              that.timerID = null;
          };
          this._scroll = function (bodyTable, tbody, interval) {
              debugger
              var firstRow = tbody.find('tr:first');
              // var firstRow = tbody.find('tr:even');
              var rowHeight = firstRow.height();
              bodyTable.animate({top: '-' + rowHeight + 'px'}, interval * 0.5, function () {
                  tbody.append(firstRow.prop("outerHTML"));
                  bodyTable.css('top', '0px');
                  firstRow.remove();
              });
          }
      };

      layui.use(['table','util'], function () {
          var table = layui.table;
          var layer = layui.layer;        
          var $ = layui.jquery;
          var alarmTableContainer = $('#alarmTableContainer');
          //第一个实例
          var tabBoard3 = table.render({
              elem: '#alarmTable'
              , url: '/api/webapi/kanban' //数据接口
              , page: false//开启分页         
              , done: function (res, curr, count) {// 表格渲染完成之后的回调
                  var that = this.elem.next();
                  res.data.forEach(function (item, index) {
                      if (index % 2 == 0) {
                          var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "#F4F4FA");
                      }
                  });

                  if (res.data.length > 10 || count > 10) { // 滚动显示
                      let scroller = new TableScroller(alarmTableContainer, 5000);
                      scroller.start();
                  }
              }

              ,cols: [[ //表头
          {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
      ,{field: 'username', title: '用户名', width:80}
      ,{field: 'sex', title: '性别', width:80, sort: true}
      ,{field: 'city', title: '城市', width:80} 
      ,{field: 'sign', title: '签名', width: 177}
      ,{field: 'experience', title: '积分', width: 80, sort: true}
      ,{field: 'score', title: '评分', width: 80, sort: true}
      ,{field: 'classify', title: '职业', width: 80}
      ,{field: 'wealth', title: '财富', width: 135, sort: true}
    ]]
          });
    });
   

  </script>

</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值