gpt4 book ai didi

jquery - float 底部 - 彼此顶部

转载 作者:太空宇宙 更新时间:2023-11-03 18:40:16 24 4
gpt4 key购买 nike

大家好,我想寻求有关 css - jquery 拖放问题的帮助。我熟悉将 div 定位到容器底部(position:relative -> position:absolute;底部:0;)。

目标:我想从存储 div 中拖放元素并将这些元素放到另一个 div 中,然后将这些元素从下到上垂直放置。就像 build 一座塔(2D)之类的。我怎样才能将这些元素漂浮到新容器的底部并相互叠加?

谢谢 - 贾尼

#drop {
height:300px;
background:#EDCCE9;
}
#drag {
margin-top:10px;
height:50px;
background:#B8D8E3;
}
#drag img {
display: inline-block;
margin-right:10px;
}
.temp {
display: block;
}

$('.item').draggable({
containment: 'document',
revert: true,
helper: 'clone',
start: function () {
contents = $(this).html();
currentValue = $(this).attr('value');
}
});
$('#drop').droppable({
hoverClass: 'border',
accept: '.item',
drop: function (event, ui) {
$(this).append($(ui.draggable).clone());
$('#drop .item').addClass('temp');
$('.temp').removeClass('ui.draggable item');
$(".temp").draggable({
containment: 'document',
revert: true
});
}
});
$('#drag').droppable({
hoverClass: 'border',
accept: '.temp',
drop: function (event, ui) {
$(ui.draggable).remove();
}
});

<div id="drop"></div>
<div id="drag">
<img src="blocks/a.png" width="50px" height="50px" class="item" />
<img src="blocks/b.png" width="50px" height="50px" class="item" />
<img src="blocks/c.png" width="50px" height="50px" class="item" />
<img src="blocks/d.png" width="50px" height="50px" class="item" />
<img src="blocks/e.png" width="50px" height="50px" class="item" />
<img src="blocks/f.png" width="50px" height="50px" class="item" />
</div>

最佳答案

遗憾的是,没有“float:bottom;”为此类型构造。你将不得不使用 position:absolute;正如你所描述的。

要按照您的意愿执行此操作,您可以依靠 jQuery 来应用正确的 bottom 值(您可以这样做,我的测量容器中已有的元素数 ,然后将此值除以每个元素的高度,前提是它们都与示例中的大小相同。)

要计算堆栈中的元素,可以使用.length() read here for more

您可以使用 :nth-child 选择器仅使用 CSS 来完成此操作。这个问题是,所有浏览器(我指的是 IE8 及以下版本)均不完全支持它,请参阅 can i use it了解详情。

使用此方法,您只需创建一个像这样的列表,其中指定的 bottom 值会增加:

li:nth-child(1) {
bottom:0;
}
li:nth-child(2) {
bottom:50px;
}
li:nth-child(3) {
bottom:100px;
}
...

这个广告令人作呕。那么问题就来了,如果你知道你需要考虑多少元素,而且如果你有很多元素需要考虑,它只需要添加大量的 CSS。


如果它们的大小不完全相同,那么您将不得不使用 jQuery 方法,而不是简单地计算堆栈中已有元素的数量,您需要拉出它们的高度并使用它来计算正确的 bottom

关于jquery - float 底部 - 彼此顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17783419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com