Skip to content Skip to sidebar Skip to footer

Adding Amount To Background-position On Click (jquery)

I pretty new to js and jquery so please bear with me. I'd like to change the background-position and add 1% on #div1 while clicking on #button1 and take -1% on #div1 while clicking

Solution 1:

Like this:

functionmakeClicker(index) {
    $('#leftbutton' + index).click(function() {
        var bPos = $('#div' + index).css('background-position');
        bPos = bPos.replace(/%/g, '').split(' ');
        bPos = (-1 + parseInt(bPos[0], 0)) + '% ' + bPos[1] + '%';
        $('#div' + index).css('background-position', bPos);
    });
    $('#rightbutton' + index).click(function() {
        var bPos = $('#div' + index).css('background-position');
        bPos = bPos.replace(/%/g, '').split(' ');
        bPos = (1 + parseInt(bPos[0], 0)) + '% ' + bPos[1] + '%';
        $('#div' + index).css('background-position', bPos);
    });
}

for(var i = 1; i <= 5; i++)
    makeClicker(i);

EDIT: Fixed mistakes.

Demo

Post a Comment for "Adding Amount To Background-position On Click (jquery)"