javascript - 适合容器中的旋转图像 jQuery css

标签 javascript jquery html css

我开发了这个脚本来通过单击按钮来旋转图像...

问题

它旋转得很好,但问题是一旦图像旋转 90 度或 270 度,它就不再适合主容器......

我希望它在旋转 90/270 度后能够与容器配合。

$(document).ready(function() {
  var degrees = 0;
  $("button").click(function rotateMe(e) {
    degrees += 90;
    //$('.img').addClass('rotated'); // for one time rotation
    $(".content").css({
      'transform': 'rotate(' + degrees + 'deg)',
      '-ms-transform': 'rotate(' + degrees + 'deg)',
      '-moz-transform': 'rotate(' + degrees + 'deg)',
      '-webkit-transform': 'rotate(' + degrees + 'deg)',
      '-o-transform': 'rotate(' + degrees + 'deg)'
    });
  })
});
.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;
}

div {
  border: 1px solid blue;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div class="container">
  <button>
Rotate
</button>
  <div class="content">
    <img class="img" src="http://projectpuffin.audubon.org/sites/g/files/amh646/f/styles/hero_mobile/public/atpu-scholtz.jpg" />
  </div>
</div>

最佳答案

使用scale在旋转时缩小图像,并使用 Degrees % 180了解图像的方向以缩小图像

$(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 */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">

  <button>
Rotate
</button>
  <div class="content">

    <img class="img" src="http://projectpuffin.audubon.org/sites/g/files/amh646/f/styles/hero_mobile/public/atpu-scholtz.jpg" />
  </div>
</div>

关于javascript - 适合容器中的旋转图像 jQuery css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49867848/

相关文章:

javascript - Chrome 扩展程序可更改首选项文件中的值

javascript - emberjs 从另一个 View 锚定

javascript - jquery tipsy 坏了

jquery - 通过ajax调用刷新部分 View 挂起应用程序

php - 为什么我的提交表单会将我带到我的 php 页面?

javascript - 捕获 block 在节点提取中不起作用

javascript - 垂直边框左侧的间距

html - 子菜单,设置位置更靠左

html - 网站上的图片未在 Chrome 上显示

html - 在浏览器上放大和缩小时如何保持内容在原位