fit rotated image in container jQuery css

后端 未结 1 632
遇见更好的自我
遇见更好的自我 2021-01-22 06:39

I developed this script to rotate an image with button clicking...

PROBLEM

It rotates well, but the problem is that once the image is rotated 9

1条回答
  •  礼貌的吻别
    2021-01-22 07:21

    use scale to scale down the image when rotated and degrees % 180 to know the orientation of the image to scale it back

    $(document).ready(function() {
      var degrees = 0;
      $("button").click(function rotateMe(e) {
        degrees += 90;
        if (degrees % 180 == 0) 
          $(".content").css({
            'transform': 'rotate(' + degrees + 'deg) scale(1)'
          });
        else
          $(".content").css({
            'transform': 'rotate(' + degrees + 'deg) scale(0.8)'
          });
      })
    });
    .rotated {
      transform: rotate(90deg);
      -ms-transform: rotate(90deg);
      -moz-transform: rotate(90deg);
      -webkit-transform: rotate(90deg);
      -o-transform: rotate(90deg);
    }
    
    .img {
      transition: all 0.5s ease;
    }
    
    .container {
      border: 1px solid blue;
      display: inline-block;
      width: 300px;
      margin: 0 auto;
      text-align: center; /* added */
    }
    
    div {
      border: 1px solid blue;
      display: inline-block;
      transition: all .5s linear; /* added */
    }
    
    

    0 讨论(0)
提交回复
热议问题