设置变换后图像的宽度和高度:旋转

Yos*_*ria 7 html css

我有一个宽度为 190px 高度为 260px 的 div,我在该 div 上分配了 img 标签,当我上传显示图像之前的图像时,之后我旋转图像但图像的宽度和高度没有变化div,我使用了继承,关于位置和显示的一切,但一点都不好..上传后的状态 错误的事情

don*_*lys 7

我想出了一种自动化的方法,如下所示:

首先,我获得图像的自然高度和宽度(来自 onload 触发器):

var naturalWidth = event.currentTarget.naturalWidth
var naturalHeight = event.currentTarget.naturalHeight
Run Code Online (Sandbox Code Playgroud)

然后我使用纵横比计算变换比例并生成变换样式,如下所示(伪代码):

对于 90 度(y 轴偏移):

const scale = naturalWidth > naturalHeight ? naturalHeight / naturalWidth : 1;
const yshift = -100 * scale;
const style = `transform:rotate(90deg) translateY(${yshift}%) scale(${scale}); transform-origin: top left;`
Run Code Online (Sandbox Code Playgroud)

对于 270 度(x 轴偏移):

const scale = naturalWidth > naturalHeight ? naturalHeight / naturalWidth : 1;
const xshift = -100 * scale;
const style = `transform:rotate(270deg) translateX(${xshift}%) scale(${scale}); transform-origin: top left;`
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。


小智 2

嘿,

请尝试此代码。

var $=jQuery.noConflict();
$(document).ready(function(){
	$('#RotateButton').click(function(){
	   $('.col').toggleClass("afterRot");
	});
});
/* ----- IE Support CSS Script ----- */
var userAgent, ieReg, ie;
userAgent = window.navigator.userAgent;
ieReg = /msie|Trident.*rv[ :]*11\./gi;
ie = ieReg.test(userAgent);
if(ie) {
  $(".col").each(function () {
    var $container = $(this),
        imgUrl = $container.find("img").prop("src");
    if (imgUrl) {
      $container.css("backgroundImage", 'url(' + imgUrl + ')').addClass("custom-object-fit");
    }
  });
}
Run Code Online (Sandbox Code Playgroud)
body { padding: 0; margin: 0; }
.col { position: relative; display: block; width:100vh; height: 100vh; }
.afterRot{ transform: rotate(90deg); object-fit: cover; }
.col img { position: absolute; top: 0; left: 0; right: 0; width: 100%; height: 100%;	object-fit: cover; }
.custom-object-fit { position: relative; background-size: cover; background-position: center center; }
.custom-object-fit img { opacity: 0; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mob">
	<button type="button" id="RotateButton"> Rotate </button>
	<div class="col">     
    	<img class="nor" id="rowImg" src="https://cdn-images-1.medium.com/max/1600/1*tSyuv3ZRCfsSD5aXB7v8DQ.png">
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)