js实现表格无缝滚动效果

举报
经典鸡翅 发表于 2022/02/17 22:58:04 2022/02/17
【摘要】 <!doctype html><html><head><meta charset="utf-8"><title>table表格无缝向上滚动</title><script src="http://libs.baidu.com/jquery/1.11...

    
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>table表格无缝向上滚动</title>
  6. <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
  7. <style>
  8. .tablebox {
  9. height: 500px;
  10. overflow: hidden;
  11. position: relative;
  12. width: 1000px;
  13. margin: 100px auto;
  14. background-color: rgba(6,26,103,1);
  15. }
  16. .tbl-header {
  17. width: 100%;
  18. position: absolute;
  19. top: 0;
  20. left: 0;
  21. z-index: 999;
  22. }
  23. .tbl-body {
  24. width: 100%;
  25. position: absolute;
  26. top: 0;
  27. left: 0;
  28. }
  29. .tablebox table {
  30. width: 100%;
  31. }
  32. .tablebox table th,
  33. .tablebox table td {
  34. font-size: 24px;
  35. color: #7ca6f4;
  36. line-height: 45px;
  37. text-align: center;
  38. }
  39. .tablebox table tr th {
  40. background-color: #1f1f9c;
  41. cursor: pointer;
  42. }
  43. .tablebox table tr td {
  44. background-color: transparent;
  45. }
  46. .tbl-body tr:nth-child(even) td,.tbl-body1 tr:nth-child(even) td {
  47. background-color: rgba(31, 31, 156, .5);
  48. }
  49. .tablebox table tr td span,
  50. .tablebox table tr td span {
  51. font-size: 24px;
  52. }</style>
  53. </head>
  54. <body>
  55. <div class="tablebox">
  56. <div class="tbl-header">
  57. <table border="0" cellspacing="0" cellpadding="0">
  58. <thead>
  59. <tr>
  60. <th>排名</th>
  61. <th>地市</th>
  62. <th>销售收入(万元)</th>
  63. <th>同比(%)</th>
  64. <th>环比(%)</th>
  65. <th>销售计划(万元)</th>
  66. <th>计划完成率(%)</th>
  67. </tr>
  68. </thead>
  69. <tbody style="opacity:0;"></tbody>
  70. </table>
  71. </div>
  72. <div class="tbl-body">
  73. <table border="0" cellspacing="0" cellpadding="0">
  74. <thead>
  75. <tr>
  76. <th>排名</th>
  77. <th>地市</th>
  78. <th>销售收入(万元)</th>
  79. <th>同比(%)</th>
  80. <th>环比(%)</th>
  81. <th>销售计划(万元)</th>
  82. <th>计划完成率(%)</th>
  83. </tr>
  84. </thead>
  85. <tbody></tbody>
  86. </table>
  87. </div>
  88. </div>
  89. <script>
  90. var MyMarhq = '';
  91. clearInterval(MyMarhq);
  92. $('.tbl-body tbody').empty();
  93. $('.tbl-header tbody').empty();
  94. var str = '';
  95. var Items = [{"Ranking":"1","City":"保定","SaleIncome":"2506734.43","SaleRough":"296071.96","SalePlan":"7200000","PlanFinish":"34.82","TonOilincome":"264.15","OilTransform":"29.62","An":"53.00","Mom":"-13.00"},
  96. {"Ranking":"2","City":"沧州","SaleIncome":"1425935.58","SaleRough":"93717.83","SalePlan":"3200000","PlanFinish":"44.56","TonOilincome":"366.59","OilTransform":"11.23","An":"65.00","Mom":"43.00"},
  97. {"Ranking":"3","City":"秦皇岛","SaleIncome":"1372207.38","SaleRough":"241071.82","SalePlan":"3000000","PlanFinish":"45.74","TonOilincome":"342.32","OilTransform":"6.61","An":"34.00","Mom":"7.00"},
  98. {"Ranking":"4","City":"衡水","SaleIncome":"972451.15","SaleRough":"87638.60","SalePlan":"2700000","PlanFinish":"36.02","TonOilincome":"246.03","OilTransform":"11.56","An":"15.00","Mom":"-18.00"},
  99. {"Ranking":"5","City":"石家庄","SaleIncome":"939010.52","SaleRough":"140217.37","SalePlan":"7200000","PlanFinish":"13.04","TonOilincome":"139.44","OilTransform":"19.23","An":"-57.00","Mom":"-48.00"},
  100. {"Ranking":"6","City":"邢台","SaleIncome":"774274.70","SaleRough":"124693.90","SalePlan":"3700000","PlanFinish":"20.93","TonOilincome":"138.87","OilTransform":"9.44","An":"-20.00","Mom":"-44.00"},
  101. {"Ranking":"7","City":"唐山","SaleIncome":"680456.79","SaleRough":"50542.14","SalePlan":"3600000","PlanFinish":"18.90","TonOilincome":"243.60","OilTransform":"16.95","An":"-29.00","Mom":"-49.00"},
  102. {"Ranking":"8","City":"张家口","SaleIncome":"613319.87","SaleRough":"176075.96","SalePlan":"3000000","PlanFinish":"20.44","TonOilincome":"87.09","OilTransform":"7.97","An":"2.00","Mom":"-24.00"},
  103. {"Ranking":"9","City":"中油华奥","SaleIncome":"596575.25","SaleRough":"387024.26","SalePlan":"11000000","PlanFinish":"5.42","TonOilincome":"93.58","OilTransform":"19.06","An":"35.00","Mom":"6.00"},
  104. {"Ranking":"10","City":"承德","SaleIncome":"589048.12","SaleRough":"68966.73","SalePlan":"2200000","PlanFinish":"26.77","TonOilincome":"193.24","OilTransform":"11.29","An":"30.00","Mom":"-28.00"},
  105. {"Ranking":"11","City":"廊坊","SaleIncome":"515448.14","SaleRough":"137934.72","SalePlan":"3500000","PlanFinish":"14.73","TonOilincome":"95.47","OilTransform":"8.80","An":"-48.00","Mom":"-3.00"},
  106. {"Ranking":"12","City":"瑞州","SaleIncome":"399875.26","SaleRough":"85371.46","SalePlan":"2000000","PlanFinish":"19.99","TonOilincome":"114.42","OilTransform":"11.42","An":"128.00","Mom":"-30.00"},
  107. {"Ranking":"13","City":"石家庄中油","SaleIncome":"90543.62","SaleRough":"20065.14","SalePlan":"430000","PlanFinish":"21.06","TonOilincome":"213.44","OilTransform":"20.45","An":"-24.00","Mom":"-17.00"},
  108. {"Ranking":"14","City":"辛集中油","SaleIncome":"49255.52","SaleRough":"8743.12","SalePlan":"250000","PlanFinish":"19.70","TonOilincome":"202.67","OilTransform":"29.95","An":"19.00","Mom":"-6.00"},
  109. {"Ranking":"15","City":"井陉中油","SaleIncome":"29682.60","SaleRough":"2175.66","SalePlan":"140000","PlanFinish":"21.20","TonOilincome":"730.20","OilTransform":"25.55","An":"-74.00","Mom":"-52.00"},
  110. {"Ranking":"16","City":"保定中油","SaleIncome":"11887.73","SaleRough":"2944.97","SalePlan":"100000","PlanFinish":"11.89","TonOilincome":"118.12","OilTransform":"34.16","An":"-64.00","Mom":"-72.00"}]
  111. $.each(Items,function (i, item) {
  112. str = '<tr>'+
  113. '<td>'+item.Ranking+'</td>'+
  114. '<td>'+item.City+'</td>'+
  115. '<td>'+(+item.SaleIncome/10000).toFixed(2)+'</td>'+
  116. '<td>'+(+item.An).toFixed(2)+'</td>'+
  117. '<td>'+(+item.Mom).toFixed(2)+'</td>'+
  118. '<td>'+(item.SalePlan/10000).toFixed(2)+'</td>'+
  119. '<td>'+(+item.PlanFinish).toFixed(2)+'</td>'+
  120. '</tr>'
  121. $('.tbl-body tbody').append(str);
  122. $('.tbl-header tbody').append(str);
  123. });
  124. if(Items.length > 10){
  125. $('.tbl-body tbody').html($('.tbl-body tbody').html()+$('.tbl-body tbody').html());
  126. $('.tbl-body').css('top', '0');
  127. var tblTop = 0;
  128. var speedhq = 50; // 数值越大越慢
  129. var outerHeight = $('.tbl-body tbody').find("tr").outerHeight();
  130. function Marqueehq(){
  131. if(tblTop <= -outerHeight*Items.length){
  132. tblTop = 0;
  133. } else {
  134. tblTop -= 1;
  135. }
  136. $('.tbl-body').css('top', tblTop+'px');
  137. }
  138. MyMarhq = setInterval(Marqueehq,speedhq);
  139. // 鼠标移上去取消事件
  140. $(".tbl-header tbody").hover(function (){
  141. clearInterval(MyMarhq);
  142. },function (){
  143. clearInterval(MyMarhq);
  144. MyMarhq = setInterval(Marqueehq,speedhq);
  145. })
  146. }
  147. </script>
  148. </body>
  149. </html>

文章来源: blog.csdn.net,作者:经典鸡翅,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/hanqing456/article/details/111879021

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。