Kareem Sultan
Kareem Sultan

Reputation: 190

JQuery dynamic height issue

1- I want the right sidebar to take the same video height

2- also when I change the video it doesn't take the same height, please click on chapter 2 to see the issue

its work only when I resize the page

I don't know why the sidebar didn't take the same video height can anyone help me, please?

https://codepen.io/kemozzz/full/gXEjjJ/

var vidHeight = $('.video-container').height();
$('.description').css('height', vidHeight + 'px');
$(window).resize(function() {
  var vidHeight = $('.video-container').height();
  $('.description').css('height', vidHeight + 'px');
});
/* Tools */

.no-padding {
  padding: 0!important;
}

.slow-effect {
  transition: all .2s ease-in-out;
  -webkit-transition: all .2s ease-in-out;
}


/* ======================================================================================= */


/* Layout ================================================================================ */

.space {
  height: 50px;
}

.container-fluid {
  padding: 0 4%;
}


/* Global Styles ========================================================================= */


/* Form Elements */


/*start places*/

body {
  height: 2000px;
}

.main-container {
  border: 2px solid #000;
  overflow: hidden;
}

.description {
  background-color: #898989;
  padding: 16px 30px;
  overflow: auto;
}

.description ul {
  margin: 0;
  padding: 0;
}

.description ul li a {
  background-color: #898989;
  color: #fff;
  padding-bottom: 18px;
  margin-bottom: 18px;
  position: relative;
  border: none;
}

.nav-tabs>li>a:hover {
  border: none;
  border-bottom: 2px solid #fff;
  background-color: #fff;
  color: #555555;
}

.description ul li a>h2 {
  font-weight: 700;
  font-size: 35px;
  margin: 0;
}

.description ul li a>p {
  font-size: 18px;
  line-height: 30px;
  margin-top: 5px;
  margin-bottom: 0;
}

.video-container {
  position: relative;
}

.video-container .tools {
  position: absolute;
  width: 100%;
  height: 40px;
  background-color: #a4a4a4;
  left: 0;
  bottom: 0;
  z-index: 999;
}

.video-container:hover .tools {
  visibility: hidden;
}

#style-1::-webkit-scrollbar {
  background-color: #898989;
}

#style-1::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
  background-color: #adadad
}

#style-1::-webkit-scrollbar {
  width: 20px;
}

@media(min-width: 992px) {
  .description {
    border-left: 2px solid #000;
  }
  .description ul li a {
    border-bottom: 2px solid #fff;
  }
}

@media(max-width: 991px) {
  .description {
    border-left: 2px solid #000;
    white-space: nowrap;
    padding: 0;
    height: auto!important;
  }
  .description ul li a {
    display: inline-block;
    width: 34%;
    padding: 14px;
    text-align: left;
    overflow: hidden;
    min-height: 252px;
    white-space: normal;
    vertical-align: text-top;
    margin-bottom: 0;
  }
  .description ul li:after {
    content: "";
    display: inline-block;
    width: 2px;
    height: 170px;
    position: absolute;
    right: 6px;
    top: 20px;
    background-color: #fff;
  }
  .description ul li:last-child:after {
    content: "";
    display: none;
  }
  .description ul li a>h2 {
    font-size: 22px;
  }
  .description ul li a>p {
    font-size: 14px;
    line-height: 23px;
  }
}

