2017-07-28 1 views
3

Kann mir jemand helfen, den weißen Zwischenraum während der Animation zu entfernen?Warum gibt es während der Animation zwischen zwei 'div' einen leeren Platz?

Muss ich noch ein div wrap beide div in eins hinzufügen?

Ich habe gewählt, Position-Top zu verwenden, um meine div-Animationen anzupassen, ist das das Problem verursacht und bitte schlagen Sie mir eine bessere Methode, um diese Animation zu tun, wenn überhaupt?

<!DOCTYPE HTML> 
 
<html> 
 

 
<head> 
 
    <style> 
 
     #top { 
 
      background: white; 
 
      color: white; 
 
      width: 300px; 
 
      height: 300px; 
 
      display: inline-block; 
 
      overflow: hidden; 
 
     } 
 

 
     #first { 
 
      background: blue; 
 
      transition: 0.5s; 
 
      height: 300px; 
 
      position: relative; 
 
     } 
 

 
     #second { 
 
      background: green; 
 
      transition: 0.5s; 
 
      height: 300px; 
 
      position: relative; 
 
     } 
 
    </style> 
 
    <script> 
 
     var up = true; 
 
     var down = false; 
 
     function animations() { 
 
      if (up) { 
 
       document.getElementById('first').style.top = '-300px'; 
 
       document.getElementById('second').style.top = '-300px'; 
 
       up = false; 
 
       down = true; 
 
      } 
 
      else if (down) { 
 
       document.getElementById('first').style.top = '0px'; 
 
       document.getElementById('second').style.top = '300px'; 
 
       down = false; 
 
       up = true; 
 
      } 
 
     } 
 

 
     //timer events 
 
     var startAnimations = setInterval(animations, 1000); 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <div id="top"> 
 
     <div id="first">first</div> 
 
     <div id="second">second</div> 
 
    </div> 
 
</body> 
 

 
</html>

Antwort

3

Während Ihr zweites div Element oben 0px statt 300px nach unten bewegen machen;

Ich hoffe, es hilft.

+0

Mein jsFiddle Link: https://jsfiddle.net/duog6smr/ –

+0

Vielen Dank für Ihre Hilfe ! –

+0

Anytime @MohanRavi –

3

Geben Sie keinen Stil top 300px, während zweite Div sinkt, setzen Sie es einfach 0px wie erste Div. Beide Divs sind relativ und der erste Push der zweite, so wird es nicht 300px wird es 300px + first div sein.

document.getElementById('second').style.top = '0px'; 

Hoffnung hilft,

+0

Vielen Dank für Ihre Hilfe! Ich schätze dich! –

+0

Bitte markieren Sie Ashishs Lösung als Antwort. Er antwortete zuerst. @MohanRavi – Berkay

+0

Das ist so bescheiden von dir! –

0

Seine wegen Ihrer relativen Positionierung. Verwenden Sie den angegebenen CSS:

#top { 
      background: white; 
      color: white; 
      width: 300px; 
      height: 300px; 
      display: inline-block; 
      overflow: hidden; 
      position:absolute; 
     } 

     #first { 
      background: blue; 
      transition: 0.5s; 
      height: 300px; 
      width:300px; 
      position:absolute; 
     } 

     #second { 
      margin:0px; 
      background: green; 
      transition: 0.5s; 
      height: 300px; 
      width:300px; 
      position: absolute; 
     } 
1

<!DOCTYPE HTML> 
 
<html> 
 

 
<head> 
 
    <style> 
 
     #top { 
 
      background: white; 
 
      color: white; 
 
      width: 300px; 
 
      height: 300px; 
 
      display: inline-block; 
 
      overflow: hidden; 
 
     } 
 

 
     #first { 
 
      background: blue; 
 
      transition: 0.5s; 
 
      height: 300px; 
 
      position: relative; 
 
     } 
 

 
     #second { 
 
      background: green; 
 
      transition: 0.5s; 
 
      height: 300px; 
 
      position: relative; 
 
     } 
 
    </style> 
 
    <script> 
 
     var up = true; 
 
     var down = false; 
 
     function animations() { 
 
      if (up) { 
 
             
 
       up = false; 
 
       down = true; 
 
      document.getElementById('first').style.top = '-300px'; 
 
       document.getElementById('second').style.top = '-300px'; 
 

 
      } 
 
      else if (down) { 
 
       down = false; 
 
       up = true; 
 
       document.getElementById('second').style.top = '0px'; 
 
       document.getElementById('first').style.top = '0px'; 
 
       
 
      } 
 
     } 
 

 
     //timer events 
 
     var startAnimations = setInterval(animations, 1000); 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <div id="top"> 
 
     <div id="first">first</div> 
 
     <div id="second">second</div> 
 
    </div> 
 
</body> 
 

 
</html>

+0

@Mohan Ravi überprüfen Sie es. wenn irgendwelche Zweifel mich wissen lassen. – Dhaarani

+0

Vielen Dank für Ihre Hilfe! –

0

<!DOCTYPE HTML> 
 
<html> 
 

 
<head> 
 
    <style> 
 
     #top { 
 
      background: white; 
 
      color: white; 
 
      width: 300px; 
 
      height: 300px; 
 
      display: inline-block; 
 
      overflow: hidden; 
 
     } 
 

 
     #first { 
 
      background: blue; 
 
      transition: 0.5s; 
 
      height: 300px; 
 
      position: relative; 
 
     } 
 

 
     #second { 
 
      background: green; 
 
      transition: 0.5s; 
 
      height: 300px; 
 
      position: relative; 
 
     } 
 
    </style> 
 
    <script> 
 
     var up = true; 
 
     var down = false; 
 
     function animations() { 
 
      if (up) { 
 
       document.getElementById('first').style.top = '-300px'; 
 
       document.getElementById('second').style.top = '-300px'; 
 
       up = false; 
 
       down = true; 
 
      } 
 
      else if (down) { 
 
       document.getElementById('first').style.top = '0px'; 
 
       document.getElementById('second').style.top = '0'; 
 
       down = false; 
 
       up = true; 
 
      } 
 
     } 
 

 
     //timer events 
 
     var startAnimations = setInterval(animations, 1000); 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <div id="top"> 
 
     <div id="first">first</div> 
 
     <div id="second">second</div> 
 
    </div> 
 
</body> 
 

 
</html>

Verwandte Themen