Don't overwrite css properties, but add to them with jQuery

后端 未结 3 533
情歌与酒
情歌与酒 2021-01-23 00:29

I just want to know how to use the jquery .css() function to not overwrite, but to add additional values to css properties.

For instance, I have an element that is curre

3条回答
  •  滥情空心
    2021-01-23 01:01

    Since transform is kind of a shorthand, the values must be combined... There's no increment method.

    What you could do, is retrieve the previous value and append the new one:

    Updated JsFiddle

    var rotation = 0;
    function rotate() {
        rotation += 90;
        var rotationString = 'rotate(' + rotation + 'deg)';
        var prev = $('#square').css('transform');
        $('#square').css('transform', prev + " " + rotationString);
    }
    

提交回复
热议问题