Div is showing for 1 second after I want it hidden












0















I have two div's in the same place and I only show one at a time. I have a function that hides one div and then shows the other and vice versa. My problem is that one of the divs is showing in the corner of the container after I hide it for 1 second, it is originally centered but when I hide it I set the width and height to 0. When it is in the corner the styling is gone and I only see the text from it and the button I use that holds the function to change divs, and it disappears after a second. I'm not sure why this is happening and would appreciate some help with this.



here is an example html of this -



<div class='event-container'>
<div class='events hidden'>
<span class='no-Events event-message'>no Events this Day</span>
<span class='event-title event-message'></span>
<span class='event-desc event-message'></span>
<button class='show-event-form rotate'>Submit</button>
</div>
<form class='new-event-form'>
<legend>Submit New Event</legend>
<input id='new-event-title' type='text' name='title' placeholder='title'>
<input id='new-event-desc' type='text' name='desc' placeholder='desc'>
<input type='submit' class='submit-event rotate' value='submit'>
</form>
</div>


Either the .events div will show or the .new-event-form Form will show/hide.



I use these css classes to show/hide them



.hidden {
height: 0; opacity: 0; width: 0;
transition: height 0ms 400ms, opacity 200ms 0ms, width 0ms 0ms;
}

.visible {
height: 100%; opacity: 1; width: 100%;
transition: height 200ms 0ms, opacity 600ms 200ms, width 0ms 0ms;
}


here is the css for .new-event-form and .events



.new-event-form{
width: 100%;
display:flex;
flex-direction: column;
justify-content: space-evenly;
text-align: center;
}

.events{
height: 100%;
display:flex;
flex-direction: column;
align-items: center; /*center children vertically*/
}


And this function changes the div's between show/hide classes



const hideShowEventsDiv = ()=> {
let eventsDiv = document.querySelector('.events');
let newEventForm = document.querySelector('.new-event-form');

if(eventsDiv.classList.contains('hidden')){
//show Events
newEventForm.classList.add('hidden');
newEventForm.classList.remove('visible');
eventsDiv.classList.remove('hidden');
eventsDiv.classList.add('visible');
} else {
//show new Event Form
eventsDiv.classList.remove('visible');
eventsDiv.classList.add('hidden');
newEventForm.classList.remove('hidden');
newEventForm.classList.add('visible');
}
}


eventListner for this



//Submit form and show event or new event form



 document.addEventListener('click', (e)=>{
if(e.target.classList.contains('rotate')){
e.preventDefault();
newEvent.submit();
newEvent.clear();
// ---> this one hides/shows the div
hideShowEventsDiv();
}
});


This problem is only happening for the .events Div. The .new-event-form Form is not having any issues with fading or showing in the corner and then dissapearing when I try to hide it, that is only the .events div with this problem










share|improve this question

























  • I'm sorry if I missed it. But, what triggers this show/hide event?

    – C.RaysOfTheSun
    Nov 17 '18 at 1:02











  • @C.RaysOfTheSun I think the css transitions kick in after the class changes to hidden or visible

    – Iskandar Reza Razali
    Nov 17 '18 at 1:05













  • @I.R.R. Yeah. But, like, what triggers the class change? A button? Does it happen onload? :)

    – C.RaysOfTheSun
    Nov 17 '18 at 1:07








  • 1





    @C.RaysOfTheSun right, my bad. Us computer folk are like computers too sometimes -- error: context not defined.

    – Iskandar Reza Razali
    Nov 17 '18 at 1:13











  • Hahaha! All's good lol

    – C.RaysOfTheSun
    Nov 17 '18 at 1:21
















0















I have two div's in the same place and I only show one at a time. I have a function that hides one div and then shows the other and vice versa. My problem is that one of the divs is showing in the corner of the container after I hide it for 1 second, it is originally centered but when I hide it I set the width and height to 0. When it is in the corner the styling is gone and I only see the text from it and the button I use that holds the function to change divs, and it disappears after a second. I'm not sure why this is happening and would appreciate some help with this.



here is an example html of this -



