简体   繁体   English

使用CSS Trasition时如何更改父div的高度?

[英]How to change height of parent div when using css trasition?

I have a problem with a CSS transition. 我对CSS转换有疑问。 I need to change the height of the parent div relative to the child divs in the transition. 我需要在过渡中更改父div相对于子div的高度。

I am using CSS which has a number of steps which slide from right to left as the user clicks continue (Magento onepage checkout with progress bar). 我正在使用CSS,它具有许多步骤,这些步骤会随着用户单击继续而从右向左滑动(带有进度条的Magento单页结帐)。

The problem is that the parent container .opc has a height of 970px but the heights of the additional steps vary so I need to find a way to make the parent DIV .opc change height to accommodate the sizes of the remaining steps. 问题是父容器.opc的高度为970px但是其他步骤的高度各不相同,因此我需要找到一种方法来使父DIV .opc更改高度以适应其余步骤的大小。

.opc { position:relative; overflow:hidden; height:970px; padding-top:20px; text-align:center; }

I've tried adding height: auto; 我试过增加height: auto; or height: 100%; height: 100%; but the remaining pages still don't fill the page and I am not sure how to solve it! 但其余页面仍无法填充页面,我不确定如何解决!

Is there a way to affect the height using jQuery or Javascript, maybe pure CSS? 有没有一种方法可以使用jQuery或Javascript(可能是纯CSS)来影响高度?

I'm thinking jQuery to detect which step the user is on the adjust the height of the container to fit the content? 我正在考虑使用jQuery来检测用户要调整容器高度以适合内容的哪个步骤?

<script>
    jQuery(document).ready(function(){

        if('#opc-billing'){
            jQuery('.opc').height(1200);
        }

        if('#opc-shipping'){
            jQuery('.opc').height(500);
        }
    })
</script>

Although the above solution doesn't work :( 虽然上述解决方案不起作用:(

Any help would be appreciated guys! 任何帮助将不胜感激的家伙!

Although it is not a very dynamic function what you have is getting there. 尽管它不是一个非常动态的功能,但是您所能达到的目标。 Try this: 尝试这个:

$(document).ready(function{
    var x = $("#opc-billing").height();
    var y = $("#opc-shipping").height();
    var opc = $(".opc");
    if (x === 600){       // - The number values are just examples of course.
        opc.height(1200);
    }
    if (y === 200){
        opc.height(400);
    } else {opc.height(300);}  // - default height, could be left blank if set by CSS(example - else{})
})

DEMO 演示

Keep in mind that when using the height() method results may be unexpected because height() returns the computed value of an element which does not include padding, border or margin and does not take into account something like when a page is zoomed in. Learn more about height() here . 请记住,使用height()方法时,结果可能会出乎意料,因为height()返回的元素的计算值不包括填充,边框或边距,并且不考虑诸如放大页面之类的内容。 在此处了解有关height()更多信息。 To get total height including padding, border and margins use outerHeight() . 要获取包括填充,边框和边距在内的总高度,请使用outerHeight()

UPDATE: I added some extra bells and whistles to the JSFiddle. 更新:我在JSFiddle中添加了一些额外的功能。 Check it out! 看看这个!

Here is a quick demo that might help: 这是一个可能有帮助的快速演示:

I am simply adding a CSS transition property to the parent as well, and adjusting the height at the same time as the position of the child. 我也只是向父级添加CSS过渡属性,并在调整子级位置的同时调整高度。

http://jsfiddle.net/qF3u7/ http://jsfiddle.net/qF3u7/

.parent {
    background-color: lightyellow;
    -webkit-transition: height 2s;
    height: 50px;
}

.transit {
    position: relative;
    top: 0;
    width: 50px;
    height: 50px;
    border: 1px solid red;
    -webkit-transition: top 2s;
}

PS: Run this in chrome as I didn't bother with the other browser prefixes for the CSS. PS:在chrome中运行此代码,因为我不必理会CSS的其他浏览器前缀。

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

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