Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

jquery - Slide cloned element down when it appears

I have the following code that clones a div and it's content on clicking a button. The clone part works but when it outputs the cloned div I want it to slide down.

This is what I've tried but the cloned div just appears rather than sliding down. Where am I going wrong?

$(".add-prod-group").on('click', function(){
  var ele = $(this).next().next().next('.group-add-prod').clone(true);
  $(this).next().next().next('.group-add-prod').after(ele).slideDown();
});

HTML:

<a href="#" class="add-prod-group">+</a>
<p class="text-center">Add products to your order</p>
<hr />
<div class="group-add-prod">
  <p>contents</p>
</div>

https://jsfiddle.net/dx29Lfo8/

question from:https://stackoverflow.com/questions/65846400/slide-cloned-element-down-when-it-appears

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can make newly added div as display:none then after adding it to your DOM you can use $(".group-add-prod:last").slideDown(); to make that div visible.

Demo Code :

$(".add-prod-group").on('click', function() {
  //get first div clone it
  var ele = $(this).siblings('.group-add-prod:first').clone(true);
  //add class none
  $(ele).addClass("none")
  //add it after last div
  $(this).siblings('.group-add-prod:last').after(ele)
  $(".group-add-prod:last").slideDown(); //slide it
});
.none {
  display: none
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="add-prod-group">+</a>
<p class="text-center">Add products to your order</p>
<hr />
<div class="group-add-prod">
  <p>contents</p>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...