<div class='event-container'>
<div class='events hidden'>
<span class='no-Events event-message'>no Events this Day</span>
<span class='event-title event-message'></span>
<span class='event-desc event-message'></span>
<button class='show-event-form rotate'>Submit</button>
</div>
<form class='new-event-form'>
<legend>Submit New Event</legend>
<input id='new-event-title' type='text' name='title' placeholder='title'>
<input id='new-event-desc' type='text' name='desc' placeholder='desc'>
<input type='submit' class='submit-event rotate' value='submit'>
</form>
</div>


Either the .events div will show or the .new-event-form Form will show/hide.



I use these css classes to show/hide them



.hidden {
height: 0; opacity: 0; width: 0;
transition: height 0ms 400ms, opacity 200ms 0ms, width 0ms 0ms;
}

.visible {
height: 100%; opacity: 1; width: 100%;
transition: height 200ms 0ms, opacity 600ms 200ms, width 0ms 0ms;
}


here is the css for .new-event-form and .events



.new-event-form{
width: 100%;
display:flex;
flex-direction: column;
justify-content: space-evenly;
text-align: center;
}

.events{
height: 100%;
display:flex;
flex-direction: column;
align-items: center; /*center children vertically*/
}


And this function changes the div's between show/hide classes



const hideShowEventsDiv = ()=> {
let eventsDiv = document.querySelector('.events');
let newEventForm = document.querySelector('.new-event-form');

if(eventsDiv.classList.contains('hidden')){
//show Events
newEventForm.classList.add('hidden');
newEventForm.classList.remove('visible');
eventsDiv.classList.remove('hidden');
eventsDiv.classList.add('visible');
} else {
//show new Event Form
eventsDiv.classList.remove('visible');
eventsDiv.classList.add('hidden');
newEventForm.classList.remove('hidden');
newEventForm.classList.add('visible');
}
}


eventListner for this



//Submit form and show event or new event form



 document.addEventListener('click', (e)=>{
if(e.target.classList.contains('rotate')){
e.preventDefault();
newEvent.submit();
newEvent.clear();
// ---> this one hides/shows the div
hideShowEventsDiv();
}
});


This problem is only happening for the .events Div. The .new-event-form Form is not having any issues with fading or showing in the corner and then dissapearing when I try to hide it, that is only the .events div with this problem










share|improve this question

























  • I'm sorry if I missed it. But, what triggers this show/hide event?

    – C.RaysOfTheSun
    Nov 17 '18 at 1:02











  • @C.RaysOfTheSun I think the css transitions kick in after the class changes to hidden or visible

    – Iskandar Reza Razali
    Nov 17 '18 at 1:05













  • @I.R.R. Yeah. But, like, what triggers the class change? A button? Does it happen onload? :)

    – C.RaysOfTheSun
    Nov 17 '18 at 1:07








  • 1





    @C.RaysOfTheSun right, my bad. Us computer folk are like computers too sometimes -- error: context not defined.

    – Iskandar Reza Razali
    Nov 17 '18 at 1:13











  • Hahaha! All's good lol

    – C.RaysOfTheSun
    Nov 17 '18 at 1:21














0












0








0


0






I have two div's in the same place and I only show one at a time. I have a function that hides one div and then shows the other and vice versa. My problem is that one of the divs is showing in the corner of the container after I hide it for 1 second, it is originally centered but when I hide it I set the width and height to 0. When it is in the corner the styling is gone and I only see the text from it and the button I use that holds the function to change divs, and it disappears after a second. I'm not sure why this is happening and would appreciate some help with this.



here is an example html of this -



<div class='event-container'>
<div class='events hidden'>
<span class='no-Events event-message'>no Events this Day</span>
<span class='event-title event-message'></span>
<span class='event-desc event-message'></span>
<button class='show-event-form rotate'>Submit</button>
</div>
<form class='new-event-form'>
<legend>Submit New Event</legend>
<input id='new-event-title' type='text' name='title' placeholder='title'>
<input id='new-event-desc' type='text' name='desc' placeholder='desc'>
<input type='submit' class='submit-event rotate' value='submit'>
</form>
</div>


Either the .events div will show or the .new-event-form Form will show/hide.



I use these css classes to show/hide them



.hidden {
height: 0; opacity: 0; width: 0;
transition: height 0ms 400ms, opacity 200ms 0ms, width 0ms 0ms;
}

.visible {
height: 100%; opacity: 1; width: 100%;
transition: height 200ms 0ms, opacity 600ms 200ms, width 0ms 0ms;
}


