js实现表格无缝滚动效果

前端之家收集整理的这篇文章主要介绍了js实现表格无缝滚动效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  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. }
  26. .tableBox table {
  27. width: 100%;
  28. }
  29. .tableBox table th,.tableBox table td {
  30. font-size: 24px;
  31. color: #7ca6f4;
  32. line-height: 45px;
  33. text-align: center;
  34. }
  35. .tableBox table tr th {
  36. background-color: #1f1f9c;
  37. cursor: pointer;
  38. }
  39. .tableBox table tr td {
  40. background-color: transparent;
  41. }
  42. .tbl-body tr:nth-child(even) td,.tbl-body1 tr:nth-child(even) td {
  43. background-color: rgba(31,31,156,.5);
  44. }
  45. .tableBox table tr td span,.tableBox table tr td span {
  46. font-size: 24px;
  47. }</style>
  48. </head>
  49. <body>
  50. <div class="tableBox">
  51. <div class="tbl-header">
  52. <table border="0" cellspacing="0" cellpadding="0">
  53. <thead>
  54. <tr>
  55. <th>排名</th>
  56. <th>地市</th>
  57. <th>销售收入(万元)</th>
  58. <th>同比(%)</th>
  59. <th>环比(%)</th>
  60. <th>销售计划(万元)</th>
  61. <th>计划完成率(%)</th>
  62. </tr>
  63. </thead>
  64. <tbody style="opacity:0;"></tbody>
  65. </table>
  66. </div>
  67. <div class="tbl-body">
  68. <table border="0" cellspacing="0" cellpadding="0">
  69. <thead>
  70. <tr>
  71. <th>排名</th>
  72. <th>地市</th>
  73. <th>销售收入(万元)</th>
  74. <th>同比(%)</th>
  75. <th>环比(%)</th>
  76. <th>销售计划(万元)</th>
  77. <th>计划完成率(%)</th>
  78. </tr>
  79. </thead>
  80. <tbody></tbody>
  81. </table>
  82. </div>
  83. </div>
  84.  
  85. <script>
  86. var MyMarhq = '';
  87. clearInterval(MyMarhq);
  88. $('.tbl-body tbody').empty();
  89. $('.tbl-header tbody').empty();
  90. var str = '';
  91. 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"},{"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"}]
  92. $.each(Items,function (i,item) {
  93. str = '<tr>'+
  94. '<td>'+item.Ranking+'</td>'+
  95. '<td>'+item.City+'</td>'+
  96. '<td>'+(+item.SaleIncome/10000).toFixed(2)+'</td>'+
  97. '<td>'+(+item.An).toFixed(2)+'</td>'+
  98. '<td>'+(+item.Mom).toFixed(2)+'</td>'+
  99. '<td>'+(item.SalePlan/10000).toFixed(2)+'</td>'+
  100. '<td>'+(+item.PlanFinish).toFixed(2)+'</td>'+
  101. '</tr>'
  102. $('.tbl-body tbody').append(str);
  103. $('.tbl-header tbody').append(str);
  104. });
  105. if(Items.length > 10){
  106. $('.tbl-body tbody').html($('.tbl-body tbody').html()+$('.tbl-body tbody').html());
  107. $('.tbl-body').css('top','0');
  108. var tblTop = 0;
  109. var speedhq = 50; // 数值越大越慢
  110. var outerHeight = $('.tbl-body tbody').find("tr").outerHeight();
  111. function Marqueehq(){
  112. if(tblTop <= -outerHeight*Items.length){
  113. tblTop = 0;
  114. } else {
  115. tblTop -= 1;
  116. }
  117. $('.tbl-body').css('top',tblTop+'px');
  118. }
  119. MyMarhq = setInterval(Marqueehq,speedhq);
  120. 鼠标移上去取消事件
  121. $(".tbl-header tbody").hover(function (){
  122. clearInterval(MyMarhq);
  123. },function (){
  124. clearInterval(MyMarhq);
  125. MyMarhq =

猜你在找的JavaScript相关文章