Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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);

});

});
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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>

about 4 years ago · Santiago Trujillo Report

0

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

about 4 years ago · Santiago Trujillo Report

0

If all you wish to do is hide and show a div upon clicking a button, you can do so in simpler way.

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

    $(".view-more").toggle(function() {
        $(this).animate({height: '200'});
}, function() {
        $(this).animate({height: '100'});
    });​

    });

When given a list of functions as arguments, the .toggle(fn1, fn2) method will alternate between the functions given starting with the first one. This automatically keeps track of the toggle state for you - you don't have to do that.

jQuery doc is here. There are multiple forms of .toggle() depending upon the arguments used so you don't always find the right one when searching the jQuery doc.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!