2017-03-12 7 views
-1

Ich habe so lange gesucht, wie man eine JQuery auf einem div herausschieben kann. Es gab einige Dokumentationen, aber ich habe nicht die gefunden, die meinem Problem entspricht.JQuery Slide Animation + CSS

ich will einfach Text 1 zum links gleiten und zugleich Text 2 in gleiten vom rechts und auf dem Bildschirm zentriert werden.

enter image description here

Hier ist der Code^- ^.

enter code herehttps://jsfiddle.net/sru33se6/4/

Antwort

1

ich es tat mit jquery ui Umschaltschieber. laufen unter Snippet (auf eine beliebige Stelle den Text zu verschieben)

var startWithText1 = true; 
 
var first; 
 
var second; 
 
$(document).click(function() { 
 
    if (startWithText1) { 
 
    first = $("#text1"); 
 
    second = $("#text2"); 
 
    } else { 
 
    first = $("#text2"); 
 
    second = $("#text1"); 
 
    } 
 
    first.toggle("slide", { 
 
     'direction': 'left' 
 
    }, 500, 
 
    function() { 
 
     second.toggle("slide", { 
 
     'direction': 'right' 
 
     }, 500) 
 
    } 
 
); 
 
    startWithText1 = !startWithText1; 
 
});
body { 
 
    display: flex; 
 
    overflow-x: hidden; 
 
    overflow-y: hidden; 
 
} 
 

 
.content { 
 
    display: flex; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
} 
 

 
.text { 
 
    width: 200px; 
 
    height: 100px; 
 
    background-color: teal; 
 
    font-size: 72px; 
 
    color: white; 
 
    display: flex; 
 
    border: 4px solid black; 
 
    border-radius: 5px; 
 
    margin-left: 10px; 
 
} 
 

 
.text h1 { 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    margin-top: auto; 
 
    margin-bottom: auto; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
 
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" /> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css"> 
 
<link rel="stylesheet" href="../css/test.css"> 
 

 
<div id="content" class="content"> 
 
    <!--Text 1 slides smooth out to the left.--> 
 
    <div class="text"> 
 
    <h1 id="text1">Text 1</h1> 
 
    <h1 id="text2" style="display: none;">Text 2</h1> 
 
    </div> 
 
</div>

+0

Perfect! Danke für die Hilfe. Nicht ganz, was ich meinte, aber genug, dass ich es verstehe und es selbst benutze! – Insanic

2

Ich weiß nicht, ob es der bessere Weg zu gehen, aber man könnte auf diese Weise versuche ich tat:

@keyframes slide-from-center{ 
 
    from {left: 50%} 
 
    to {left: -50%} 
 
} 
 

 
@keyframes slide-from-right{ 
 
    from {left: 150%} 
 
    to {left: 50%} 
 
} 
 

 
body { 
 
    display: flex; 
 
    overflow-x: hidden; 
 
    overflow-y: hidden; 
 
} 
 

 
.content { 
 
    display: flex; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
} 
 

 
.text { 
 
    position: relative; 
 
    min-width: 200px; 
 
    min-height: 100px; 
 
    background-color: teal; 
 
    color: white; 
 
    border: 4px solid black; 
 
    border-radius: 5px; 
 
    margin-left: 10px; 
 
    overflow: hidden; 
 
} 
 

 
.text h1 { 
 
    position: absolute; 
 
    transform: translate(-50%, -50%); 
 
    font-size: 0.8rem; 
 
    width: 100%; 
 
    text-align: center; 
 
    margin: auto 0 auto 0; 
 
    top: 50%; 
 
} 
 

 
.text h1:first-of-type{ 
 
    left: 50%; 
 
    animation: slide-from-center 3s linear 1 forwards; 
 
} 
 

 
.text h1:nth-of-type(2){ 
 
    left: 150%; 
 
    animation: slide-from-right 3s linear 1 forwards; 
 
}
<html> 
 
    <head> 
 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css"> 
 
    <link rel="stylesheet" href="../css/test.css"> 
 
    </head> 
 
    <body> 
 
    <div id="content" class="content"> 
 
     <!--Text 1 slides smooth out to the left.--> 
 
     <div class="text"><h1>Text 1</h1><h1> 
 
     Text 2 
 
     </h1></div> 
 
     <!--Text 2 should slide in to the right after Text 2 slided out. Currently commented out Text 2 below--> 
 
     
 
     <!--<div class="text" style="display: hidden;"><h1>Text 2</h1></div>--> 
 
     
 
    </div> 
 
</body> 
 
</html>