1

I have the following click event. I would also like to toggle the background-position: between top and bottom on click.

$('.close').click(function () {
    $('.banner-inner-content').slideToggle('slow', function () {
    });
});

So how can i toggle between?

.close{background-position:top}

and

.close{background-position:bottom;}

4 Answers 4

3

You can pass a function to .css():

$(this).css('background-position', function(i, val) {
    return val === 'top' ? 'bottom' : 'top';
});

Or you define a new class:

.close.bottom {
    background-position: bottom;
}

and use .toggleClass():

$(this).toggleClass('bottom');
Sign up to request clarification or add additional context in comments.

Comments

0

you could use two differents css class (one for top and the other for bottom) and when onClick event is shooted change it.

Comments

0
    $('.close').click(function () {
        if($('.banner-inner-content').hasClass('top')){
            $('.banner-inner-content').removeClass('top');
            $('.banner-inner-content').addClass('bottom');
    }else{
            $('.banner-inner-content').addClass('top');
            $('.banner-inner-content').removeClass('bottom');
    }
});

Comments

0

try using isvisible to check if its being displayed or not and add the respective position:

$('.close').click(function () {
    $('.banner-inner-content').slideToggle('slow', function () {
        if( $(this).is(':visible') ) {
            $(this).css({backgroundPosition: 'top'});
        }
        else {
            $(this).css({backgroundPosition: 'bottom'});
        }
    });
});

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.