0

Have a fairly simple show/hide script for a set of data filters from a button. I've been looking at solutions on how to animate the transition but can't seem to see how this script differs from what's described on the jQuery site.

I read somewhere else that CSS3 animations might be easier or better but that also remains a mystery to me.

Is there an easy modification to this script:

$('.toggle').click(function (event) {
    event.preventDefault();
    var target = $(this).attr('href');
    $(target).toggleClass('hidden show');
}); 
3
  • What kind of animation are you wanting? fade/slide down/slide right? Commented Feb 25, 2016 at 16:21
  • You have error in your code, comma after 'hidden show'. Commented Feb 25, 2016 at 16:24
  • Oops on the error, left over from messing around, thanks. Just a slide down animation. Very curious to see how this is done. Commented Feb 25, 2016 at 16:27

3 Answers 3

1

Instead of changing the classes, you can use the built-in toggleX methods (eg toggle() and slideToggle()).

If you want to do something more fancy such as animating colours, you'll need to look at the animate method and possibly including jquery-ui, which is where css3 transitions may be easier / less overhead unless you're already including jquery-ui.

$("#toggle").click(function() {
    $("#target").toggle(500);
});

$("#slide").click(function() {
    $("#target").slideToggle(500);
});

Basic fiddle: https://jsfiddle.net/t2j03v6d/

Sign up to request clarification or add additional context in comments.

1 Comment

I knew it had to be something simple but I was just not finding the right page to understand it. Thanks!
1

$('.target').on( 'click', function () {
  var $stuff = $(this).find('.stuff');
  
  if ( $stuff.is(':visible') ) {
    $stuff.slideUp('slow');
  } else {
    $stuff.slideDown('slow');
  }
});
.target .stuff {
  display: none;
  
  height: 400px;
  background-color: #F00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<ul>
  <li class="target">
    Show/Hide
    <div class="stuff"></div>
  </li>
</ul>

2 Comments

Please provide an explanation / commentary with your answer, not just code.
Excellent! Thanks for providing the CSS version of it. Wish there were multiple right answer checks for this.
1

I'm not sure what animation you're looking for but here I've used slideToggle() (http://api.jquery.com/slidetoggle/) using some of the code you've provided: https://jsfiddle.net/8gavvmnL/1/

HTML:

<a class="toggle" href="#pop">Click Me</a>
<div id="pop">
  I'm hidden until the button is clicked!
</div>

jQuery:

$("#pop").hide();
$('.toggle').click(function (event) {
    event.preventDefault();
    var target = $(this).attr('href');
    $(target).slideToggle();
}); 

1 Comment

Change slideDown() to slideToggle() for a better match with the requirement / OPs code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.