0

I have this jQuery code:

var help;
    $('.helpPin').click(function(){

        if(help == true){
            var cssObj = {
                'position' : 'absolute',
                'bottom' : '0'
            };
            $('#help').css(cssObj);
            help = false;
        } else {
            var cssObj = {
                'position' : 'absolute',
                'bottom' : '-206px'
            };
            $('#help').css(cssObj);
            help = true;
        }
    });

That basically just swaps css for an element, how can i introduce animate effects to so its not too jerky? Can you use .css inside .animate?

2
  • 2
    toggleClass would be better. Commented Mar 15, 2012 at 12:13
  • Have a look at the documentation. For an animated version, use jQuery UI toggleClass. Commented Mar 15, 2012 at 12:20

4 Answers 4

3

First of all, addinga/amending CSS properties directly in jQuery is considered bad practice. All you need to do is set the position on the element in your CSS:

CSS

#help {
    position: absolute;
}

You can then refactor your jQuery code to cut it down drastically, and also animate your element as required:

var help;

$('.helpPin').click(function(){
    var bottom = help ? '0' : '-206px';
    $("#help").animate({ 'bottom' : bottom });
    help = !help;
});
Sign up to request clarification or add additional context in comments.

3 Comments

Short, intelligent, efficient. hat tip
help = !help; can you expand on this slightly please? Does it set a variable to the opposite of its current value?
@benhowdle89 that's correct - it will invert a boolean value.
2

A working demo

#help
{
    position: absolute;
}

$('.helpPin').click(function(){
        if(help == true){
            $('#help').animate({"bottom": '0'});
            help = false;
        } else {
            $('#help').animate({"bottom": '-206px'});
            help = true;
        }
    });

Comments

0

Try to code less and more efficient way.

var help;
    $('.helpPin').click(function(){

        if(help == true){               
            $('#help').css({
                'position' : 'absolute',
                'bottom' : '0'
            }).animate();
            help = false;
        } else {
            $('#help').css({
                'position' : 'absolute',
                'bottom' : '-206px'
            }).animate();
            help = true;
        }
    });

Hope, you'are looking for the same.

1 Comment

I had given you the logic that about how to start up. What sort of animation you want? You want me to give the exact code? Your code exist on jsfiddle?
0

You should set the position using CSS, in the meantime......

var help;
$('.helpPin').click(function () {

     var bottom = (help) ? '0' : '-206px';

     $('#help')
          .css({ 'position': 'absolute' });
          .animate({ 'bottom': bottom });

     help = !help;
}); 

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.