0

I am new to JS.

Right now I managed to make a text appear and a div slide to the right when I click on a div.

The problem is: How can I manage to do this in reverse? (with the same animation)

I tried the if/else statement but I cant get it done. Hope some of you can help me.

Here is my Javascript code

$(document).ready(function(){
        $('#profile').click(function() {
            $('#profile-info').fadeIn("").addClass('animated fadeInDown');
            $('#profile').animate({left:'200px'}, 800,'swing');
        });
    });

Here is my HTML code

<section>
<div id="profile">
    </div>
        <div id="profile-photo">
        </div>
            <div id="profile-info">
                <p>Hello, I'm</p> 
                    <p>Rick James</p>
            </div>
</section>
4
  • can u make a fiddle with your code??? --> jsfiddle.net Commented Apr 17, 2014 at 16:39
  • Where is the relevant css mentioned in code...? Commented Apr 17, 2014 at 16:40
  • Yes, either make a jsFiddle or atleast provide the markup of the animated and fadeInDown CSS classes? Commented Apr 17, 2014 at 16:40
  • By "in reverse" do you mean by clicking on the same button? Or do you mean after a certain period of time in the .animate call? Commented Apr 17, 2014 at 16:40

3 Answers 3

1

Try this:

I'm using

if ( $( "#profile-info" ).is( ":hidden" ) ) { 
   //do this
} else {
   //do this
}

So basically if it is hidden it will do the code, the if not, the other desired code

$(document).ready(function(){
        $('#profile').click(function() {
           if ( $( "#profile-info" ).is( ":hidden" ) ) {
            $('#profile-info').fadeIn("").addClass('animated fadeInDown');
            $('#profile').animate({left:'200px'}, 800,'swing');
           } else {
            $('#profile-info').fadeOut("").removeClass('animated fadeInDown');
            $('#profile').animate({left:'200px'}, 800,'swing');
           }
        });
    });

DEMO HERE: http://jsfiddle.net/YEwUZ/509/

UPDATED:

JQUERY

$(document).ready(function(){
        $('#profile').click(function() {
           if ( $( "#profile-info" ).is( ":hidden" ) ) {
            $('#profile-info').fadeIn("").addClass('animated fadeInDown');
            $('#profile-info').animate({left:'0px',opacity:1}, 800,'swing');

           } else {
            $('#profile-info').animate({left:'200px',opacity:0}, 800,'swing');
            $('#profile-info').fadeOut("").removeClass('animated fadeInDown');

         }
      });
 });

HTML

<section>
        <div id="profile">
            test
        </div>

        <div id="profile-photo">
        </div>

        <div id="profile-info">
            <p>Hello, I'm</p> 
            <p>Rick James</p>
        </div>
</section>

CSS

#profile-info {
    display: none;
    position:relative;
    left:200px;
    opacity:0;
}

DEMO HERE: http://jsfiddle.net/YEwUZ/512/

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

4 Comments

this works perfect! thanks!! +1 only how can i manage to make #profile-info fade out with a animation instead of the simply fadeOut function
thanks... and if you want to add animation to it, you could just change the jquery from fadeOut to something like slideUp. DEMO: jsfiddle.net/YEwUZ/510
Or you could set the div to position:relative; and use animation with jquery: see the demo here: jsfiddle.net/YEwUZ/511
There is one more thing you can do to make it a little nice when it fades in: You can animate the opacity so it does everything at once: jsfiddle.net/YEwUZ/512
1

Try this:

$(document).ready(function(){
    $('#profile').click(function() {
        if($(this).css('left') !== '200px'){
        $('#profile-info').fadeIn("").addClass('animated fadeInDown');
        $('#profile').animate({left:'200px'}, 800,'swing');
       } else {
             $('#profile-info').fadeIn("").removeClass('animated fadeInDown');
        $('#profile').animate({left:'0px'}, 800,'swing'); 
       } 
    });
});

1 Comment

this works fine! only the text just will be out in an instant instead of fading out. but thanks!
0

You can use fadeToggle(options) and toggleClass() from jQuery for this.

Comments

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.