简体   繁体   English

如何在JavaScript中显示具有滑行效果的无显示行?

[英]How To Show a Display None Row With Slide Down Effect in Javascript?

i have a table with number of rows that some of rows are hidden and only visisble when "show" button clicked. 我有一个带有行数的表,其中某些行是隐藏的,只有单击“显示”按钮时才可见。 my question is how can i display block my row with effect slide down? 我的问题是如何显示效果下降的行? Here is My snippet : 这是我的片段:

 function toggleRow(e){ var subRow = e.parentNode.parentNode.nextElementSibling; subRow.style.display = subRow.style.display === 'none' ? 'table-row' : 'none'; } 
 .subRow { background-color: #CFCFCF; display:none; } 
 <table style="width:50%" border="1"> <caption>Test Table</caption> <thead> <tr align="center" class="parentRow"> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> <th>Column 4</th> <th>Column 5</th> </tr> </thead> <tbody> <tr class="parentRow"> <td><a href="JavaScript:Void(0);" onclick="toggleRow(this);">SHOW</a></td> <td>test cell</td> <td>test cell</td> <td>test cell</td> <td>test cell</td> </tr> <tr class="subRow"> <td colspan="5"><p>Lorem ipsum dolor sit amet...</p></td> </tr> <tr class="parentRow"> <td><a href="JavaScript:Void(0);" onclick="toggleRow(this);">SHOW</a></td> <td>test cell</td> <td>test cell</td> <td>test cell</td> <td>test cell</td> </tr> <tr class="subRow"> <td colspan="5"><p>Lorem ipsum dolor sit amet...</p></td> </tr> </tbody> </table> 

I recommend to toggle a class instead. 我建议改为切换课程。

I also changed href="JavaScript:Void(0);" 我还更改了href="JavaScript:Void(0);" to href="#" to avoid script error. to href="#"以避免脚本错误。

As you can't animate a table-row I added the animation to an extra div , here used with max-height 由于您无法为table-row设置动画,因此我将动画添加到了额外的div ,此处与max-height

To be noted, animation of height is difficult and the below trick, where I use max-height , the value need to be set so it always is bigger than the content. 需要注意的是,高度动画很困难,下面是技巧,我使用max-height ,需要设置该值,使其始终大于内容。 If this is not doable, a script is needed to grab the content height prior to kicking of the transition 如果这样做不可行,则需要一个脚本来在过渡开始之前获取内容的高度

 function toggleRow(e){ var subRow = e.parentNode.parentNode.nextElementSibling; subRow.classList.toggle('RowShow'); return false; } 
 .subRow td { background-color: #CFCFCF; padding: 0; border: 0; } .subRow div { max-height: 0; transition: max-height 0.5s; overflow: hidden; } .subRow.RowShow div { max-height: 100px; transition: max-height 1s; } 
 <table style="width:50%" border="1"> <caption>Test Table</caption> <thead> <tr align="center" class="parentRow"> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> <th>Column 4</th> <th>Column 5</th> </tr> </thead> <tbody> <tr class="parentRow"> <td><a href="#" onclick="return toggleRow(this);">SHOW</a></td> <td>test cell</td> <td>test cell</td> <td>test cell</td> <td>test cell</td> </tr> <tr class="subRow"> <td colspan="5"><div><p>Lorem ipsum dolor sit amet...</p></div></td> </tr> <tr class="parentRow"> <td><a href="#" onclick="return toggleRow(this);">SHOW</a></td> <td>test cell</td> <td>test cell</td> <td>test cell</td> <td>test cell</td> </tr> <tr class="subRow"> <td colspan="5"><div><p>Lorem ipsum dolor sit amet...</p></div></td> </tr> </tbody> </table> 

Void should be lowercase but I use preventDefault instead Void应该是小写字母,但我改用preventDefault

You are using tables so there is an issue animating the table row. 您正在使用表格,因此表格行存在动画问题。

slideToggle in table row 表格行中的slideToggle

I have implemented that fix 我已经实施了该修复程序

 $(function() { $(".show").on("click", function(e) { e.preventDefault(); $(this).closest("tr").next().find(".content").slideToggle(); }); }); 
 .subRow { padding:0; background-color: #CFCFCF; } .content { background-color: #CFCFCF; display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <table style="width:50%" border="1"> <caption>Test Table</caption> <thead> <tr align="center" class="parentRow"> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> <th>Column 4</th> <th>Column 5</th> </tr> </thead> <tbody> <tr class="parentRow"> <td><a href="#" class="show">SHOW</a> </td> <td>test cell</td> <td>test cell</td> <td>test cell</td> <td>test cell</td> </tr> <tr class="subRow"> <td colspan="5"> <div class="content"><p>Lorem ipsum dolor sit amet...</p></div> </td> </tr> <tr class="parentRow"> <td><a href="#" class="show">SHOW</a> </td> <td>test cell</td> <td>test cell</td> <td>test cell</td> <td>test cell</td> </tr> <tr class="subRow"> <td colspan="5"> <div class="content"><p>Lorem ipsum dolor sit amet...</p></div> </td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM