Adjusting div width and height after rotated(旋转后调整div的宽度和高度)
问题描述
如果我有这些规则:
<上一页>宽度:50px;高度:100px;-moz 变换:旋转(0 度)然后一个事件将转换更改为:
<上一页>-moz 变换:旋转(90 度)从逻辑上讲,这不应该自动交换宽度和高度吗?我需要旋转来切换宽度和高度以进行准确的位置检测.
谢谢,
乔
似乎转换是在其他所有内容之后应用的,所以宽度和高度没有更新.我能想到的最佳解决方案是使用旋转矩阵自己计算旋转尺寸:
[ cos X -sin X ] [ width ][ 罪 X cos X ] [ 高度 ]将其转换为 JavaScript 很简单.您需要旋转所有四个角 (0,0) (w,0) (0,h) (w,h),然后旋转的尺寸是旋转边界矩形的宽度和高度.
var angle = angle_in_degrees * Math.PI/180,sin = Math.sin(角度),cos = Math.cos(角度);//(0,0) 保持为 (0, 0)//(w,0) 旋转var x1 = cos * 宽度,y1 = sin * 宽度;//(0,h) 旋转var x2 = -sin * 高度,y2 = cos * 高度;//(w,h) 旋转var x3 = cos * 宽度 - sin * 高度,y3 = sin * 宽度 + cos * 高度;var minX = Math.min(0, x1, x2, x3),maxX = Math.max(0, x1, x2, x3),minY = Math.min(0, y1, y2, y3),maxY = Math.max(0, y1, y2, y3);var rotateWidth = maxX - minX,旋转高度 = maxY - minY;If I have these rules:
width:50px; height:100px; -moz-transform: rotate(0deg)
and then an event changes the transform to:
-moz-transform: rotate(90deg)
logically, shouldn't that automatically exchange the width and the height? I need the rotate to switch width and height for accurate position detection.
Thanks,
Joe
It seems like the transform is applied after everything else, so the width and height aren't updated. The best solution I can think of is to calculate the rotated dimensions yourself, using the rotation matrix:
[ cos X -sin X ] [ width ]
[ sin X cos X ] [ height ]
It's straightforward to translate this into JavaScript. You need to rotate all four corners (0,0) (w,0) (0,h) (w,h) and then the rotated dimensions are the width and height of the rotated bounding rectangle.
var angle = angle_in_degrees * Math.PI / 180,
sin = Math.sin(angle),
cos = Math.cos(angle);
// (0,0) stays as (0, 0)
// (w,0) rotation
var x1 = cos * width,
y1 = sin * width;
// (0,h) rotation
var x2 = -sin * height,
y2 = cos * height;
// (w,h) rotation
var x3 = cos * width - sin * height,
y3 = sin * width + cos * height;
var minX = Math.min(0, x1, x2, x3),
maxX = Math.max(0, x1, x2, x3),
minY = Math.min(0, y1, y2, y3),
maxY = Math.max(0, y1, y2, y3);
var rotatedWidth = maxX - minX,
rotatedHeight = maxY - minY;
这篇关于旋转后调整div的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:旋转后调整div的宽度和高度
基础教程推荐
- fetch 是否支持原生多文件上传? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
