css - 设置转换后图像的宽度和高度:旋转
问题描述
我有一个宽度:190px 和高度:260px 的 div,我在该 div 上分配了 img 标签,当我上传显示图像之前的图像时,之后我旋转图像但图像的宽度和高度没有改变div,我用过继承,关于位置和显示的一切,但一点都不好..
解决方案
我想出了一种自动化的方法,如下所示:
首先,我得到了图像的自然高度和宽度(来自 onload 触发器):
naturalWidth = event.currentTarget.naturalWidth
naturalHeight = event.currentTarget.naturalHeight
然后我使用纵横比计算变换比例并生成如下变换样式(伪代码):
对于 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;"
对于 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;"
希望这可以帮助。
推荐阅读
- python - 绘制顺序覆盖 Mousedown 事件绘制的图像
- excel - 代码中引用的 Excel 工作簿在任务栏中无处可见
- angular - 使用 Jamine 和 Angular 测试链式窗口方法
- prolog - 同时访问两个或多个可回溯谓词的解决方案
- python - 创建元组列表
- javascript - Vue-Router (v.3):addRoute 方法不会覆盖共享相同路径的现有路由
- go - Golang 选择不进入“默认”情况
- google-coral - 无法在 Raspbery Pi 上更新 Coral SW
- c# - List.IndexOf(x):错误=>方法'IndexOf'没有重载在c#中需要1个参数
- c# - Blazor 中的本机窗口