jeudi 19 février 2015

Swap Divs On Click / JavaScript syntax


I know nothing of JavaScript... I'm close to what I need to achieve but there are few bugs that I think are caused by the poor quality of my JavaScript syntax.


The Goal: I have a main div with three links, each of which should replace onclick the original div by a new correspondent one. The user should be able to close the new content and return to the main one.


Problem 1: "display: none;" should take effect only after animation.


Problem 2: When you're back to the original main content div if you click the same link again it want display the correspondent div a second time. Animations also fail to take effect.



$(function() {
$("#opens-content-a").click(function() {
$(".content-main").toggleClass("slideOut");
$(".content-main").css("display","none");
$(".content-a").css("display","block");
$(".content-a").toggleClass("slideIn");
});
});

$(function() {
$("#closes-content-a").click(function() {
$(".content-a").toggleClass("slideOut");
$(".content-a").css("display","none");
$(".content-main").css("display","block");
$(".content-main").toggleClass("slideIn");
});
});

$(function() {
$("#opens-content-b").click(function() {
$(".content-main").toggleClass("slideOut");
$(".content-main").css("display","none");
$(".content-b").css("display","block");
$(".content-b").toggleClass("slideIn");
});
});

$(function() {
$("#closes-content-b").click(function() {
$(".content-b").toggleClass("slideOut");
$(".content-b").css("display","none");
$(".content-main").css("display","block");
$(".content-main").toggleClass("slideIn");
});
});

$(function() {
$("#opens-content-c").click(function() {
$(".content-main").toggleClass("slideOut");
$(".content-main").css("display","none");
$(".content-c").css("display","block");
$(".content-c").toggleClass("slideIn");
});
});

$(function() {
$("#closes-content-c").click(function() {
$(".content-c").toggleClass("slideOut");
$(".content-c").css("display","none");
$(".content-main").css("display","block");
$(".content-main").toggleClass("slideIn");
});
});


HTML



<section>
<div class="container">

<div class="content-main">
<div id="column-left">
<h1 id="opens-content-a">OPEN A</h1>Some Content</div>
<div id="column-center">
<h1 id="opens-content-b">OPEN B</h1>Some Content</div>
<div id="column-right">
<h1 id="opens-content-c">OPEN C</h1>Some Content</div>
</div>

<div class="content-a">
<div id="closes-content-a">X</div>
Some Content
</div>

<div class="content-b">
<div id="closes-content-b">X</div>
Some Content
</div>

<div class="content-c">
<div id="closes-content-c">X</div>
Some Content
</div>

</div>


CSS



body {
background-color: #24354c;
text-align: center;
margin: 0;
padding: 0;
font-family: "arial", sans-serif;
}

p {
text-align: left;
padding: 20px 20px;
}

h1 {
color: #FF0000;
text-align: left;
padding: 0px 20px;
}

section {
display: table;
position: relative;
min-height: 100%;
width: 100%;
background: #24354c;
}

.container {
display: table-cell;
vertical-align: middle;
}

.content-main {
display: inline-block;
margin: 0 auto;
overflow:auto;
width: 80%;
max-width: 1100px;
color: #fff;
background: #17202f;
}
#column-left { float: left; width: 30%; }
#column-right { float: right; width: 30%; }
#column-center { display: inline-block; width: 30%; }

.content-a {
display: none;
margin: 0 auto;
overflow:auto;
width: 80%;
max-width: 1100px;
color: #fff;
background: #17202f;
}

.content-b {
display: none;
margin: 0 auto;
overflow:auto;
width: 80%;
max-width: 1100px;
color: #fff;
background: #17202f;
}

.content-c {
display: none;
margin: 0 auto;
overflow:auto;
width: 80%;
max-width: 1100px;
color: #fff;
background: #17202f;
}

#opens-content-a { cursor: pointer;}

#closes-content-a {
float: right;
padding: 20px 20px;
color: #FF0000;
font-weight: bold;
font-size: x-large;
cursor: pointer;
}

#opens-content-b { cursor: pointer;}

#closes-content-b {
float: right;
padding: 20px 20px;
color: #FF0000;
font-weight: bold;
font-size: x-large;
cursor: pointer;
}

#opens-content-c { cursor: pointer;}

#closes-content-c {
float: right;
padding: 20px 20px;
color: #FF0000;
font-weight: bold;
font-size: x-large;
cursor: pointer;
}

/*-------------------------- SLIDE OUT ---------------------------*/
.slideOut {
animation-name: slideOut;
-webkit-animation-name: slideOut;
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-timing-function: ease-out;
animation-timing-function: ease-out;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}

@-webkit-keyframes slideOut {
0% {
opacity: 1;
-webkit-transform: translateY(0);
}
100% {
opacity: 0;
-webkit-transform: translateY(20px);
}
}
@keyframes slideOut {
0% {
opacity: 1;
transform: translateY(0);
}
100% {
opacity: 0;
transform: translateY(20px);
}
}

/*-------------------------- SLIDE IN ---------------------------*/

.slideIn {
visibility: visible;
animation-name: slideIn;
-webkit-animation-name: slideIn;

animation-duration: 1.5s;
-webkit-animation-duration: 1.5s;

animation-timing-function: ease;
-webkit-animation-timing-function: ease;
}

@keyframes slideIn {
0% {
opacity: 0;
transform: translateY(-50%);
}

100% {
opacity: 1;
transform: translateY(0%);
}
}

@-webkit-keyframes slideIn {
0% {
opacity: 0;
-webkit-transform: translateY(-50%);
}

100% {
opacity: 1;
-webkit-transform: translateY(0%);
}
}


This what I have so far JSFiddle





Aucun commentaire:

Enregistrer un commentaire