1

我想要一个按钮,每次单击时将元素旋转 5 度。我已经能够用边框宽度做这种事情,比如

  $(document).on('click', '.brdr_width_up', function() {
        $(ws.currentCell).css({'border-top-width' : '+=1', 'border-right-width' : '+=1','border-bottom-width' : '+=1','border-left-width' : '+=1'});
 });

但是同样的想法似乎不适用于变换:旋转:

    $(document).on('click', '.rotate_cw', function() {
        $(ws.currentCell).css({'-webkit-transform': 'rotate(+=5deg)'});
        $(ws.currentCell).css({'-moz-transform': 'rotate(+=5deg)'});
        var deg = $(ws.currentCell).css({'-moz-transform': 'rotate()'});
     });

并且上面的 var 行并没有带回当前的旋转,所以我可以添加它。

有谁知道这样做的方法?

谢谢

4

2 回答 2

5

如果你不想弄乱矩阵,你可以使用数据属性来跟踪旋转,并做更多这样的事情:

$(document).on('click', '.rotate_cw', function() {
    var r = $(ws.currentCell).data('rot') + 5;
    $(ws.currentCell).css({
      '-webkit-transform': 'rotate('+r+'deg)',
         '-moz-transform': 'rotate('+r+'deg)',
          '-ms-transform': 'rotate('+r+'deg)',
           '-o-transform': 'rotate('+r+'deg)',
              'transform': 'rotate('+r+'deg)'
    }).data('rot', r);
});

小提琴

于 2013-05-08T16:48:04.897 回答
1

使用以下函数(来源:Get element -moz-transform:rotate value in jQuery

function getRotationDegrees(obj) {
    var matrix = obj.css("-webkit-transform") ||
    obj.css("-moz-transform")    ||
    obj.css("-ms-transform")     ||
    obj.css("-o-transform")      ||
    obj.css("transform");
    if(matrix !== 'none') {
        var values = matrix.split('(')[1].split(')')[0].split(',');
        var a = values[0];
        var b = values[1];
        var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
    } else { var angle = 0; }
    return angle;
}

要获得旋转,然后将角度添加 5 度并重新应用,例如

$(document).on('click', '.rotate_cw', function() {
   var target = $(ws.currentCell);
   var rotation = getRotationDegrees(target) + 5;
   target.css({
      '-webkit-transform': 'rotate(' + rotation + 'deg)'
   ,  '-moz-transform': 'rotate(' + rotation + 'deg)'
   ,  '-o-transform': 'rotate(' + rotation + 'deg)'
   ,  '-ms-transform': 'rotate(' + rotation + 'deg)'
   ,  'transform': 'rotate(' + rotation + 'deg)'
   });
});

这也适用于应用了默认旋转的元素。

jsFiddle:http: //jsfiddle.net/georeith/px8fL/7/

于 2013-05-08T16:49:02.833 回答