2017-12-28 1 views
0

Ich habe einen div und einen Knopf.Wie kann man eine Slide-Up-Animation auf einem dynamischen Element hinzufügen?

mit einem Klick auf die Schaltfläche ich bin ein anderes div anhängen. nach dem Anhängen der zweiten div möchte ich Folie bis zum ersten div hinzufügen und rutschen mit jquery-Animationsmethode auf den zweiten.

<div class="main"> 
    <div class="user"> 
     <div class="closed_div"> 
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
     </div> 
    </div> 
    <button class="addDiv">Add open Div</button> 
</div> 

Jetzt bin ich Anfügen der neuen div mit folgenden Code

$(document).ready(function(){ 
      $('.addDiv').click(function(){ 
       var html = '<div class="open_div">Lorem Ipsum is simply dummy text of the printing and \n\ 
         typesetting industry. Lorem Ipsum has been the industrys \n\ 
         standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type \n\ 
         specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining \n\ 
         essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, \n\ 
         and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>'; 
       $('.user').append(html); 
      }); 
     }); 

und Animation mit Code folgenden

$(".closed_div").animate({height:'100%','opacity':1},1500); 
$(".open_div").animate({height:0},1500); 

Aber seine nicht funktioniert well.Can Sie mir vorschlagen, was ich tue falsch.

Thnakyou.

+0

Haben Sie versucht, slideUp() und slidedown() anstelle von belebten() setzen? Sieht so aus, als hättest du ein "." vor open_div in $ ("open_div"). animate ({height: 0}, 1500); – JasonB

+0

Ja, ich habe slideUp und slideDown ausprobiert. und ich habe einen fraglichen Punkt verpasst. nicht in echt –

+0

Wenn du das neue div anfügst, möchtest du das alte div durch slideup verstecken und das neue div mit slidedown anzeigen. Recht? –

Antwort

3

Hier ist eine Implementierung, die die Funktionen jQuery slideUp und slideDown verwendet. Ich entferne die Divs, die hochgeschoben sind. Höhe: 100% ist manchmal das Problem mit der Animation.

let counter = 0; 
 

 
$('.addDiv').click(function() { 
 
    var html = $('<div class="closed_div">Lorem Ipsum is simply dummy text of the printing and \n\ 
 
         typesetting industry. Lorem Ipsum has been the industrys \n\ 
 
         standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type \n\ 
 
         specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining \n\ 
 
         essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, \n\ 
 
         and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.' + counter++ + '</div>'); 
 
    $('.user div').slideUp(300, function() { 
 
    $(this).remove(); 
 
    });    
 
    $('.user').append(html); 
 
    html.slideDown(); 
 
    
 
});
.closed_div { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="main"> 
 
    <div class="user"> 
 
    <div class="open_div" style="display:none;"> 
 
     Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has 
 
     survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop 
 
     publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
 
    </div> 
 
    </div> 
 
    <button class="addDiv">Add open Div</button> 
 
</div>

1

$(".closed_div").animate({height:'100%','opacity':1},1500); 
 
    $(function() { 
 
    $('.addDiv').click(function(){ 
 
     var elem = $(this); 
 
     $('.user').after('<div class="open_div none">Lorem Ipsum is simply dummy text of the printing and \n\ 
 
typesetting industry. Lorem Ipsum has been the industrys \n\ 
 
standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type \n\ 
 
specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining \n\ 
 
essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, \n\ 
 
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>').next('div').slideDown('slow'); 
 
    }); 
 
});
.none{ 
 
    display:none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="main"> 
 
    <div class="user"> 
 
     <div class="closed_div"> 
 
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
 
     </div> 
 
    </div> 
 
    <button class="addDiv">Add open Div</button> 
 
</div>

1

Sie benötigen Anzeige keine Eigenschaft auf den neu open_div beigefügten einzustellen, fügen Sie closed_div Klasse mit der zuletzt geöffneten div. Verstecken Sie dann das letzte closed_div mit slideUp. Innerhalb des Rückrufs des SlideUp, slideDown der letzte open_div.

Und in Ihrem CSS, müssen Sie display: none Eigenschaft open_div

$(document).ready(function(){ 
 
      $('.addDiv').click(function(){ 
 
       var html = '<div class="open_div">Lorem Ipsum is simply dummy text of the printing and \n\ 
 
         typesetting industry. Lorem Ipsum has been the industrys \n\ 
 
         standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type \n\ 
 
         specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining \n\ 
 
         essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, \n\ 
 
         and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>'; 
 
       $('.open_div:last').addClass('closed_div'); 
 
       $('.user').append(html); 
 
       $('.closed_div:last').slideUp("slow", function() { 
 
       \t $('.open_div:last').slideDown("slow"); 
 
       }); 
 
      }); 
 
     });
.open_div { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="main"> 
 
    <div class="user"> 
 
     <div class="closed_div"> 
 
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. 
 
     </div> 
 
    </div> 
 
    <button class="addDiv">Add open Div</button> 
 
</div>

Verwandte Themen