2

When you click the link apply any CSS animation during show/hide toggle. The toggle works, but animation is not applied.

$('.working-hours-link').click(function(e) {
  $(this).siblings(".hours").toggleClass('hidden shown');
});
.hidden {
  background-color: #fff;
  color: #000;
  -webkit-transition: all 1s;
  -moz-transition: all 1s;
  -o-transition: all 1s;
  transition: all 1s;
  display: none;
}

.shown {
  background-color: #000;
  color: #fff;
  -webkit-transition: all 1s;
  -moz-transition: all 1s;
  -o-transition: all 1s;
  transition: all 1s;
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="working-hours-link" href="#">Show working hours</a>
<br>
<div class="hours hidden">
  Sunday: 12:00 pm-6:00 pm
</div>

https://jsfiddle.net/ako6uzxt/

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
user3108268
  • 1,043
  • 3
  • 18
  • 37
  • which kind of transition are you expecting to see? color and background are actually fading, but the display property run like a switch. Maybe do you expect an opacity transition (from 0 to 1)? – Fabrizio Calderan Feb 11 '20 at 11:39
  • `CSS` transitions doesn't work with `display` properties. Here are some [alternatives](https://stackoverflow.com/questions/3331353/transitions-on-the-css-display-property) to it. – Viral Feb 11 '20 at 11:40
  • 1
    Does this answer your question? [Transitions on the CSS display property](https://stackoverflow.com/questions/3331353/transitions-on-the-css-display-property) – aryan Feb 11 '20 at 11:44

4 Answers4

5

transition doesn't work for display. try to change display:none and display:block to opacity:0 and opacity:1

Yosef Tukachinsky
  • 5,570
  • 1
  • 13
  • 29
2

The issue is because the transition CSS rule needs to exist on the element before the class is applied/removed from it. As such you can add that to the .hours rule. In addition you no longer need to use the vendor prefixes on transition.

Also note that you can't animate display so I used a combination of opacity and height instead.

$('.working-hours-link').click(function(e) {
  $(this).siblings(".hours").toggleClass('hidden shown');
});
.hours {
  transition: height, opacity 1s;
}

.hidden {
  background-color: #fff;
  color: #000;
  height: 0;
  opacity: 0;
}

.shown {
  background-color: #000;
  color: #fff;
  height: 20px;
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="working-hours-link" href="#">Show working hours</a>
<br>
<div class="hours hidden">
  Sunday: 12:00 pm-6:00 pm
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

In css display: none and display: block the transition will not work. because the hidden file is not in html by default.. after click the button the files will show.. replace of display none we can use opacity:0; please remove the display: none and display:block

css

.hidden {
  opacity: 0; 
  visibility: hidden;
}
.shown {
  opacity: 1;
  visibility:visible;
}
Manikandan2811
  • 831
  • 4
  • 9
0

Please add this Code

.hours{display:none;background-color:#000;color:#fff;}

$('.working-hours-link').click(function(e) {
    $(this).siblings(".hours").toggleClass('hidden');
});
$('.working-hours-link').click(function(e) {
    $(this).siblings(".hours").slideToggle();
});
Mehul Davara
  • 321
  • 2
  • 4