here is the css for .new-event-form and .events



.new-event-form{
width: 100%;
display:flex;
flex-direction: column;
justify-content: space-evenly;
text-align: center;
}

.events{
height: 100%;
display:flex;
flex-direction: column;
align-items: center; /*center children vertically*/
}


And this function changes the div's between show/hide classes



const hideShowEventsDiv = ()=> {
let eventsDiv = document.querySelector('.events');
let newEventForm = document.querySelector('.new-event-form');

if(eventsDiv.classList.contains('hidden')){
//show Events
newEventForm.classList.add('hidden');
newEventForm.classList.remove('visible');
eventsDiv.classList.remove('hidden');
eventsDiv.classList.add('visible');
} else {
//show new Event Form
eventsDiv.classList.remove('visible');
eventsDiv.classList.add('hidden');
newEventForm.classList.remove('hidden');
newEventForm.classList.add('visible');
}
}


eventListner for this



//Submit form and show event or new event form



 document.addEventListener('click', (e)=>{
if(e.target.classList.contains('rotate')){
e.preventDefault();
newEvent.submit();
newEvent.clear();
// ---> this one hides/shows the div
hideShowEventsDiv();
}
});


This problem is only happening for the .events Div. The .new-event-form Form is not having any issues with fading or showing in the corner and then dissapearing when I try to hide it, that is only the .events div with this problem










share|improve this question
















I have two div's in the same place and I only show one at a time. I have a function that hides one div and then shows the other and vice versa. My problem is that one of the divs is showing in the corner of the container after I hide it for 1 second, it is originally centered but when I hide it I set the width and height to 0. When it is in the corner the styling is gone and I only see the text from it and the button I use that holds the function to change divs, and it disappears after a second. I'm not sure why this is happening and would appreciate some help with this.



here is an example html of this -



<div class='event-container'>
<div class='events hidden'>
<span class='no-Events event-message'>no Events this Day</span>
<span class='event-title event-message'></span>
<span class='event-desc event-message'></span>
<button class='show-event-form rotate'>Submit</button>
</div>
<form class='new-event-form'>
<legend>Submit New Event</legend>
<input id='new-event-title' type='text' name='title' placeholder='title'>
<input id='new-event-desc' type='text' name='desc' placeholder='desc'>
<input type='submit' class='submit-event rotate' value='submit'>
</form>
</div>


Either the .events div will show or the .new-event-form Form will show/hide.



I use these css classes to show/hide them



.hidden {
height: 0; opacity: 0; width: 0;
transition: height 0ms 400ms, opacity 200ms 0ms, width 0ms 0ms;
}

.visible {
height: 100%; opacity: 1; width: 100%;
transition: height 200ms 0ms, opacity 600ms 200ms, width 0ms 0ms;
}


here is the css for .new-event-form and .events



.new-event-form{
width: 100%;
display:flex;
flex-direction: column;
justify-content: space-evenly;
text-align: center;
}

.events{
height: 100%;
display:flex;
flex-direction: column;
align-items: center; /*center children vertically*/
}


And this function changes the div's between show/hide classes



const hideShowEventsDiv = ()=> {
let eventsDiv = document.querySelector('.events');
let newEventForm = document.querySelector('.new-event-form');

if(eventsDiv.classList.contains('hidden')){
//show Events
newEventForm.classList.add('hidden');
newEventForm.classList.remove('visible');
eventsDiv.classList.remove('hidden');
eventsDiv.classList.add('visible');
} else {
//show new Event Form
eventsDiv.classList.remove('visible');
eventsDiv.classList.add('hidden');
newEventForm.classList.remove('hidden');
newEventForm.classList.add('visible');
}
}


eventListner for this



//Submit form and show event or new event form



 document.addEventListener('click', (e)=>{
if(e.target.classList.contains('rotate')){
e.preventDefault();
newEvent.submit();
newEvent.clear();
// ---> this one hides/shows the div
hideShowEventsDiv();
}
});


This problem is only happening for the .events Div. The .new-event-form Form is not having any issues with fading or showing in the corner and then dissapearing when I try to hide it, that is only the .events div with this problem







javascript html css ecmascript-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 10:38







icewizard

















asked Nov 17 '18 at 0:45









icewizardicewizard

589