@media screen and (min-width: 220px) and (max-width: 550px) {
  .description ul li a {
    padding: 8px;
    min-height: 200px;
  }
  .description ul li a h2 {
    font-size: 14px;
  }
  .description ul li a p {
    font-size: 10px;
    line-height: 16px;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container-fluid">
  <div class="row">
    <div class="space"></div>
    <div id="exTab2" class="col-xs-12 no-padding main-container">
      <div class="col-md-9 no-padding video-container tab-content">
        <div id="1" class="tabs-container tab-pane fade in active">
          <!-- Item-1 -->
          <div class="tools slow-effect"></div><video controls="" width="100%"><source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4"> <source src="test.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="2" class="tabs-container tab-pane fade">
          <!-- Item-2 -->
          <div class="tools slow-effect"></div><video controls="" width="100%"><source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4"> <source src="mov_bbb.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="3" class="tabs-container tab-pane fade">
          <!-- Item-3 -->
          <div class="tools slow-effect"></div><video controls="" width="100%"><source src="movie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="4" class="tabs-container tab-pane fade">
          <!-- Item-4 -->
          <div class="tools slow-effect"></div><video controls="" width="100%"><source src="test.WEBM" type="video/mp4"> <source src="test.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
      </div>
      <!-- Item-2 -->
      <div class="col-md-3 description" id="style-1">
        <ul class="nav nav-tabs">
          <li class="active">
            <a data-toggle="tab" href="#1">
              <h2>Chapter 1</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt doloremque incidunt.</p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#2">
              <h2>Chapter 2</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt, rerum doloremque incidunt laudantium aliquid itaque cum error nobis.</p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#3">
              <h2>Chapter 3</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit.</p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#4">
              <h2>Chapter 4</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt, rerum doloremque incidunt laudantium aliquid itaque.</p>
            </a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>

Upvotes: 3

Views: 284

Answers (2)

AuxTaco
AuxTaco

Reputation: 5171

Flexbox will solve this for you without any extra Javascript at all.

#exTab2 {
  display: flex;
}
.video-container {
  flex: 0 0 auto;
}
#style-1 {
  flex: 1 0 auto;
  position: relative;
}
@media(min-width: 992px) {
  .wrap {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
  #exTab2 {
    flex-direction: row;
  }
}
@media(max-width: 991px) {
  #exTab2 {
    flex-direction: column;
  }
}

(You'll need to wrap your ul in a .wrap div and move the description class to that div in order to keep the sidebar from stretching the container, as seen in this answer. This also breaks your control-hiding div, but that's not how you want to achieve that anyway.)

/* Tools */
.no-padding   { padding: 0!important; }
.slow-effect { transition: all .2s ease-in-out; -webkit-transition: all .2s ease-in-out; }

/* ======================================================================================= */
/* Layout ================================================================================ */
.container-fluid {
  margin-top: 50px;
  padding: 0 4%;
}

#exTab2 {
  display: flex;
}

.video-container {
  flex: 0 0 auto;
}

#style-1 {
  flex: 1 0 auto;
  position: relative;
}

/* Global Styles ========================================================================= */
/* Form Elements */
/*start places*/
body {
  height: 2000px;
}
.main-container {
  border: 2px solid #000;
  overflow: hidden;
}
.description {
  background-color: #898989;
  padding: 16px 30px;
  overflow: auto;
}
.description ul {
  margin: 0;
  padding: 0;
}
.description ul li a {
  background-color: #898989;
  color: #fff;
  padding-bottom: 18px;
  margin-bottom: 18px;
  position: relative;
  border:none; 
}
.nav-tabs>li>a:hover {
  border:none; 
  border-bottom: 2px solid #fff;
  background-color: #fff;
  color: #555555;
}

.description ul li a > h2 {
  font-weight: 700;
  font-size: 35px;
  margin: 0;
}
.description ul li a > p {
  font-size: 18px;
  line-height: 30px;
  margin-top: 5px;
  margin-bottom: 0;
}
.video-container {
  position: relative;
}

.video-container .tools {
  position: absolute;
  width: 100%;
  height: 40px;
  background-color: #a4a4a4;
  left: 0;
  bottom: 0;
  z-index: 999;
}

.video-container:hover .tools {
  visibility: hidden;
}

#style-1::-webkit-scrollbar {
  background-color: #898989;
}

#style-1::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
  background-color: #adadad
}
#style-1::-webkit-scrollbar {
  width: 20px;
}
@media(min-width: 992px) {
  .wrap {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
  #exTab2 {
    flex-direction: row;
  }
  .description {
    border-left: 2px solid #000;
  }
  .description ul li a {
    border-bottom: 2px solid #fff;
  }
}

@media(max-width: 991px) {
  #exTab2 {
    flex-direction: column;
  }
  .description {
    border-left: 2px solid #000;
    white-space: nowrap;
    padding: 0;
    height: auto!important;
  }
  .description ul li a {
    display: inline-block;
    width: 34%;
    padding: 14px;
    text-align: left;
    overflow: hidden;
    min-height: 252px;
    white-space: normal;
    vertical-align: text-top;
    margin-bottom: 0;
  }
  .description ul li:after {
    content: "";
    display: inline-block;
    width: 2px;
    height: 170px;
    position: absolute;
    right: 6px;
    top: 20px;
    background-color: #fff;
  }
  .description ul li:last-child:after {
    content: "";
    display: none;
  }
  .description ul li a > h2 {
    font-size: 22px;
  }
  .description ul li a > p {
    font-size: 14px;
    line-height: 23px;
  }
}

