Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Animation in toggleClass functon in Jquery

I am making a div which would show on button click and hide on clicking again.I am using toggleClass function of jquery. The problem is I need animation while the div opens and closes. I have tried transition:1s, in all relevant classes and even on body, but in vain. This is the fiddle: http://jsfiddle.net/9dq2p7dw/105/

Here is the code for my jquery:

$(document).ready(function(){
$(".view").click(function(){

$(".view-more").toggleClass("hide show", 500);

});

});
like image 257
Sarthak Tuteja Avatar asked Sep 17 '25 21:09

Sarthak Tuteja


2 Answers

You can use .fadeToggle

$(document).ready(function(){
$(".view").click(function(){

$(".view-more").fadeToggle( "hide show", "linear" );

});

});
.hide{
  display:none;
}
.show{
  display:block
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="view">View All</a>
<div class="view-more hide" >

<div class="col-md-6">
 <h1>
 asasasas
 </h1>
</div>

<div class="col-md-6">

 <h1>
 sasasas
 </h1>

</div>

</div>
like image 99
Curiousdev Avatar answered Sep 20 '25 13:09

Curiousdev


Use transition: all 0.3s ease;

Refer CSS transitions

$(document).ready(function() {
  $(".view").click(function() {

    $(".view-more").toggleClass("hide show", 500);

  });

});
.hide {
  opacity: 0;
  transition: all 1s ease;
}

.show {
  opacity: 1;
  transition: all 1s ease;
}
.view-more{
background-color:#F0C798;
padding:10px;
box-sizing:border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="view">View All</a>
<div class="view-more hide">

  <div class="col-md-6">
    <h1>
      asasasas
    </h1>
  </div>

  <div class="col-md-6">

    <h1>
      sasasas
    </h1>

  </div>

</div>

JS Fiddle Link

like image 31
Geethu Jose Avatar answered Sep 20 '25 14:09

Geethu Jose