589













  • I'm sorry if I missed it. But, what triggers this show/hide event?

    – C.RaysOfTheSun
    Nov 17 '18 at 1:02











  • @C.RaysOfTheSun I think the css transitions kick in after the class changes to hidden or visible

    – Iskandar Reza Razali
    Nov 17 '18 at 1:05













  • @I.R.R. Yeah. But, like, what triggers the class change? A button? Does it happen onload? :)

    – C.RaysOfTheSun
    Nov 17 '18 at 1:07








  • 1





    @C.RaysOfTheSun right, my bad. Us computer folk are like computers too sometimes -- error: context not defined.

    – Iskandar Reza Razali
    Nov 17 '18 at 1:13











  • Hahaha! All's good lol

    – C.RaysOfTheSun
    Nov 17 '18 at 1:21



















  • I'm sorry if I missed it. But, what triggers this show/hide event?

    – C.RaysOfTheSun
    Nov 17 '18 at 1:02











  • @C.RaysOfTheSun I think the css transitions kick in after the class changes to hidden or visible

    – Iskandar Reza Razali
    Nov 17 '18 at 1:05













  • @I.R.R. Yeah. But, like, what triggers the class change? A button? Does it happen onload? :)

    – C.RaysOfTheSun
    Nov 17 '18 at 1:07








  • 1





    @C.RaysOfTheSun right, my bad. Us computer folk are like computers too sometimes -- error: context not defined.

    – Iskandar Reza Razali
    Nov 17 '18 at 1:13











  • Hahaha! All's good lol

    – C.RaysOfTheSun
    Nov 17 '18 at 1:21

















I'm sorry if I missed it. But, what triggers this show/hide event?

– C.RaysOfTheSun
Nov 17 '18 at 1:02





I'm sorry if I missed it. But, what triggers this show/hide event?

– C.RaysOfTheSun
Nov 17 '18 at 1:02













@C.RaysOfTheSun I think the css transitions kick in after the class changes to hidden or visible

– Iskandar Reza Razali
Nov 17 '18 at 1:05







@C.RaysOfTheSun I think the css transitions kick in after the class changes to hidden or visible

– Iskandar Reza Razali
Nov 17 '18 at 1:05















@I.R.R. Yeah. But, like, what triggers the class change? A button? Does it happen onload? :)

– C.RaysOfTheSun
Nov 17 '18 at 1:07







@I.R.R. Yeah. But, like, what triggers the class change? A button? Does it happen onload? :)

– C.RaysOfTheSun
Nov 17 '18 at 1:07






1




1





@C.RaysOfTheSun right, my bad. Us computer folk are like computers too sometimes -- error: context not defined.

– Iskandar Reza Razali
Nov 17 '18 at 1:13





@C.RaysOfTheSun right, my bad. Us computer folk are like computers too sometimes -- error: context not defined.

– Iskandar Reza Razali
Nov 17 '18 at 1:13













Hahaha! All's good lol

– C.RaysOfTheSun
Nov 17 '18 at 1:21





Hahaha! All's good lol

– C.RaysOfTheSun
Nov 17 '18 at 1:21












1 Answer
1






active

oldest

votes


















2














If you're talking about how the date text ("No events on [date]") and the button get all squeezed up during the transition, it's because you shrink their parent by setting its width property to 0. setting the overflow property to hidden in your hidden class will solve this issue :)






window.onload = ()=>{
let btn = document.querySelector('#toggler');
btn.addEventListener('click', function(){
// grab the events div
let eventsDiv = document.querySelector('#events');
// grab our mock form
let eventForm = document.querySelector('#event-form');
// toggle does the conditional you had before
// it ascts like a switch, removing the class if its there
// and adding it in otherwise.
eventForm.classList.toggle('hidden');
eventsDiv.classList.toggle('hidden');
});
};

.big-container {
display: flex;
align-items: center;
justify-content: center;
margin: 15px;
}

.container {
height: 200px;
display: flex;
flex-direction: column;
transition: height 0.2s ease-in-out, opacity 0.2s ease-in-out;
text-align: center;
}

.container-item {
background-color: orange;
height: 100px;
margin: 15px;
box-shadow: 0 5px 10px 2px black;
}

.hidden {
height: 0;
opacity: 0;
width: 0;
overflow: hidden;
}

#events {
background-color: green;
}

#event-form {
background-color: red;
}

