简体   繁体   English

用CSS3变换设置div的高度(旋转)

[英]Set height on div with CSS3 transform (rotate)

Goal 目标

I'm working on a collapsible sidebar using jQuery for animation. 我正在使用jQuery动画制作可折叠的侧边栏。 I would like to have vertical text on the sidebar that acts as a label and can swap on the animateOut/animateIn effect. 我希望侧边栏上有垂直文本作为标签,可以交换animateOut / animateIn效果。

Normally I would use an image of the text that I've simply swapped vertically, and switch it out on animation, but with CSS3 transforms I'd like to get it to work instead. 通常情况下,我会使用我只是垂直交换的文本图像,并在动画中将其切换出来,但是对于CSS3转换,我想让它工作。

Problem 问题

The problem I'm facing is that setting the height on my rotated container makes it expand horizontally (as it's rotated 90deg) so that doesn't work. 我面临的问题是,在旋转的容器上设置高度使其水平扩展(因为它旋转了90度),这样就不起作用了。 I then tried to set the width (hoping it would expand vertically, acting as height), but that has an odd effect of causing the width of my parent container to expand as well. 然后我尝试设置宽度(希望它会垂直扩展,充当高度),但这会产生奇怪的效果,导致我父容器的宽度也会扩展。

Fix? 固定?

How can I set the height on a rotated (transformed) element without affecting the width of the parent container? 如何在不影响父容器宽度的情况下在旋转(转换)元素上设置高度? So far I have been unable to do this. 到目前为止,我一直无法做到这一点。

Live Example 实例

Here's a fiddle that demonstrates my problem: Fiddle 这是一个演示我的问题的小提琴小提琴

The collapse-pane class is what I have rotated and contains the span I have my text inside. collapse-pane类是我旋转的,包含我的文本里面的范围。 You'll notice it has a width set, that widens the border, but also affects the parent container. 您会注意到它有一个宽度设置,它会扩大边框,但也会影响父容器。

The code: 代码:

CSS: CSS:

.right-panel{
    position:fixed;
    right:0;
    top:0;
    bottom:0;
    border:1px solid #ccc;
    background-color:#efefef;
}
.collapse-pane{
    margin-top:50px;
    width:30px;
    border:1px solid #999;
    cursor:pointer;
    /* Safari */
    -webkit-transform: rotate(-90deg);
    /* Firefox */
    -moz-transform: rotate(-90deg);
    /* IE */
    -ms-transform: rotate(-90deg);
    /* Opera */
    -o-transform: rotate(-90deg);
    /* Internet Explorer */
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);   
}
.collapse-pane span{
    padding:5px;
}

HTML HTML

<div class="right-panel">
    <div class="collapse-pane">
        <span class="expand">Expand</span>
    </div>
    <div style="width:0;" class="panel-body">
        <div style="display:none;" class="panel-body-inner">
            adsfasdfasdf
        </div>
    </div>
</div>

Here's an image also showing the problem, so you don't have to view the Fiddle. 这是一个也显示问题的图像,因此您不必查看小提琴。

奇怪的div间距

Update 更新

I've made the problem statement a little clearer, as my original post was confusing the issues. 我使问题陈述更加清晰,因为我的原始帖子让问题混乱。

Thanks for any help! 谢谢你的帮助!

If you don't want the rotated divs size expand it's parent size, you need to take it out of the flow. 如果您不希望旋转的divs大小扩展它的父级大小,则需要将其从流中取出。 You may use absolute positioning for this. 您可以使用绝对定位。

The following demo uses this technique and also positions the "expand" element by setting a transform orign and top/right values. 以下演示使用此技术,并通过设置变换orign和top / right值来定位“expand”元素。

DEMO DEMO

CSS : CSS:

.right-panel {
    position:fixed;
    right:0;
    top:0;
    bottom:0;
    border:1px solid #ccc;
    background-color:#efefef;
}
.collapse-pane {
    position:absolute;
    border:1px solid #999;
    cursor:pointer;
    top:20%;
    right:100%;
    -ms-transform-origin:100% 100%;
    -webkit-transform-origin: 100% 100%;
    transform-origin: 100% 100%;
    /* Safari */
    -webkit-transform: rotate(-90deg);
    /* Firefox */
    -moz-transform: rotate(-90deg);
    /* IE */
    -ms-transform: rotate(-90deg);
    /* Opera */
    -o-transform: rotate(-90deg);
    /* Internet Explorer */
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}
.collapse-pane span {
    padding:5px;
}

jQuery : jQuery:

$(document).ready(function () {
    var height = $(".right-panel").height();
    $('.collapse-pane').click(function () {
        if ($(".collapse-pane span").html() == "Expand") {
            $(".panel-body").animate({
                width: 200
            }, 400);
            $(".panel-body-inner").fadeIn(500);
            $(".collapse-pane span").html("Collapse");
        } else {
            $(".panel-body").animate({
                width: 00
            }, 400);
            $(".panel-body-inner").fadeOut(300);
            $(".collapse-pane span").html("Expand");
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM