gpt4 book ai didi

javascript - 列表在元素放置上向下移动

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:10 25 4
gpt4 key购买 nike

我很难解决以下问题:我的列表之一 #cardHolder 会在我将元素拖入其中时向下移动。我知道它与 CSS 相关,但我似乎无法修复它。感谢您的帮助。

$(document).ready(function () {
$( "#cardHolder, #hand" ).sortable({
connectWith: ".connectedSortable",
receive: function(event, ui) {
var $this = $(this);
if ($this.children('li').length > 1 && $this.attr('id') != "hand") {
console.log('Only one per list!');
$(ui.sender).sortable('cancel');
}
}

}).disableSelection();
});
#trumpHolder{
border: 5px solid #ccc;
padding: 5px;
height: 250px;
width: 150px;
margin-top: 5px;
margin-bottom: 5px;
margin-left: auto;
margin-right: auto;

}
#holderWrapper{
width: 100%;
text-align: center;

}
#cardHolder {
border: 5px solid #ccc;
padding: 5px;
height: 250px;
width: 150px;
margin: 5px 5px 5px 5px;
display: inline-block;
}
#cardHolderOther{
border: 5px solid #ccc;
padding: 5px;
height: 250px;
width: 150px;
display: inline-block;
margin: 5px 5px 5px 5px;
}
.table{
display: table;
margin: 0 auto;


}
#hand {
height: 250px;
border: 5px solid #ccc;
padding: 0;
width: 100%;
clear: both;
margin-top: 5px;

}

#cardHolder li, #hand li {
margin: 7px 7px 7px 7px;
padding: 5px;
font-size: 1.2em;
width: 120px;
display: inline-block;
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.8.24/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://code.jquery.com/ui/1.8.24/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="boerenbridge.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="boerenbridge.css">


<ul id="trumpHolder" class="connectedSortable">

</ul>
<div id="holderWrapper">
<ul id="cardHolder" class="connectedSortable">

</ul>
<ul id="cardHolderOther" class="connectedSortable">

</ul>
</div>
<div class="table">
<ul id="hand" class="connectedSortable">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>

代码如下:

HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Boerenbridge</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.8.24/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://code.jquery.com/ui/1.8.24/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="boerenbridge.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="boerenbridge.css">
</head>
<body>
<ul id="trumpHolder" class="connectedSortable">

</ul>
<div id="holderWrapper">
<ul id="cardHolder" class="connectedSortable">

</ul>
<ul id="cardHolderOther" class="connectedSortable">

</ul>
</div>
<div class="table">
<ul id="hand" class="connectedSortable">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
</body>

CSS

#trumpHolder{
border: 5px solid #ccc;
padding: 5px;
height: 250px;
width: 150px;
margin-top: 5px;
margin-bottom: 5px;
margin-left: auto;
margin-right: auto;

}
#holderWrapper{
width: 100%;
text-align: center;

}
#cardHolder {
border: 5px solid #ccc;
padding: 5px;
height: 250px;
width: 150px;
margin: 5px 5px 5px 5px;
display: inline-block;
}
#cardHolderOther{
border: 5px solid #ccc;
padding: 5px;
height: 250px;
width: 150px;
display: inline-block;
margin: 5px 5px 5px 5px;
}
.table{
display: table;
margin: 0 auto;


}
#hand {
height: 250px;
border: 5px solid #ccc;
padding: 0;
width: 100%;
clear: both;
margin-top: 5px;

}

#cardHolder li, #hand li {
margin: 7px 7px 7px 7px;
padding: 5px;
font-size: 1.2em;
width: 120px;
display: inline-block;
}

Javascript/Jquery

$(document).ready(function () {
$( "#cardHolder, #hand" ).sortable({
connectWith: ".connectedSortable",
receive: function(event, ui) {
var $this = $(this);
if ($this.children('li').length > 1 && $this.attr('id') != "hand") {
console.log('Only one per list!');
$(ui.sender).sortable('cancel');
}
}

}).disableSelection();
});

最佳答案

在使用display: inline-block 时将vertical-align: top; 添加到#cardHolder 以确保它粘在顶部,否则它将尝试将其对齐到默认值 baseline, see this link有关 vertical-aligninline 元素的更多信息。

下方左侧是使用默认 baseline 值的 vertical-align 示例,右侧是使用 top 的示例 值。

.inline-list {
width: 250px;
float: left;
}

.inline-list div {
display: inline-block;
border: 3px solid #444;
margin: 0;
padding: 0;
}

.inline-list .cardholder {
width: 100px;
height: 100px;
}

.inline-list.align .cardholder {
vertical-align: top;
}
<div class="inline-list">
<div class="cardholder">Some text</div>
<div class="cardholder"></div>
</div>

<div class="inline-list align">
<div class="cardholder">Some text</div>
<div class="cardholder"></div>
</div>

关于javascript - 列表在元素放置上向下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33197346/

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