#placeholder {
background-color: pink;
height: 200px;
width: 200px;
}

<div class="big-container">
<div class="big-container" id="placeholder">
<input type="button" value="toggle visibility" id="toggler">
</div>
<div class="big-container">
<div class="container" id="events">
<span class="container-item">There are no events on November 17, 2018</span>
<span class="container-item"></span>
<span class="container-item"></span>
<button class="container-item" id="btn-events">I am a button</button>
</div>
<div class="container hidden" id="event-form">
<div class="container-item"></div>
<div class="container-item"></div>
<div class="container-item"></div>
<button class="container-item" id="btn-submit">I am another button</button>
</div>
</div>
</div>





Also, here's a working example and a modified version of your pen :)






share|improve this answer


























  • Thank you for the help I appreciate it.

    – icewizard
    Nov 17 '18 at 18:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347152%2fdiv-is-showing-for-1-second-after-i-want-it-hidden%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If you're talking about how the date text ("No events on [date]") and the button get all squeezed up during the transition, it's because you shrink their parent by setting its width property to 0. setting the overflow property to hidden in your hidden class will solve this issue :)






window.onload = ()=>{
let btn = document.querySelector('#toggler');
btn.addEventListener('click', function(){
// grab the events div
let eventsDiv = document.querySelector('#events');
// grab our mock form
let eventForm = document.querySelector('#event-form');
// toggle does the conditional you had before
// it ascts like a switch, removing the class if its there
// and adding it in otherwise.
eventForm.classList.toggle('hidden');
eventsDiv.classList.toggle('hidden');
});
};

.big-container {
display: flex;
align-items: center;
justify-content: center;
margin: 15px;
}

.container {
height: 200px;
display: flex;
flex-direction: column;
transition: height 0.2s ease-in-out, opacity 0.2s ease-in-out;
text-align: center;
}

.container-item {
background-color: orange;
height: 100px;
margin: 15px;
box-shadow: 0 5px 10px 2px black;
}

.hidden {
height: 0;
opacity: 0;
width: 0;
overflow: hidden;
}

#events {
background-color: green;
}

#event-form {
background-color: red;
}

#placeholder {
background-color: pink;
height: 200px;
width: 200px;
}

<div class="big-container">
<div class="big-container" id="placeholder">
<input type="button" value="toggle visibility" id="toggler">
</div>
<div class="big-container">
<div class="container" id="events">
<span class="container-item">There are no events on November 17, 2018</span>
<span class="container-item"></span>
<span class="container-item"></span>
<button class="container-item" id="btn-events">I am a button</button>
</div>
<div class="container hidden" id="event-form">
<div class="container-item"></div>
<div class="container-item"></div>
<div class="container-item"></div>
<button class="container-item" id="btn-submit">I am another button</button>
</div>
</div>
</div>





Also, here's a working example and a modified version of your pen :)






share|improve this answer


























  • Thank you for the help I appreciate it.

    – icewizard
    Nov 17 '18 at 18:26
















2














If you're talking about how the date text ("No events on [date]") and the button get all squeezed up during the transition, it's because you shrink their parent by setting its width property to 0. setting the overflow property to hidden in your hidden class will solve this issue :)






window.onload = ()=>{
let btn = document.querySelector('#toggler');
btn.addEventListener('click', function(){
// grab the events div
let eventsDiv = document.querySelector('#events');
// grab our mock form
let eventForm = document.querySelector('#event-form');
// toggle does the conditional you had before
// it ascts like a switch, removing the class if its there
// and adding it in otherwise.
eventForm.classList.toggle('hidden');
eventsDiv.classList.toggle('hidden');
});
};

.big-container {
display: flex;
align-items: center;
justify-content: center;
margin: 15px;
}

.container {
height: 200px;
display: flex;
flex-direction: column;
transition: height 0.2s ease-in-out, opacity 0.2s ease-in-out;
text-align: center;
}

.container-item {
background-color: orange;
height: 100px;
margin: 15px;
box-shadow: 0 5px 10px 2px black;
}

.hidden {
height: 0;
opacity: 0;
width: 0;
overflow: hidden;
}

#events {
background-color: green;
}

#event-form {
background-color: red;
}

#placeholder {
background-color: pink;
height: 200px;
width: 200px;
}