@media screen and (min-width: 220px) and (max-width: 550px) {
  .description ul li a {
    padding: 8px;
    min-height: 200px;
  }
  .description ul li a h2 {
    font-size: 14px;
  }
  .description ul li a p {
    font-size: 10px;
    line-height: 16px;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <div class="row container-fluid">
      <div id="exTab2" class="col-xs-12 no-padding main-container">
        <div class="col-md-9 no-padding video-container tab-content">
          <div id="1" class="tabs-container tab-pane fade in active">
            <!-- Item-1 -->
            <div class="tools slow-effect"></div>
            <video controls="" width="100%">
              <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
              <source src="test.ogg" type="video/ogg">
              Your browser does not support HTML5 video.
            </video>
          </div>
          <div id="2" class="tabs-container tab-pane fade">
            <!-- Item-2 -->
            <div class="tools slow-effect"></div>
            <video controls="" width="100%">
              <source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4">
              <source src="mov_bbb.ogg" type="video/ogg">
              Your browser does not support HTML5 video.
            </video>
          </div>
          <div id="3" class="tabs-container tab-pane fade">
            <!-- Item-3 -->
            <div class="tools slow-effect"></div>
            <video controls="" width="100%">
              <source src="movie.mp4" type="video/mp4">
              <source src="movie.ogg" type="video/ogg">
              Your browser does not support HTML5 video.
            </video>
          </div>
          <div id="4" class="tabs-container tab-pane fade">
            <!-- Item-4 -->
            <div class="tools slow-effect"></div>
            <video controls="" width="100%">
              <source src="test.WEBM" type="video/mp4">
              <source src="test.ogg" type="video/ogg">
              Your browser does not support HTML5 video.
            </video>
          </div>
        </div>
        <!-- Item-2 -->
        <div class="col-md-3" id="style-1">
          <div class="wrap description">
            <ul class="nav nav-tabs">
              <li class="active">
                <a data-toggle="tab" href="#1">
                  <h2>Chapter 1</h2>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt doloremque incidunt.</p>
                </a>
              </li>
              <li>
                <a data-toggle="tab" href="#2">
                  <h2>Chapter 2</h2>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt, rerum doloremque incidunt laudantium aliquid itaque cum error nobis.</p>
                </a>
              </li>
              <li>
                <a data-toggle="tab" href="#3">
                  <h2>Chapter 3</h2>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit.</p>
                </a>
              </li>
              <li>
                <a data-toggle="tab" href="#4">
                  <h2>Chapter 4</h2>
                  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt, rerum doloremque incidunt laudantium aliquid itaque.</p>
                </a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>

Upvotes: 1

Temani Afif
Temani Afif

Reputation: 272901

It's because you need to apply the change on the click event. So you may add this code to resize the video each time you click on a chapter (you may add some delay to be sure you get the correct size) :

$('ul.nav-tabs a').click(function() {
  setTimeout(function() {
    var vidHeight = $('.video-container').height();
    $('.description').css('height', vidHeight + 'px');
  }, 500);

});

You may also use $(document).ready function in order to be sure the resize code works well at page load.

Full code :

$(document).ready(function() {
  setTimeout(function() {
    var vidHeight = $('.video-container').height();
    $('.description').css('height', vidHeight + 'px');
  }, 500);
  $(window).resize(function() {
    var vidHeight = $('.video-container').height();
    $('.description').css('height', vidHeight + 'px');
  });

  $('ul.nav-tabs a').click(function() {
    setTimeout(function() {
      var vidHeight = $('.video-container').height();
      $('.description').css('height', vidHeight + 'px');
    }, 500);

  });
});
/* Tools */

.no-padding {
  padding: 0!important;
}

.slow-effect {
  transition: all .2s ease-in-out;
  -webkit-transition: all .2s ease-in-out;
}


/* ======================================================================================= */


/* Layout ================================================================================ */

.space {
  height: 50px;
}

.container-fluid {
  padding: 0 4%;
}


/* Global Styles ========================================================================= */


/* Form Elements */


/*start places*/

body {
  height: 2000px;
}

.main-container {
  border: 2px solid #000;
  overflow: hidden;
}

.description {
  background-color: #898989;
  padding: 16px 30px;
  overflow: auto;
}

.description ul {
  margin: 0;
  padding: 0;
}

.description ul li a {
  background-color: #898989;
  color: #fff;
  padding-bottom: 18px;
  margin-bottom: 18px;
  position: relative;
  border: none;
}

.nav-tabs>li>a:hover {
  border: none;
  border-bottom: 2px solid #fff;
  background-color: #fff;
  color: #555555;
}

.description ul li a>h2 {
  font-weight: 700;
  font-size: 35px;
  margin: 0;
}

.description ul li a>p {
  font-size: 18px;
  line-height: 30px;
  margin-top: 5px;
  margin-bottom: 0;
}

.video-container {
  position: relative;
}

.video-container .tools {
  position: absolute;
  width: 100%;
  height: 40px;
  background-color: #a4a4a4;
  left: 0;
  bottom: 0;
  z-index: 999;
}

.video-container:hover .tools {
  visibility: hidden;
}

#style-1::-webkit-scrollbar {
  background-color: #898989;
}

#style-1::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
  background-color: #adadad
}

