gpt4 book ai didi

javascript - 从 JQuery-UI 包含中排除元素

转载 作者:行者123 更新时间:2023-11-29 10:28:54 25 4
gpt4 key购买 nike

我正在尝试允许一个元素在另一个元素内可拖动/可调整大小,但不能在该元素的另一个子元素的区域内。

给定下表行: table row with blue area spanning three columns and the fifth column greyed out

我希望能够在表格行内的任意位置拖动蓝色区域并调整其大小,但不能拖动灰色表格单元格所在的位置(即它会在前四个单元格内自由移动/调整大小,但在到达最后一个单元格时停止一个)。

我可以使用 containment: '.middle tr'containment: $el.closest('tr'),(其中 $el 是蓝色元素的选择器)JQuery.draggableJQuery.resizable 中的选项,但我没能找到从包含中排除最后一列的方法。

如何从包含区域中排除元素?

代码示例:

$(function() {
$("td").droppable({
drop: function(event, ui) {
var draggable = ui.draggable;

var newLeft = draggable.offset().left - draggable.closest(".middle table").offset().left;
var childNum = Math.round((newLeft + 100) / 100);

var newContainer = $(this).closest('tr').find('td:nth-of-type(' + childNum + ')');

draggable.css({
top: '',
left: ''
});

newContainer.append(draggable);
},
});
$(".planning-spot").resizable({
grid: [100, 10000000],
handles: "e",
containment: ".middle tr"
}).each(function() {
var $el = $(this);
$el.draggable({
containment: $el.closest('tr'),
axis: 'x',
});
});
});
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}

table {
border-collapse: collapse;
overflow: hidden;
table-layout: fixed;
width: 500px;
margin: 20px;
}

tr {
position: relative;
background: #FFF;
}

td, th {
position: relative;
padding: 0;
width: 100px;
height: 30px;
border: 1px solid #000;
}

.planning-spot {
position: absolute;
top: 0;
left: 0;
height: 20px;
width: 90px;
border-radius: 10px;
margin: 5px;
text-align: center;
z-index: 1;
cursor: pointer;
color: #FFF;
width: 290px;
background: #3CF;
}

.no-planning {
background: #CCC;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<div class="wrapper">
<div class="middle">
<table>
<tbody>
<tr>
<td>
<div></div>
<div class="planning-spot">18 / 20</div>
</td>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
<td class="no-planning">
<div></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>

http://jsfiddle.net/xpvt214o/668937/

我知道我可以在 droppableresizabledropstop 函数中检查位置> 分别并在需要时撤消操作(这是我当前的解决方案),但我正在寻找一种解决方案,以防止它首先被移动到“不需要的”区域。

最佳答案

您可以使用 getBoundingClientRect() 确定可拖动区域,并将容器分配给一个数组,该数组采用每个可调整大小元素的宽度

$(function() {

$("td").droppable({
drop: function(event, ui) {
var draggable = ui.draggable;

var newLeft = draggable.offset().left - draggable.closest(".middle table").offset().left;
var childNum = Math.round((newLeft + 100) / 100);

var newContainer = $(this).closest('tr').find('td:nth-of-type(' + childNum + ')');

draggable.css({
top: '',
left: ''
});

newContainer.append(draggable);
},
});
$(".planning-spot").resizable({
grid: [100, 10000000],
handles: "e",
containment: ".middle tr"
}).each(function() {
var $el = $(this);
var bBoxTr = $el.closest('tr')[0].getBoundingClientRect();
var bBoxTd = $el.closest('tr').children(".no-planning")[0].getBoundingClientRect();
var x1 = bBoxTr.x, y1 = bBoxTr.y;
var x2 = bBoxTr.right-bBoxTd.width, y2 = bBoxTr.bottom;
$el.draggable({
// containment: $el.closest('tr'),
containment: [x1,y1,x2-$el.width()-10,y2],
axis: 'x',
});
});
});
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}

table {
border-collapse: no-collapse;
overflow: hidden;
table-layout: fixed;
width: 500px;
margin: 20px;
}

tr {
position: relative;
background: #FFF;
}

td, th {
position: relative;
padding: 0;
width: 100px;
height: 30px;
border: 1px solid #000;
}

.planning-spot {
position: absolute;
top: 0;
left: 0;
height: 20px;
width: 90px;
border-radius: 10px;
margin: 5px;
text-align: center;
z-index: 1;
cursor: pointer;
color: #FFF;
width: 290px;
background: #3CF;
}

.no-planning {
background: #CCC;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<div class="wrapper">
<div class="middle">
<table>
<tbody>
<tr id="test">
<td>
<div></div>
<div class="planning-spot">18 / 20</div>
</td>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
<td class="no-planning">
<div></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>

编辑:

因为 getBoundingClientRect(); 不是所有浏览器都支持,最好使用 jquery 特性,position(), height()宽度();

请参阅下面修改后的代码段,函数 getDragArea 返回一个包含拖动区域坐标的数组。我添加了一个监听器(鼠标悬停),它允许在需要时重置拖动区域。表格包裹在滚动的 div 中以测试行为。

$(function() {

$("td").droppable({
drop: function(event, ui) {
var draggable = ui.draggable;

var newLeft = draggable.offset().left - draggable.closest(".middle table").offset().left;
var childNum = Math.round((newLeft + 100) / 100);

var newContainer = $(this).closest('tr').find('td:nth-of-type(' + childNum + ')');

draggable.css({
top: '',
left: ''
});

newContainer.append(draggable);
},
});
$(".planning-spot").resizable({
grid: [100, 10000000],
handles: "e",
containment: ".middle tr"
}).each(function() {
$(this).on('mouseover',function(){
$(this).draggable({
containment: getDragArea($(this)),
axis: 'x',
});
})
});

function getDragArea($el){
var $tr = $el.closest('tr');
var $tds = $el.closest('tr').children(".no-planning");
var width = $el.closest('tr').width()-$tds.width()*$tds.length;
var height = $el.closest('tr').height();
var position = $el.closest('tr').position();
var x1 = position.left, y1 = position.top;
var x2 = x1+width, y2 = y1+height;
return [x1,y1,x2-$el.width()-10,y2]
}
});
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}

table {
border-collapse: no-collapse;
overflow: hidden;
table-layout: fixed;
width: 500px;
margin: 20px;
}

tr {
position: relative;
background: #FFF;
}

td, th {
position: relative;
padding: 0;
width: 100px;
height: 30px;
border: 1px solid #000;
}

.planning-spot {
position: absolute;
top: 0;
left: 0;
height: 20px;
width: 90px;
border-radius: 10px;
margin: 5px;
text-align: center;
z-index: 1;
cursor: pointer;
color: #FFF;
width: 290px;
background: #3CF;
}

.no-planning {
background: #CCC;
}

.wrapper{
overflow:auto;
width:500px;
height:120px;
border: solid 1px red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<div class="wrapper">
<div class="middle">
<table>
<tbody>
<tr id="test">
<td>
<div></div>
<div class="planning-spot">18 / 20</div>
</td>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
<td>
<div></div>
</td>
<td class="no-planning">
<div></div>
</td>
<td class="no-planning">
<div></div>
</td>
</tr>
</tbody>
</table>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
</div>

关于javascript - 从 JQuery-UI 包含中排除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51386993/

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