<div class="big-container">
<div class="big-container" id="placeholder">
<input type="button" value="toggle visibility" id="toggler">
</div>
<div class="big-container">
<div class="container" id="events">
<span class="container-item">There are no events on November 17, 2018</span>
<span class="container-item"></span>
<span class="container-item"></span>
<button class="container-item" id="btn-events">I am a button</button>
</div>
<div class="container hidden" id="event-form">
<div class="container-item"></div>
<div class="container-item"></div>
<div class="container-item"></div>
<button class="container-item" id="btn-submit">I am another button</button>
</div>
</div>
</div>





Also, here's a working example and a modified version of your pen :)






share|improve this answer


























  • Thank you for the help I appreciate it.

    – icewizard
    Nov 17 '18 at 18:26














2












2








2







If you're talking about how the date text ("No events on [date]") and the button get all squeezed up during the transition, it's because you shrink their parent by setting its width property to 0. setting the overflow property to hidden in your hidden class will solve this issue :)






window.onload = ()=>{
let btn = document.querySelector('#toggler');
btn.addEventListener('click', function(){
// grab the events div
let eventsDiv = document.querySelector('#events');
// grab our mock form
let eventForm = document.querySelector('#event-form');
// toggle does the conditional you had before
// it ascts like a switch, removing the class if its there
// and adding it in otherwise.
eventForm.classList.toggle('hidden');
eventsDiv.classList.toggle('hidden');
});
};

.big-container {
display: flex;
align-items: center;
justify-content: center;
margin: 15px;
}

.container {
height: 200px;
display: flex;
flex-direction: column;
transition: height 0.2s ease-in-out, opacity 0.2s ease-in-out;
text-align: center;
}

.container-item {
background-color: orange;
height: 100px;
margin: 15px;
box-shadow: 0 5px 10px 2px black;
}

.hidden {
height: 0;
opacity: 0;
width: 0;
overflow: hidden;
}

#events {
background-color: green;
}

#event-form {
background-color: red;
}

#placeholder {
background-color: pink;
height: 200px;
width: 200px;
}

<div class="big-container">
<div class="big-container" id="placeholder">
<input type="button" value="toggle visibility" id="toggler">
</div>
<div class="big-container">
<div class="container" id="events">
<span class="container-item">There are no events on November 17, 2018</span>
<span class="container-item"></span>
<span class="container-item"></span>
<button class="container-item" id="btn-events">I am a button</button>
</div>
<div class="container hidden" id="event-form">
<div class="container-item"></div>
<div class="container-item"></div>
<div class="container-item"></div>
<button class="container-item" id="btn-submit">I am another button</button>
</div>
</div>
</div>





Also, here's a working example and a modified version of your pen :)






share|improve this answer















If you're talking about how the date text ("No events on [date]") and the button get all squeezed up during the transition, it's because you shrink their parent by setting its width property to 0. setting the overflow property to hidden in your hidden class will solve this issue :)






window.onload = ()=>{
let btn = document.querySelector('#toggler');
btn.addEventListener('click', function(){
// grab the events div
let eventsDiv = document.querySelector('#events');
// grab our mock form
let eventForm = document.querySelector('#event-form');
// toggle does the conditional you had before
// it ascts like a switch, removing the class if its there
// and adding it in otherwise.
eventForm.classList.toggle('hidden');
eventsDiv.classList.toggle('hidden');
});
};

.big-container {
display: flex;
align-items: center;
justify-content: center;
margin: 15px;
}

.container {
height: 200px;
display: flex;
flex-direction: column;
transition: height 0.2s ease-in-out, opacity 0.2s ease-in-out;
text-align: center;
}

.container-item {
background-color: orange;
height: 100px;
margin: 15px;
box-shadow: 0 5px 10px 2px black;
}

.hidden {
height: 0;
opacity: 0;
width: 0;
overflow: hidden;
}

#events {
background-color: green;
}

#event-form {
background-color: red;
}

#placeholder {
background-color: pink;
height: 200px;
width: 200px;
}

<div class="big-container">
<div class="big-container" id="placeholder">
<input type="button" value="toggle visibility" id="toggler">
</div>
<div class="big-container">
<div class="container" id="events">
<span class="container-item">There are no events on November 17, 2018</span>
<span class="container-item"></span>
<span class="container-item"></span>
<button class="container-item" id="btn-events">I am a button</button>
</div>
<div class="container hidden" id="event-form">
<div class="container-item"></div>
<div class="container-item"></div>
<div class="container-item"></div>
<button class="container-item" id="btn-submit">I am another button</button>
</div>
</div>
</div>





