2
I set up a table with scroll horizontal and vertical from this original http://jsfiddle.net/k5k7vtwu/:
document.querySelector('.ux-data-table').onscroll = function (e) {
// called when the window is scrolled.
var topOfDiv = Math.max(document.querySelector(".ux-data-table").scrollTop - 2, 0);
document.getElementsByTagName('thead')[0].style = "top:" + topOfDiv + "px;";
}
.ux-data-table {
width: auto;
max-width: 100%;
height: 200px;
overflow: auto;
float:left;
}
.ux-data-table1 {
width: auto;
max-width: 100%;
height: 200px;
overflow: auto;
float:left;
}
/* Prevents the header from overflowing the scrollbars */
.ux-data-table-inner {
position: relative;
}
.ux-data-table-inner1 {
position: relative;
}
.ux-data-table table {
width: 100%;
}
.ux-data-table1 table {
width: 100%;
}
.ux-data-table table,
.ux-data-table th,
.ux-data-table td {
background-color: black;
color: white;
border: 1px solid black;
width: 50px;
min-width: 50px;
max-width: 50px;
}
.ux-data-table1 table,
.ux-data-table1 th,
.ux-data-table1 td {
background-color: black;
color: white;
border: 1px solid black;
width: 50px;
min-width: 50px;
max-width: 50px;
}
.ux-data-table thead {
position: absolute;
}
.ux-data-table1 thead {
position: absolute;
}
.ux-data-table tbody {
margin-top: 20px;
display:block;
}
.ux-data-table1 tbody {
margin-top: 20px;
display:block;
}
.ux-data-table td {
background-color: white;
color: black;
border: 1px solid black;
}
.ux-data-table1 td {
background-color: white;
color: black;
border: 1px solid black;
}
<h4>
Tabela 1
</h4>
<div class='ux-data-table'>
<div class='ux-data-table-inner'>
<table>
<thead>
<tr>
<th>COLOCAÇÃO</th>
<th>Nº</th>
<th>NOME</th>
<th>CÓD. FAIXA ETÁRIA</th>
<th>COL. FAIXA ETÁRIA</th>
<th>TEMPO SWIM</th>
<th>T1</th>
<th>TEMPO BIKE</th>
<th>T2</th>
<th>TEMPO RUN</th>
<th>TEMPO TOTAL</th>
<th>EQUIPE</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>12</td>
<td>MARCUS VINICIUS FERNANDES</td>
<td>M3034</td>
<td>–</td>
<td>00:11:32</td>
<td>00:00:29</td>
<td>00:45:36</td>
<td>00:00:29</td>
<td>00:22:01</td>
<td>01:20:07</td>
<td>MF ASSESSORIA ESPORTIVA</td>
</tr>
<tr>
<td>2</td>
<td>16</td>
<td>FELIPE JOSÉ MOLETTA</td>
<td>M3034</td>
<td>–</td>
<td>00:13:37</td>
<td>00:00:22</td>
<td>00:44:34</td>
<td>00:00:25</td>
<td>00:21:29</td>
<td>01:20:27</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>3</td>
<td>17</td>
<td>FERNANDO LUNARDELLI TOLDI</td>
<td>M3034</td>
<td>–</td>
<td>00:12:20</td>
<td>00:00:18</td>
<td>00:47:46</td>
<td>00:00:17</td>
<td>00:21:58</td>
<td>01:22:39</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>4</td>
<td>20</td>
<td>RAFAEL GOMES JURITI FERREIRA</td>
<td>M3539</td>
<td>–</td>
<td>00:14:55</td>
<td>00:00:15</td>
<td>00:45:58</td>
<td>00:00:26</td>
<td>00:24:54</td>
<td>01:26:28</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>5</td>
<td>13</td>
<td>RAUL FURTADO</td>
<td>M4044</td>
<td>–</td>
<td>00:13:34</td>
<td>00:00:27</td>
<td>00:46:59</td>
<td>00:00:30</td>
<td>00:25:08</td>
<td>01:26:38</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>6</td>
<td>18</td>
<td>HUGO AMARAL HORTA BARBOSA</td>
<td>M3034</td>
<td>–</td>
<td>00:13:26</td>
<td>00:00:40</td>
<td>00:48:38</td>
<td>00:00:26</td>
<td>00:25:01</td>
<td>01:28:11</td>
<td>FIBRATECH</td>
</tr>
<tr>
<td>7</td>
<td>21</td>
<td>JUSCELINO ADEODATO DE MIRANDA VASCO</td>
<td>M3539</td>
<td>–</td>
<td>00:14:40</td>
<td>00:00:25</td>
<td>00:50:36</td>
<td>00:00:22</td>
<td>00:25:41</td>
<td>01:31:44</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>8</td>
<td>23</td>
<td>GUSTAVO SLAIB CRUZ PEREIRA</td>
<td>M3539</td>
<td>–</td>
<td>00:14:55</td>
<td>00:00:25</td>
<td>00:51:53</td>
<td>00:00:30</td>
<td>00:26:03</td>
<td>01:33:46</td>
<td>GP COACHING</td>
</tr>
<tr>
<td>9</td>
<td>24</td>
<td>PETER PICHNOFF</td>
<td>M3539</td>
<td>–</td>
<td>00:14:55</td>
<td>00:00:19</td>
<td>00:53:30</td>
<td>00:00:43</td>
<td>00:26:30</td>
<td>01:35:57</td>
<td>GP COACHING</td>
</tr>
</tbody>
</table>
</div>
</div>
<br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/>
<h4>
Tabela 2
</h4>
<div class='ux-data-table1'>
<div class='ux-data-table-inner1'>
<table>
<thead>
<tr>
<th>COLOCAÇÃO</th>
<th>Nº</th>
<th>NOME</th>
<th>CÓD. FAIXA ETÁRIA</th>
<th>COL. FAIXA ETÁRIA</th>
<th>TEMPO SWIM</th>
<th>T1</th>
<th>TEMPO BIKE</th>
<th>T2</th>
<th>TEMPO RUN</th>
<th>TEMPO TOTAL</th>
<th>EQUIPE</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>12</td>
<td>MARCUS VINICIUS FERNANDES</td>
<td>M3034</td>
<td>–</td>
<td>00:11:32</td>
<td>00:00:29</td>
<td>00:45:36</td>
<td>00:00:29</td>
<td>00:22:01</td>
<td>01:20:07</td>
<td>MF ASSESSORIA ESPORTIVA</td>
</tr>
<tr>
<td>2</td>
<td>16</td>
<td>FELIPE JOSÉ MOLETTA</td>
<td>M3034</td>
<td>–</td>
<td>00:13:37</td>
<td>00:00:22</td>
<td>00:44:34</td>
<td>00:00:25</td>
<td>00:21:29</td>
<td>01:20:27</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>3</td>
<td>17</td>
<td>FERNANDO LUNARDELLI TOLDI</td>
<td>M3034</td>
<td>–</td>
<td>00:12:20</td>
<td>00:00:18</td>
<td>00:47:46</td>
<td>00:00:17</td>
<td>00:21:58</td>
<td>01:22:39</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>4</td>
<td>20</td>
<td>RAFAEL GOMES JURITI FERREIRA</td>
<td>M3539</td>
<td>–</td>
<td>00:14:55</td>
<td>00:00:15</td>
<td>00:45:58</td>
<td>00:00:26</td>
<td>00:24:54</td>
<td>01:26:28</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>5</td>
<td>13</td>
<td>RAUL FURTADO</td>
<td>M4044</td>
<td>–</td>
<td>00:13:34</td>
<td>00:00:27</td>
<td>00:46:59</td>
<td>00:00:30</td>
<td>00:25:08</td>
<td>01:26:38</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>6</td>
<td>18</td>
<td>HUGO AMARAL HORTA BARBOSA</td>
<td>M3034</td>
<td>–</td>
<td>00:13:26</td>
<td>00:00:40</td>
<td>00:48:38</td>
<td>00:00:26</td>
<td>00:25:01</td>
<td>01:28:11</td>
<td>FIBRATECH</td>
</tr>
<tr>
<td>7</td>
<td>21</td>
<td>JUSCELINO ADEODATO DE MIRANDA VASCO</td>
<td>M3539</td>
<td>–</td>
<td>00:14:40</td>
<td>00:00:25</td>
<td>00:50:36</td>
<td>00:00:22</td>
<td>00:25:41</td>
<td>01:31:44</td>
<td>(nenhuma)</td>
</tr>
<tr>
<td>8</td>
<td>23</td>
<td>GUSTAVO SLAIB CRUZ PEREIRA</td>
<td>M3539</td>
<td>–</td>
<td>00:14:55</td>
<td>00:00:25</td>
<td>00:51:53</td>
<td>00:00:30</td>
<td>00:26:03</td>
<td>01:33:46</td>
<td>GP COACHING</td>
</tr>
<tr>
<td>9</td>
<td>24</td>
<td>PETER PICHNOFF</td>
<td>M3539</td>
<td>–</td>
<td>00:14:55</td>
<td>00:00:19</td>
<td>00:53:30</td>
<td>00:00:43</td>
<td>00:26:30</td>
<td>01:35:57</td>
<td>GP COACHING</td>
</tr>
</tbody>
</table>
</div>
</div>
Since I need several different tables on the same page, I duplicated the original table, changed the CSS and JS styles that are being used, changing the Ids and classes, but still the top of the tables (thead) are not fixed in the vertical scrolling as it is in the first one (Table 1).
I thought doubling the function and changing the classes and Ids would work, but it didn’t roll.
Could someone tell me what I need to fix so that the scroll the tables function equally?
Cara vc use this JS only to leave this fixed thead? Da para deixar ela parada sem precisar de JS, vai funcionar o Chrome, Firefox, Edge e Safari. If you’re interested I can make a model
– hugocsl
@hugocsl I made a solution following his line of reasoning. But I didn’t dare just do it with CSS. That’s on you! ;D
– LipESprY
Dear @Lipespry and hugocsl, thank you so much for your answers! I ended up using this example with JS because when I tried to lock the table header only with CSS, I mocked the horizontal scroll.
– Manoel Vilhena Saldanha
JS might even work. But CSS, in the end, turns out to be a cleaner solution! #off don’t underestimate our friend @hugocsl who "plays" with css!
– LipESprY
@Lipespry haha I will post the answer only with CSS without bugging any scroll!
– hugocsl
@hugocsl, surely a CSS expert would take that literally. It’s not my case, rs although I manage to manage well. Using JS can harm me in terms of browser compatibility or even responsiveness?
– Manoel Vilhena Saldanha
I haven’t had a compatibility problem with Javascript codes yet. The only problem I’ve had is with Internet Explorer. But it doesn’t even count as a browser. kkk. About responsiveness: it goes beyond Javascript. The basics are done via styling. ;D
– LipESprY
@Manoelvilhenasaldanha for her to be responsive just put the width of the div and the table of my answer in %
– hugocsl
show @hugocsl, I will work on her responsiveness!!! Thank you so much!!
– Manoel Vilhena Saldanha