How to mix css3 transform properties without overriding (in realtime)?

前端 未结 6 1633
灰色年华
灰色年华 2020-12-11 00:26

Is there any way to combine (mix) css transform properties without overriding? For example there I want to rotate and scale. http://jsfiddle.net/hyzhak/bmyN3/

html<

6条回答
  •  悲哀的现实
    2020-12-11 00:56

    Currently there is no way to do this, it only accepts one command for transform at a time and cannot add or subtract like you'd like. Thus you'll have to add or remove a class depending on what the first and second type is and write a custom transform for that element

    Using a CSS preprocessor language like SASS or LESS could make combining transitions easier, the following allows for any number of transitions to be combined into one, but requires being told which ones to add (this is SCSS)

    .item {
      width:500px;
      height:500px;
      font-size:150px;
      background:blue;
    }
    
    $firstTransform: rotate(90deg);
    $secondTransform: scale(0.25, 0.25);
    
    @mixin transform($transform...) {
      -webkit-transform: $transform;
         -moz-transform: $transform;
          -ms-transform: $transform;
           -o-transform: $transform;
              transform: $transform;
    }
    .rotate-90 {
      @include transform($firstTransform);
    }
    .tiny-size {
      @include transform($secondTransform);
    }
    .both {
      @include  transform($firstTransform $secondTransform);
    }
    

    Demo

    Really the only way to do it currently would be to use javascript, essentially adding a class with a transform, saving a variable of that class' transforms, removing the class and doing the same with another class, then combine the two matrices of the class' transforms. EDIT: See brejep's javascript answer for more on this approach

提交回复
热议问题