and*_*dyb 12 css jquery css3 css-transforms
接下来从改变宽度/高度到CSS旋转div触发顶部/左侧重新定位我需要一些帮助来解决CSS旋转和动态宽度/高度.
我理解transform-origin并认为我需要在更新元素的宽度或高度的同时动态更新它.我只对技术解决方案感兴趣,而不是任何跨浏览器的聪明,因此演示只使用-webkit前缀.
<div id="wrap">
<div id="rotated">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
Width: <input id="width" type="range" min="20" max="400" step="1" value="200">
Height: <input id="height" type="range" min="20" max="400" step="1" value="100">
Angle: <input id="angle" type="range" min="0" max="360" step="1" value="0">
</div>
Run Code Online (Sandbox Code Playgroud)
#rotated {
background:lightblue;
border:1px dotted #000;
height:100px;
width:200px;
position:absolute;
top:300px;
left:100px;
overflow:hidden;
}
#width, #height, #angle {
display:block;
margin-bottom:10px;
width:200px;
}
Run Code Online (Sandbox Code Playgroud)
$('#width').change(function() {
$('#rotated').css({width:this.value + 'px'});
});
$('#height').change(function() {
$('#rotated').css({height:this.value + 'px'});
});
$('#angle').change(function() {
$('#rotated').css({'-webkit-transform': 'rotate(' + this.value + 'deg)'});
});
Run Code Online (Sandbox Code Playgroud)
在第二个演示中,添加CSS
#rotated {
-webkit-transform-origin: 100px 50px;
-webkit-transform: rotate(60deg);
}
Run Code Online (Sandbox Code Playgroud)
和更新value所述的角度滑块60
Angle: <input id="angle" type="range" min="0" max="360" step="1" value="60">
Run Code Online (Sandbox Code Playgroud)
通过滑块修改宽度/高度时产生正确的结果,因为元素在x, y没有移动位置的情况下在尺寸上增长和收缩.但是,现在旋转元素不再围绕所需(中心点)原点.
我已经尝试了一些变化(mousedown,mousemove,change),我认为在修改宽度/高度之前设置原点,但<div>仍然是移位位置.
$('#width, #height, #angle').change(function() {
$('#rotated').css({'-webkit-transform-origin': $('#width').val()/2 + 'px' + $('#height').val()/2 + 'px'});
});
Run Code Online (Sandbox Code Playgroud)
我假设jQuery同时应用CSS更改,而我认为原点需要在宽度/高度变化之前更新.
基本上我希望形状始终围绕中心点旋转,并且如果形状已经旋转,则在修改宽度/高度时不移动.
mrB*_*rna 14
我是第一个ganna告诉你为什么会发生这种情况.正如您所看到的那样,当您的div未旋转时,没有任何问题.那么为什么这种奇怪的行为会发生呢?
答:因为你要求它......当你改变高度或宽度时,div的50%或中间点正在改变.因此,浏览器将你的div放置在未旋转的DIV的新50%的位置,然后旋转它.
解决方案是将旋转的div包装在另一个具有固定宽度和高度的div中(比如说"#wrapper")overflow:visible.
然后将#rotated放在#wrapper中并在宽度或高度上进行更改,计算更改并将#rotated div转换为始终位于#wrapper中心的方式(例如,对于宽度,转换为 - (wrapWidth-newWidth)/ 2).
然后你旋转包装和瞧.
这是基于您的设置的代码:
HTML:
<div id="wrap">
Width: <input id="width" type="range" min="20" max="400" step="1" value="200">
Height: <input id="height" type="range" min="20" max="400" step="1" value="100">
Angle: <input id="angle" type="range" min="0" max="360" step="1" value="60">
</div>
<div id="rotateWrap">
<div id="rotated">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
#width, #height, #angle {
position:relative;
display:block;
margin-bottom:10px;
width:200px;
}
#rotateWrap{
position:absolute;
overflow:visible;
height:100px;
width:200px;
top:200px;
left:100px;
outline:2px solid red;
-webkit-transform-origin: 50% 50%;
}
#rotated {
background:lightblue;
position:relative;
overflow:hidden;
height:100px;
}
#wrap{
position:absolute;
}
Run Code Online (Sandbox Code Playgroud)
JAVASCRIPT
wPrev=$("#rotateWrap").width();
hPrev=$("#rotateWrap").height();
$('#width').mousedown(function(){
}).change(function() {
$("#rotated").width(newW=$(this).val())
.css({left: -(newW-wPrev)/2 + "px"});
});
$('#height').mousedown(function(){
}).change(function() {
$("#rotated").height(newH=$(this).val())
.css({top: -(newH-hPrev)/2 + "px"});
});
$('#angle').change(function() {
$("#rotateWrap").css({"-webkit-transform":"rotate("+$(this).val()+"deg)"});
});
Run Code Online (Sandbox Code Playgroud)