-1

I've got a div say <div style="background-position: 50% 0"></div>.

What I want is to increase background-position 50% more each time I click on it, i.e. <div style="background-position: 100% 0"></div>, then <div style="background-position: 150% 0"></div> and so on. is there a way to do that with jquery?

sdvnksv
  • 9,350
  • 18
  • 56
  • 108

3 Answers3

0

Try using .animate() jquery method:

$('div').click(function () {
    $(this).animate({
        backgroundPositionX: '+=50%'
    },0);
});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • this actually worked out. thanks! accordingly, if I'd want to reduce 50% I'll have to change it to '-=50%', right? – sdvnksv Nov 21 '13 at 10:59
0

fiddle Demo

$('div').click(function () {
    var perc = parseInt($(this).attr('style').match(/\d+%/g)) + 50;
    $(this).attr('style', "background-position: " + perc + "% 0");
});


or

fiddle Demo

$('div').click(function () {
    var perc = parseInt($(this).attr('style').match(/\d+%/g)) + 50;
    $(this).css("background-position", perc + "% 0");
});
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0
$('div').css('background-position', function (i, value) {
    var x = parseInt(value, 10);
    return (x + 50) + "%" + " 0";
});
Suresh
  • 186
  • 2
  • 14