Also, here's a working example and a modified version of your pen :)






window.onload = ()=>{
let btn = document.querySelector('#toggler');
btn.addEventListener('click', function(){
// grab the events div
let eventsDiv = document.querySelector('#events');
// grab our mock form
let eventForm = document.querySelector('#event-form');
// toggle does the conditional you had before
// it ascts like a switch, removing the class if its there
// and adding it in otherwise.
eventForm.classList.toggle('hidden');
eventsDiv.classList.toggle('hidden');
});
};

.big-container {
display: flex;
align-items: center;
justify-content: center;
margin: 15px;
}

.container {
height: 200px;
display: flex;
flex-direction: column;
transition: height 0.2s ease-in-out, opacity 0.2s ease-in-out;
text-align: center;
}

.container-item {
background-color: orange;
height: 100px;
margin: 15px;
box-shadow: 0 5px 10px 2px black;
}

.hidden {
height: 0;
opacity: 0;
width: 0;
overflow: hidden;
}

#events {
background-color: green;
}

#event-form {
background-color: red;
}

#placeholder {
background-color: pink;
height: 200px;
width: 200px;
}

<div class="big-container">
<div class="big-container" id="placeholder">
<input type="button" value="toggle visibility" id="toggler">
</div>
<div class="big-container">
<div class="container" id="events">
<span class="container-item">There are no events on November 17, 2018</span>
<span class="container-item"></span>
<span class="container-item"></span>
<button class="container-item" id="btn-events">I am a button</button>
</div>
<div class="container hidden" id="event-form">
<div class="container-item"></div>
<div class="container-item"></div>
<div class="container-item"></div>
<button class="container-item" id="btn-submit">I am another button</button>
</div>
</div>
</div>





window.onload = ()=>{
let btn = document.querySelector('#toggler');
btn.addEventListener('click', function(){
// grab the events div
let eventsDiv = document.querySelector('#events');
// grab our mock form
let eventForm = document.querySelector('#event-form');
// toggle does the conditional you had before
// it ascts like a switch, removing the class if its there
// and adding it in otherwise.
eventForm.classList.toggle('hidden');
eventsDiv.classList.toggle('hidden');
});
};

.big-container {
display: flex;
align-items: center;
justify-content: center;
margin: 15px;
}

.container {
height: 200px;
display: flex;
flex-direction: column;
transition: height 0.2s ease-in-out, opacity 0.2s ease-in-out;
text-align: center;
}

.container-item {
background-color: orange;
height: 100px;
margin: 15px;
box-shadow: 0 5px 10px 2px black;
}

.hidden {
height: 0;
opacity: 0;
width: 0;
overflow: hidden;
}

#events {
background-color: green;
}

#event-form {
background-color: red;
}

#placeholder {
background-color: pink;
height: 200px;
width: 200px;
}

<div class="big-container">
<div class="big-container" id="placeholder">
<input type="button" value="toggle visibility" id="toggler">
</div>
<div class="big-container">
<div class="container" id="events">
<span class="container-item">There are no events on November 17, 2018</span>
<span class="container-item"></span>
<span class="container-item"></span>
<button class="container-item" id="btn-events">I am a button</button>
</div>
<div class="container hidden" id="event-form">
<div class="container-item"></div>
<div class="container-item"></div>
<div class="container-item"></div>
<button class="container-item" id="btn-submit">I am another button</button>
</div>
</div>
</div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 3:17

























answered Nov 17 '18 at 2:05









C.RaysOfTheSunC.RaysOfTheSun

640127




640127













  • Thank you for the help I appreciate it.

    – icewizard
    Nov 17 '18 at 18:26



















  • Thank you for the help I appreciate it.

    – icewizard
    Nov 17 '18 at 18:26

















Thank you for the help I appreciate it.

– icewizard
Nov 17 '18 at 18:26





Thank you for the help I appreciate it.

– icewizard
Nov 17 '18 at 18:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347152%2fdiv-is-showing-for-1-second-after-i-want-it-hidden%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings