How can I stop my center div from changing position after I SlideUp another divs?

MarcRaion

I'm creating simple page with a <header> and a <section>. In the section I have 3 divs and I am positioning them with display: flex; and justify-content: space-between.

The problem is that I also use JS slideToggle() on two of them (extreme ones). It is changing the layout of my center div after they are going up. How can I do it so that my center div doesn't change position after one of the others is slid up?

$(document).ready(function() {
  $('#playlist').click(function() {
    $('#nav').slideToggle();
  });
});

$(document).ready(function() {
  $('#songs').click(function() {
    $('#listSongs').slideToggle();

  });
});
section {
  display: flex;
  justify-content: space-between;
}

#listSongs {
  margin-top: 50px;
  height: 550px;
  background-color: black;
  border-radius: 5px;
  font-size: 25px;
  width: 200px;
}

#listSongs p {
  margin-top: 5px;
  width: 200px;
  text-align: center;
  overflow: hidden;
  height: 35px;
  line-height: 50px;
  color: white;
}

#player {
  color: red;
}

#nav {
  margin-top: 50px;
  height: 550px;
  background-color: black;
  border-radius: 20px;
  width: 200px;
}

.hidden {
  margin-top: 50px;
  height: 550px;
  background-color: black;
  border-radius: 20px;
  width: 200px;
  visibility: hidden;
}

#nav p {
  text-align: center;
}

#nav ul {
  list-style-type: none;
  text-align: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section>
  <div id="listSongs">
    <p>Authors:</p>
    <div class="after"></div>
  </div>
  <div id="player">
    <p>something</p>
  </div>
  <div id="nav">
    <p>something</p>
  </div>
</section>

Rory McCrossan

The issue is because when the slideUp/slideDown/slideToggle methods complete, they set display: none on the target element. This is what causes the layout of your page to shift.

To workaround, and improve the animation, you can use CSS instead. Use the transition property to animate the height setting. Then you can toggle a class which sets height: 0 on the target element. Try this:

$(document).ready(function() {
  $('#playlist').click(function() {
    $('#nav').toggleClass('hide');
  });

  $('#songs').click(function() {
    $('#listSongs').toggleClass('hide');
  });
});
body { background-color: #CCC; }
section {
  display: flex;
  justify-content: space-between;
}

section > div.panel {
  margin-top: 50px;
  height: 550px;
  background-color: black;
  border-radius: 5px;
  font-size: 25px;
  width: 200px;
  transition: height 0.4s;
  overflow: hidden;
}

section > div.panel.hide {
  height: 0;
}

section > div.panel p {
  margin-top: 5px;
  width: 200px;
  text-align: center;
  overflow: hidden;
  height: 35px;
  line-height: 50px;
  color: white;
}

#player {
  color: red;
}

#nav {
  border-radius: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="playlist">Playlist</button>
<button id="songs">Songs</button>
<section>
  <div id="listSongs" class="panel">
    <p>Authors:</p>
    <p>lorem ipsum</p>
    <div class="after"></div>
  </div>
  <div id="player">
    <p>something</p>
  </div>
  <div id="nav" class="panel">
    <p>something</p>
  </div>
</section>

Note that I also rearranged some of the CSS to make it more generic with less repetition.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I position my div at the bottom of its container?

From Dev

How can I align to center floated divs?

From Dev

How can I stop my cursor changing to an I bar when I hover over text in a table?

From Dev

How can I stop NSTableView from saving the current scroll position?

From Dev

why is my UIButton changing size and position after assigning an image? How can I keep it the same?

From Dev

How can I stop Scrolling my <div> onmouseover?

From Dev

How can I get my text to position at the top of my divs?

From Dev

How can I change the width/height of a div without changing position?

From Dev

How can I stop virtualenv changing my PATH order

From Dev

How can I stop animated divs from moving place

From Dev

How can I position three divs in a page?

From Dev

How do I center a div in another div?

From Dev

How can I stop my recyclerView view type from changing after scrolling?

From Dev

How can I stop my center div from changing position after I SlideUp another divs?

From Dev

How can I stop Linux from changing Windows's clock?

From Dev

How can I stop NSTableView from saving the current scroll position?

From Dev

How can I get my text to position at the top of my divs?

From Dev

How can I position these divs side by side

From Dev

how can I stop multiline commands from messing up my cursor position when scrolling through history?

From Dev

How can I stop the remote computer from changing my keyboard layout?

From Dev

How can I position the menu buttons in the center?

From Dev

How can I stop my controller from moving my mouse?

From Dev

How can I hide a divs border behind another div with css?

From Dev

How can I stop animated divs from moving place

From Dev

How can i get my slideshow to stop changing size?

From Dev

Excel messes with my formula when copied, how can I stop I stop it changing one part but not the other?

From Dev

How can I stop Open Office Calc from changing the data?

From Dev

How Can I add a div to the right of my responsive divs?

From Dev

how do i stop my bootstrap carousel from changing size

Related Related

  1. 1

    How can I position my div at the bottom of its container?

  2. 2

    How can I align to center floated divs?

  3. 3

    How can I stop my cursor changing to an I bar when I hover over text in a table?

  4. 4

    How can I stop NSTableView from saving the current scroll position?

  5. 5

    why is my UIButton changing size and position after assigning an image? How can I keep it the same?

  6. 6

    How can I stop Scrolling my <div> onmouseover?

  7. 7

    How can I get my text to position at the top of my divs?

  8. 8

    How can I change the width/height of a div without changing position?

  9. 9

    How can I stop virtualenv changing my PATH order

  10. 10

    How can I stop animated divs from moving place

  11. 11

    How can I position three divs in a page?

  12. 12

    How do I center a div in another div?

  13. 13

    How can I stop my recyclerView view type from changing after scrolling?

  14. 14

    How can I stop my center div from changing position after I SlideUp another divs?

  15. 15

    How can I stop Linux from changing Windows's clock?

  16. 16

    How can I stop NSTableView from saving the current scroll position?

  17. 17

    How can I get my text to position at the top of my divs?

  18. 18

    How can I position these divs side by side

  19. 19

    how can I stop multiline commands from messing up my cursor position when scrolling through history?

  20. 20

    How can I stop the remote computer from changing my keyboard layout?

  21. 21

    How can I position the menu buttons in the center?

  22. 22

    How can I stop my controller from moving my mouse?

  23. 23

    How can I hide a divs border behind another div with css?

  24. 24

    How can I stop animated divs from moving place

  25. 25

    How can i get my slideshow to stop changing size?

  26. 26

    Excel messes with my formula when copied, how can I stop I stop it changing one part but not the other?

  27. 27

    How can I stop Open Office Calc from changing the data?

  28. 28

    How Can I add a div to the right of my responsive divs?

  29. 29

    how do i stop my bootstrap carousel from changing size

HotTag

Archive