[英]Animation from Javascript in table doesn't work. How to fix?
The animations on 1-5 lines in the table works.表中 1-5 行的动画有效。 On the 6 line doesn't.
6号线上没有。 You can see it here in action .
你可以在这里看到它的实际效果。
If you remove <td>Ff</td>
, the animations work for every line.如果删除
<td>Ff</td>
,则动画适用于每一行。
var twoColComp = { init: function (){ var tables = document.getElementsByTagName('table'); for(var i = 0; i < tables.length; i++) { if (new RegExp('(^| )two-column-comp( |$)', 'gi').test(tables[i].className)){ return; } var h = tables[i].clientHeight, t = tables[i].getBoundingClientRect().top, wT = window.pageYOffset || document.documentElement.scrollTop, wH = window.innerHeight; if(wT + wH > t + h/2){ this.make(tables[i]); } } }, make : function(el){ var rows = el.getElementsByTagName('tr'), vals = [], max, percent; for(var x = 6; x < rows.length; x++) { var cells = rows[x].getElementsByTagName('td'); for(var y = 1; y < cells.length; y++){ vals.push(parseInt(cells[y].innerHTML, 10)); } } max = Math.max.apply( Math, vals ); percent = 100/max; for(x = 0; x < rows.length; x++) { var cells = rows[x].getElementsByTagName('td'); for(var y = 1; y < cells.length; y++){ var currNum = parseInt(cells[y].innerHTML, 10); cells[y].style.backgroundSize = percent * currNum + "% 100%"; cells[y].style.transitionDelay = x/20 + "s"; } } el.className =+ " two-column-comp" } } window.onload = function(){ twoColComp.init(); } window.onscroll = function(){ twoColComp.init(); }
body { font-family: sans-serif; max-width: 60em; margin: auto; padding: 1em; } table { width: 100%; background: #eee; padding: 1em; margin: 1em auto; box-sizing: border-box; border: 1px solid #ccc; } th { font-size: 1.2em } td { font-weight: bold; border-bottom: 1px solid #fbfbfb; width: 20%; padding: .5em .25em; background-size: 0% 100%; background-repeat: no-repeat; -webkit-transition: all .75s ease-out; -moz-transition: all .75s ease-out; transition: all .75s ease-out; } td:nth-child(2) { width: 40%; color: white; text-shadow: 1px 2px #222; text-align: right; background-image: -webkit-linear-gradient(to left, #e74c3c, #e74c3c); background-image: -moz-linear-gradient(to left, #e74c3c, #e74c3c); background-image: linear-gradient(to left, #e74c3c, #e74c3c); background-position: right top; } td:nth-child(3) { width: 40%; color: white; text-shadow: 1px 2px #222; background-image: -webkit-linear-gradient(right, #3498db, #3498db); background-image: -moz-linear-gradient(right, #3498db, #3498db); background-image: linear-gradient(right, #3498db, #3498db); background-position: left top; }
<table> <tr> <th>Name</th> <th>Up</th> <th>Down</th> <th>Name</th> </tr> <tr> <td>A</td> <td>700</td> <td>170</td> <td>Aa</td> </tr> <tr> <td>B</td> <td>435</td> <td>100</td> <td>Bb</td> </tr> <tr> <td>C</td> <td>500</td> <td>175</td> <td>Cc</td> </tr> <tr> <td>D</td> <td>350</td> <td>170</td> <td>Dd</td> </tr> <tr> <td>E</td> <td>1980</td> <td>350</td> <td>Ee</td> </tr> <tr> <td>F</td> <td>2000</td> <td>180</td> <td>Ff</td> </tr> </table>
On line 137, you could add the ~~
(docs) to convert the NaN
value obtained from parseInt(cells[y].innerHTML, 10)
from the last cell of each tr
into the value 0
.在第 137 行,您可以添加
~~
(docs)以将从每个tr
的最后一个单元格的parseInt(cells[y].innerHTML, 10)
获得的NaN
值转换为值0
。
For example, on Ff
, the value of parseInt
would be NaN
.例如,在
Ff
, parseInt
的值将是NaN
。 Adding ~~
would convert it to 0
.添加
~~
会将其转换为0
。
In this example (line 141), the NaN
value would be the max value, therefore it would not apply the backgroundSize
correctly on line 148.在此示例中(第 141 行),
NaN
值将是最大值,因此它不会在第 148 行正确应用backgroundSize
。
console.log(Math.max.apply( Math, [2000, 100, NaN] )); console.log(Math.max.apply( Math, [2000, 100, 0] ));
var twoColComp = { init: function (){ var tables = document.getElementsByTagName('table'); for(var i = 0; i < tables.length; i++) { if (new RegExp('(^| )two-column-comp( |$)', 'gi').test(tables[i].className)){ return; } var h = tables[i].clientHeight, t = tables[i].getBoundingClientRect().top, wT = window.pageYOffset || document.documentElement.scrollTop, wH = window.innerHeight; if(wT + wH > t + h/2){ this.make(tables[i]); } } }, make : function(el){ var rows = el.getElementsByTagName('tr'), vals = [], max, percent; for(var x = 0; x < rows.length; x++) { var cells = rows[x].getElementsByTagName('td'); for(var y = 0; y < cells.length; y++){ vals.push(~~parseInt(cells[y].innerHTML, 10)); } } max = Math.max.apply( Math, vals ); percent = 100/max; for(x = 0; x < rows.length; x++) { var cells = rows[x].getElementsByTagName('td'); for(var y = 0; y < cells.length; y++){ var currNum = parseInt(cells[y].innerHTML, 10); cells[y].style.backgroundSize = percent * currNum + "% 100%"; cells[y].style.transitionDelay = x/20 + "s"; } } el.className =+ " two-column-comp" } } window.onload = function(){ twoColComp.init(); } window.onscroll = function(){ twoColComp.init(); }
body { font-family: sans-serif; max-width: 60em; margin: auto; padding: 1em; } table { width: 100%; background: #eee; padding: 1em; margin: 1em auto; box-sizing: border-box; border: 1px solid #ccc; } th { font-size: 1.2em } td { font-weight: bold; border-bottom: 1px solid #fbfbfb; width: 20%; padding: .5em .25em; background-size: 0% 100%; background-repeat: no-repeat; -webkit-transition: all .75s ease-out; -moz-transition: all .75s ease-out; transition: all .75s ease-out; } td:nth-child(2) { width: 40%; color: white; text-shadow: 1px 2px #222; text-align: right; background-image: -webkit-linear-gradient(to left, #e74c3c, #e74c3c); background-image: -moz-linear-gradient(to left, #e74c3c, #e74c3c); background-image: linear-gradient(to left, #e74c3c, #e74c3c); background-position: right top; } td:nth-child(3) { width: 40%; color: white; text-shadow: 1px 2px #222; background-image: -webkit-linear-gradient(right, #3498db, #3498db); background-image: -moz-linear-gradient(right, #3498db, #3498db); background-image: linear-gradient(right, #3498db, #3498db); background-position: left top; }
<table> <tr> <th>Name</th> <th>Up</th> <th>Down</th> <th>Name</th> </tr> <tr> <td>A</td> <td>700</td> <td>170</td> <td>Aa</td> </tr> <tr> <td>B</td> <td>435</td> <td>100</td> <td>Bb</td> </tr> <tr> <td>C</td> <td>500</td> <td>175</td> <td>Cc</td> </tr> <tr> <td>D</td> <td>350</td> <td>170</td> <td>Dd</td> </tr> <tr> <td>E</td> <td>1980</td> <td>350</td> <td>Ee</td> </tr> <tr> <td>F</td> <td>2000</td> <td>180</td> <td>Ff</td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.