3

I'm new to JavaScript, and I'm trying to create a simple hide and show div toggle, though it's not working. I'm not sure what the issue is -- I have the div initially display:none, then when the course-info-toggle class is clicked, it should turn to display:block, and transition down.

Any ideas?

/* Toggle on course detail sections */
$(document).ready(function() {
  $('.course-info-toggle').click(function() {
    $('.toggle-content').toggleClass('.show');
  });
});
.toggle-content.show {
  display: block;
  transition: left 0.3s linear;
}

.toggle-content {
  display: none;
  background-color: #eeeeee;
  padding: 1rem;
  margin-right: 1rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="toggle-wrap">
  <div class="course-info-toggle">
    <p id="course-details-toggle-font">COURSE OBJECTIVES</p>
    <img src="img/course-down-arrow.png" align="course-down-arrow">
  </div>
  <div class="toggle-content">
    <p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
      <p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
      </p>
  </div>
</div>
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
Shaz
  • 1,443
  • 1
  • 27
  • 67

7 Answers7

5

Remove the dot in the toggleClass

It should be

$('.toggle-content').toggleClass('show');

and not like .toggleClass('.show')

You want to add animation and transition both, it seems. Use keyframes for animation. Display property does not appear to be working with transition.

Css transition from display none to display block, navigation with subnav

Slide in from left CSS animation

Solution: You can change your css classes to this. Now, you can work over on it in different ways.

.toggle-content.show {
   visibility: visible;
     position: relative;
   opacity: 1;
     animation: leftSlide 2s forwards;
}

.toggle-content {
 opacity: 0;
  visibility: hidden;
  background-color: #eeeeee;
  transition: opacity 5s, visibility 2ms;
}

@keyframes leftSlide {
  0% {
    transform: translateX(-900px);
  }
  100% {
    transform: translateX(0);
  }
}

To make it slide down, you can use translateY.

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
1

You need to change .show to only show & add !important to display:block

/* Toggle on course detail sections */
$(document).ready(function() {
  $('.course-info-toggle').click(function() {

    $('.toggle-content').toggleClass('show');
  });
});
.toggle-content.show {
  display: block !important;
  transition: left 0.3s linear;
}

.toggle-content {
  display: none;
  background-color: #eeeeee;
  padding: 1rem;
  margin-right: 1rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="toggle-wrap">
  <div class="course-info-toggle">
    <p id="course-details-toggle-font">COURSE OBJECTIVES</p>
    <img src="img/course-down-arrow.png" align="course-down-arrow">
  </div>
  <div class="toggle-content">
    <p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
      <p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
      </p>
  </div>
</div>
brk
  • 48,835
  • 10
  • 56
  • 78
0

Change this line

$('.toggle-content').toggleClass('.show');

to line

$('.toggle-content').toggleClass('show');
Lalit
  • 1,354
  • 1
  • 8
  • 13
0

It is working as it should, unless i am mistaken and this is not a typo

$('.toggle-content').toggleClass('.show');

You should not be using '.show' it should be

$('.toggle-content').toggleClass('show');

You dont have to use .class name as per docs . It is working properly IMHO if you do this.

you can check out the fiddle here . Didnt make any changes except the one mentioned above.

semuzaboi
  • 4,972
  • 5
  • 20
  • 35
0

$(document).ready(function() {
 $('.course-info-toggle').click(function() {
  $('.toggle-content').toggleClass('show');
 });
});
.toggle-content.show {
 display: block;
    transition: left 0.3s linear;
}

.toggle-content {
 display: none;
 background-color: #eeeeee;
 padding: 1rem;
 margin-right:1rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="toggle-wrap">
 <div class="course-info-toggle">
  <p id="course-details-toggle-font">COURSE OBJECTIVES</p>
  <img src="img/course-down-arrow.png" align="course-down-arrow">
 </div>
 <div class="toggle-content">
  <p>simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
  <p>simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
  </p>
 </div>                          
</div>
Sreejith BS
  • 1,183
  • 1
  • 9
  • 18
0

toggleClass accepts a string so please use the following:

.toggleClass('show');
itacode
  • 3,721
  • 3
  • 21
  • 23
0

/* Toggle on course detail sections */
$('.course-info-toggle').click(function() {
  $('.toggle-content').toggleClass('show');
});
.toggle-content.show {
  display: block;
  transition: left 0.3s linear;
}

.toggle-content {
  display: none;
  background-color: #eeeeee;
  padding: 1rem;
  margin-right: 1rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="toggle-wrap">
  <div class="course-info-toggle">
    <p id="course-details-toggle-font">COURSE OBJECTIVES</p>
    <img src="img/course-down-arrow.png" align="course-down-arrow">
  </div>
  <div class="toggle-content">
    <p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
      <p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
      </p>
  </div>
</div>
gjijo
  • 1,206
  • 12
  • 15