Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slide two divs simultaneously

I'm trying to have two sections in the same page, a login-section and an app-section.

The idea is onLoginSubmit() to slide out to the left the login-section and slide in from the right the app-section.

Consider this HTML:

<section id="login-section">

</section>
<section id="app-section" style="display: none;">

</section>

And the JS I am using:

$('#login-section').toggle("slide", { direction: "left" }, 500);
$('#app-section').toggle("slide", { direction: "right" }, 500);

What happens is that the login-section does slide out left but the app-section only gets visible after the transition has ended, so I get the default background when the login-section is leaving the screen.

I am using jQuery and jQuery UI. Any help is appreciated.

like image 474
hpinhal Avatar asked Aug 12 '16 10:08

hpinhal


1 Answers

var $div1 = $('#login-section'), 
    $div2 = $('#app-section'),
    currentDiv = 'div1',
    $button = $('button');

$div2.hide();
$button.text('Toggle ' + currentDiv);

$(document).on('click', 'button', function (evt) {
    $div1.toggle('slide', {direction: 'right'}, 'slow');
    $div2.toggle('slide', {direction: 'left'}, 'slow');
    
    currentDiv = (currentDiv === 'div1') ? 'div2' : 'div1';
    $button.text('Toggle ' + currentDiv);
});
#login-section{
    width: 500px;
    height: 100px;
    background-color: green;
    position: absolute;
    top: 0px;
    left: 0px;
}

#app-section{
    width: 500px;
    height: 100px;
    background-color: blue;
    position: absolute;
    top: 0px;
    left: 0px;
}

.clear {
    clear: both;
}

.container {
    width: 800px;
    position: relative;
    left: 100px;
    height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.1/jquery-ui.min.js"></script>
<p>some content here </p>
<div class="container">
    <div id="login-section"></div>
    <div id="app-section"></div>
</div>
<button>Toggle</button>
like image 141
TechnicalKalsa Avatar answered Sep 19 '22 23:09

TechnicalKalsa