简体   繁体   English

使用JavaScript的SlideDown()无法正常工作

[英]SlideDown() using JavaScript not working as intended

Thanks to @mplungjan for helping me complete my first query which can be found here: Remove div with button click using JavaScript 感谢@mplungjan帮助我完成了我的第一个查询,该查询可以在这里找到: 使用JavaScript通过单击按钮删除div

The code works as intended however when we tried to get the slideDown() function to work it looks a bit... laggy and then just pops up without the animation being completed as intended. 代码按预期工作,但是当我们尝试使slideDown()函数正常工作时,它看起来有点...松懈,然后弹出而动画没有按预期完成。

Would like some support to see how can this be fixed. 希望获得一些支持,以了解如何解决此问题。

Find below working code: 查找以下工作代码:

 $(function() { var $original = $('#ValuWrapper'), $crossButton = $('#cross'), $content = $("#content"); $content.on("click", ".cross", function() { if ($(this).is("#cross")) return false; var $cross = $(this); $(this).next().slideUp(400, function() { $(this).remove(); $cross.remove(); }); }); $("#repeat").on("click", function() { $content.append($crossButton.clone(true).removeAttr("id")); $content.append( $original.clone(true) .hide() // if sliding .attr("id",$original.attr("id")+$content.find("button.cross").length) .slideDown("slow") // does not slide much so remove if you do not like it ); }); }); 
 #content { height:100%} #cross { display: none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div id="content"> <button type="button" class="buttonImgTop cross" id="cross">X</button> <div id="ValuWrapper"> ...content comes here... <br/> </div> </div> <button type="button" class="buttonImg" id="repeat">Add</button> 

When you clone the $original you should reset its height so jQuery knows what height its got to animate to. 克隆$original ,应重置其高度,以便jQuery知道其要设置的高度。

EG: $original.css('height', $(this).height()) EG: $original.css('height', $(this).height())

See demo: 观看演示:

 $(function() { var $original = $('#ValuWrapper'), $crossButton = $('#cross'), $content = $("#content"); $content.on("click", ".cross", function() { if ($(this).is("#cross")) return false; var $cross = $(this); $(this).next().slideUp(400, function() { $(this).remove(); $cross.remove(); }); }); $("#repeat").on("click", function() { $content.append($crossButton.clone(true).removeAttr("id")); $content.append( $original.clone(true) .css('height', $(this).height())//set height .hide() .attr("id",$original.attr("id")+$content.find("button.cross").length) .slideDown("slow") ); }); }); 
 #content { height:100%;} #cross { display: none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div id="content"> <button type="button" class="buttonImgTop cross" id="cross">X</button> <div id="ValuWrapper"> ...content comes here... </div> </div> <button type="button" class="buttonImg" id="repeat">Add</button> 

Kindly use below updated code for animation. 请使用以下更新的动画代码。

$("#repeat").on("click", function() {
$content.append($crossButton.clone(true).removeAttr("id"));
$content.append(
  $original.clone(true)
   // if sliding
  .attr("id",$original.attr("id")+$content.find("button.cross").length).hide());
   // does not slide much so remove if you do not like it
   $("#"+$original.attr("id")+$content.find("button.cross").length).slideDown("slow");//change 

}); });

and remove #content { height:100%;}

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

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