13

将宽度/高度更改为 CSS 旋转的 div 触发顶部/左侧重新定位之后,我需要一些帮助来解决 CSS 旋转和动态宽度/高度。

我理解transform-origin并认为我需要在更新元素的宽度或高度的同时动态更新它。我只对技术解决方案感兴趣,而不是任何跨浏览器的聪明才智,因此演示只使用-webkit前缀。

HTML

<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>

CSS

#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;
}

JavaScript

$('#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)'});
});

第二个演示中,添加 CSS

#rotated {
    -webkit-transform-origin: 100px 50px;
    -webkit-transform: rotate(60deg);
}

并将角度滑块更新value60

Angle: <input id="angle" type="range" min="0" max="360" step="1" value="60">

通过滑块修改宽度/高度时会产生正确的结果,因为元素在x, y尺寸上增长和缩小而不移动位置。但是,现在不再围绕所需的(中心点)原点旋转元素。

我已经尝试了一些变体(mousedown、mousemove、change),我认为会在修改宽度/高度之前设置原点,但<div>仍在移动位置。

$('#width, #height, #angle').change(function() {
    $('#rotated').css({'-webkit-transform-origin': $('#width').val()/2 + 'px' + $('#height').val()/2 + 'px'});
});

我假设 jQuery 正在同时应用 CSS 更改,而我认为原点需要在宽度/高度更改之前更新。

基本上,如果形状已经旋转,我希望形状始终围绕中心点旋转,并且在修改宽度/高度时不移动。

4

1 回答 1

14

小提琴演示!!!

我首先要告诉你为什么会这样。正如您所看到的,当您的 div 没有旋转时,没有问题。那么为什么会发生这种奇怪的行为呢?

回答:因为您要求它...当您更改高度或宽度时,div 的 50% 或中间点正在发生变化。因此,浏览器将您的 div 放置在未旋转的新 50% 的 DIV 的位置,然后旋转它。

解决方案是将旋转的 div 包装在另一个具有固定宽度和高度的 div 中(比如说“#wrapper”),并且overflow:visible.

然后将#rotated 放在#wrapper 内并在宽度或高度变化时计算变化并以始终位于#wrapper 中心的方式翻译#rotated div(例如,对于宽度,翻译为- (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>

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;
}

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)"});
});
于 2011-09-01T19:07:59.653 回答