[英]Position absolute divs on the same distance between each other
I have a relative positioned container with many absolute positioned divs inside. 我有一个相对定位的容器,里面有许多绝对定位的div。 I need to position the inners div in the way they are on the same distance between each other using CSS 'left' property.
我需要使用CSS'left'属性将内部div定位为彼此之间的距离相同。
I can achieve by setting fixed left values to each of them, but prefer to use a function which calculates it regardless how many inner divs a have. 我可以通过为每个左值设置固定的左值来实现,但是更喜欢使用一个函数来计算它,而不管内部有多少div。
So the desired result is illustrated on the picture below and following by the code I have at the moment. 因此,期望的结果在下面的图片中进行了说明,下面是我目前的代码。 Basically I've just stuck with calculations :)
基本上我只是坚持计算:)
Thanks in advance for any help. 在此先感谢您的帮助。
HTML 的HTML
<div id="bubbles-container" populate-bubbles></div>
CSS 的CSS
#bubbles-container{
position: relative;
width: 100%;
height: 300px;
}
.bubble {
position: absolute;
border: 1px solid rgba(0, 0, 0, 0.2);
bottom: 0;
border-radius: 10px;
height: 15px;
width: 15px;
}
JS/ANGULAR JS /英语
app.directive('populateBubbles', [function(){
return function(scope, element, attr){
console.log(element);
for (i = 1; i <= 10; i++){
element.append('<div class="bubble bubble' + i + '"></div>');
}
element.find('.bubble').each(function(){
var bubbleLength = $(this).length;
var bubbleWidth = $(this).width();
var containerWidth = element.width();
...
})
}
}])
Do not bother you with absolute positions, use flex. 不要打扰您绝对的位置,请使用flex。 You won't have to calculate :
您无需计算:
#bubbles {
display: flex;
justify-content: space-between;
align-items: flex-end;
}
Look at this -> https://jsbin.com/gepufuy/1/edit?html,css,js,output 看看这个-> https://jsbin.com/gepufuy/1/edit?html,css,js,output
If you really cannot use flexbox, here is an other solution using 如果您确实无法使用flexbox,则可以使用另一种解决方案
display: table;
display: table-cell;
Which is compatible with IE10. 与IE10兼容。
-> https://jsbin.com/gepufuy/6/edit?html,css,js,output -> https://jsbin.com/gepufuy/6/edit?html,css,js,输出
With jQuery, you can try something like this: 使用jQuery,您可以尝试执行以下操作:
var i = 0;
var container_width = $('#bubbles-container').width();
var bubble_number = $('.bubble').length;
$('.bubble').each(function() {
var left_space = (container_width / bubble_number) * i;
$(this).css({left: left_space + 'px'});
i++;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.