Solved

JQuery: How toggle animation using If statements?

Posted on 2008-10-30
4
1,108 Views
Last Modified: 2008-10-30
Hi Experts,

I have a button at the top of my page, that when clicked, changes the height of a div from 40px to 300px and animates with Jquery. Id like to add some more functionality to this button, and I think it will require some if/else statements. (Please refer to the link in the post - and dont mind the ugliness right now ;) )

Logically, (i think) I understand what it needs to do, but I'm not familiar enough with Jquery to achieve it. I'd basically like the link to function like this:

If #info_guide's height = 40px then animate and change height to 300px...
ELSE
If #info_guide's height = is 300px then animate and change height back to 40px...

Also, everytime the link is clicked, add/remove a class name of ".down" to it

I REALLY appreciate your help.


http://www.0to5.com/_dev/index.html
0
Comment
Question by:CSS_Guy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
husker475 earned 500 total points
ID: 22844302
Try this:

$(document).ready(function(){
  $("#button_id").click(function(){
    if($("#info_guide").css('height') == '40px'){
      $("#info_guide").animate({
        height: '300px'
      } , 1500);
    }
    else{
      $("#info_guide").animate({
        height: '40px'
      }, 1500);
    }
    
    //Move into the if/else-blocks if you want to.
    this.addClass("down");
    this.removeClass("down");
 
  });
});

Open in new window

0
 

Author Comment

by:CSS_Guy
ID: 22844501
This is just what I was looking for - I really appreciate your help. I had to change the class code a bit. I might have been putting your code in the wrong place, but either way - I'm all good to go. Everthing is fully functional. Thanks again.
<script>
$(document).ready(function(){
  $("#go").click(function(){
    if($("#info_panel").css('height') == '40px'){
      $("#info_panel").animate({
        height: '300px'
      } , 450);$("#go").addClass("down");
    }
    else{
      $("#info_panel").animate({
        height: '40px'
      }, 450);$("#go").removeClass("down");
    }
 
  });
});
  </script>

Open in new window

0
 
LVL 4

Expert Comment

by:husker475
ID: 22844569
My bad, sorry.
this.addClass('down') should be $(this).addClass('down');
Same with removeClass, anyway, glad you got it working.
0
 

Author Comment

by:CSS_Guy
ID: 22844586
No worries at all. Thanks again man
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why am I getting ReferenceError:  boxes is not defined error 5 33
Bootstrap NavBar Not Working 2 30
JSON help 8 48
How do I init a PHP page jusing JQuery? 8 31
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question