简体   繁体   English

将元素保留在父元素的底部

[英]Keep Element At The Bottom Of the Parent ELEMENT

I have a simple animation. 我有一个简单的动画。 Take a look at it. 看看吧。 http://jsfiddle.net/5wJ5D/ http://jsfiddle.net/5wJ5D/

MY QUESTION: 我的问题:

How can I make the children elements stay at the bottom of the screen. 如何让子元素保持在屏幕的底部。 After the animation goes for a while, all the elements eventually go to the bottom because eventually one of them is height:100%.. how can I get the elements to be at the bottom of the screen even before this happens? 动画进行一段时间后,所有元素最终都会到达底部,因为最终其中一个是高度:100%..即使在此之前,我怎样才能让元素位于屏幕的底部?

JavaS: Java类:

setInterval(function(){
    var height = Math.floor(Math.random() * (10 - 1 + 1)) + 1;
    $('#container').prepend('<div style="height:'+height+'0%"></div>');
}, 500);
html, body, #container {
    height:100%;
}
#container > div {
    width:20px;
    background:black;
    display:inline-block;
}

I tried position:absolute; 我试过位置:绝对; bottom:0.. but that obviously doesn't work. 底部:0 ..但这显然不起作用。

My solution require that you wrap your container with an other div. 我的解决方案要求你用另一个div包装你的容器。 I've called it outerContainer . 我称之为outerContainer Once you have that and set its height at 100%, you can use the display table trick : 一旦你有了它并将其高度设置为100%,你可以使用显示table技巧:

#outerContainer{
    display : table;
}

#container{
    display : table-cell;
    vertical-align:bottom;
}

Fiddle : http://jsfiddle.net/5wJ5D/2/ 小提琴: http//jsfiddle.net/5wJ5D/2/

You can set the background property to a linear-gradient and set the inner div heights to 100% 您可以将background属性设置为线性渐变,并将内部div高度设置为100%

CSS CSS

html, body, #container {
    height:100%;
}
#container > div {
    height: 100%;
    width:20px;
    display:inline-block;
}

JS JS

var count = 0;

var interval = setInterval(function () {
    if(count > 10){
        clearInterval(interval);
    }
    var height = Math.floor(Math.random() * (10 - 1 + 1)) + 1;
    $('#container').prepend('<div style="background: linear-gradient(to top, black '+ height +'0%, transparent '+ height +'0%);"></div>');
    count++;
}, 500);

jsFiddle 的jsfiddle

One quick and dirty solution 一个快速而又脏的解决方案

CSS: CSS:

#container > div {
    height: 100%;
    width:20px;
    display:inline-block;
    float:left;
    position:relative;
}

JS: JS:

setInterval(function(){
    var height = Math.floor(Math.random() * (10 - 1 + 1)) + 1;
    $('#container').prepend('<div style="bottom:'+(height-10)+'0%;height:' + height + '0%"></div>');
}, 500);

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

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