#style-1::-webkit-scrollbar {
  width: 20px;
}

@media(min-width: 992px) {
  .description {
    border-left: 2px solid #000;
  }
  .description ul li a {
    border-bottom: 2px solid #fff;
  }
}

@media(max-width: 991px) {
  .description {
    border-left: 2px solid #000;
    white-space: nowrap;
    padding: 0;
    height: auto!important;
  }
  .description ul li a {
    display: inline-block;
    width: 34%;
    padding: 14px;
    text-align: left;
    overflow: hidden;
    min-height: 252px;
    white-space: normal;
    vertical-align: text-top;
    margin-bottom: 0;
  }
  .description ul li:after {
    content: "";
    display: inline-block;
    width: 2px;
    height: 170px;
    position: absolute;
    right: 6px;
    top: 20px;
    background-color: #fff;
  }
  .description ul li:last-child:after {
    content: "";
    display: none;
  }
  .description ul li a>h2 {
    font-size: 22px;
  }
  .description ul li a>p {
    font-size: 14px;
    line-height: 23px;
  }
}

@media screen and (min-width: 220px) and (max-width: 550px) {
  .description ul li a {
    padding: 8px;
    min-height: 200px;
  }
  .description ul li a h2 {
    font-size: 14px;
  }
  .description ul li a p {
    font-size: 10px;
    line-height: 16px;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container-fluid">
  <div class="row">
    <div class="space"></div>
    <div id="exTab2" class="col-xs-12 no-padding main-container">
      <div class="col-md-9 no-padding video-container tab-content">
        <div id="1" class="tabs-container tab-pane fade in active">
          <!-- Item-1 -->
          <div class="tools slow-effect"></div><video controls="" width="100%"><source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4"> <source src="test.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="2" class="tabs-container tab-pane fade">
          <!-- Item-2 -->
          <div class="tools slow-effect"></div><video controls="" width="100%"><source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4"> <source src="mov_bbb.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="3" class="tabs-container tab-pane fade">
          <!-- Item-3 -->
          <div class="tools slow-effect"></div><video controls="" width="100%"><source src="movie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="4" class="tabs-container tab-pane fade">
          <!-- Item-4 -->
          <div class="tools slow-effect"></div><video controls="" width="100%"><source src="test.WEBM" type="video/mp4"> <source src="test.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
      </div>
      <!-- Item-2 -->
      <div class="col-md-3 description" id="style-1">
        <ul class="nav nav-tabs">
          <li class="active">
            <a data-toggle="tab" href="#1">
              <h2>Chapter 1</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt doloremque incidunt.</p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#2">
              <h2>Chapter 2</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt, rerum doloremque incidunt laudantium aliquid itaque cum error nobis.</p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#3">
              <h2>Chapter 3</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit.</p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#4">
              <h2>Chapter 4</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt, rerum doloremque incidunt laudantium aliquid itaque.</p>
            </a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions