gpt4 book ai didi

javascript - 使用 jQuery 小部件将 div 标签拖放到网格行上

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

我尝试拖动一个 div在网格系统行上,要找出他位于后面的女巫行,请放下它:

https://jsfiddle.net/johnidevo/08f83op9/5/

________________________________________________
droppable_area_(grids)__________________________
1row____________________________________________
2row____________________________________________
3row___-----------------------------------------
4row___| draggable <div> |
5row___| |
6row___-----------------------------------------
7row____________________________________________
(<div>'s)_______________________________________

我的代码如下:

jQuery( object ).draggable({                        
snap: ".selectable",
containment: "#droppable_area",
axis: "y",
drag: function( event, ui ){
console.log( jQuery( this ).parent() );
}
});

jQuery('.selectable').droppable({
drop: function( event, ui ) {
console.log( jQuery( this ) );
}
});

console.log( jQuery( this ) );什么时候draggable <div>只有一个行高大小它工作正常,如果大于 1 行大小它失败为什么?

最佳答案

发生的事情不是 IMO 的错误。我更改了“可拖动”元素的大小以匹配“可放置”元素的高度,然后选择了正确的 event.target.id。我认为问题是当“可拖动”高度大于“droppabe”高度时。 draggable 覆盖了多个 droppable 元素,此时,有多个 event.target.id 被覆盖。如果我是你,我会重新考虑我是如何完成这项任务的。另外,也许您可​​以尝试使用已经解决此问题的 jquery 插件。

为了展示这一点,我向可拖动元素添加了一个“.moveable”类,并为其指定了 20px 的高度,并为“.selectable”元素指定了相同的高度。正如预期的那样,显示了正确的 ID。

我有一个不可靠的解决方案可以帮助您解决问题 - 使用可放置公差:“指针”选项,演示了 40 像素的可拖动高度和 20 像素的可放置高度。但这并不可靠,因为您总是需要在元素的最底部或最顶部捕获可拖动对象——但这可能会帮助您找到一种方法或重新考虑您的方法。祝你好运!

$(document).ready(function() {
$(".moveable").draggable({
snap: ".selectable",
containment: "#day_kalendar",
axis: "y",
drag: function( event, ui ){
//console.log( event.target.id );
}
});

$(".selectable").droppable({
drop: function( event, ui ) {

$("#response").text(event.target.id);
console.log(this.id);

//var hfrom=$( this ).attr("data-hour");
//var hto=$( this ).attr("data-nexthour");
//$("#1463419359713").children("#eventHours").text( hfrom+" - "+hto );
},
over: function( event, ui) {
//console.log(event, ui);
},
tolerance: "pointer"
});
});
.ui-selectable .ui-selected {
background-color: #a6c9e2;
}

.ui-selectable .ui-selecting {
background: #FECA40;
}

.ui-selectable-helper {
position: absolute;
z-index: 100;
border: none;
}

.ui-dialog-titlebar {
background: transparent;
border: none;
}

[class*="col-"] {
float: left;
padding: 0px;
}

.col-1 {
width: 8.33%;
}

.col-2 {
width: 16.66%;
}

.col-3 {
width: 25%;
}

.col-4 {
width: 33.33%;
}

.col-5 {
width: 41.66%;
}

.col-6 {
width: 50%;
}

.col-7 {
width: 58.33%;
}

.col-8 {
width: 66.66%;
}

.col-9 {
width: 75%;
}

.col-10 {
width: 83.33%;
}

.col-11 {
width: 91.66%;
}

.col-12 {
width: 100%;
}

.row::after {
content: "";
clear: both;
display: block;
}

.text-right {
text-align: right;
}

.cornsilk {
background-color: #E0E0E0;
}

.forestgreen {
background-color: forestgreen;
}

.limegreen {
background-color: limegreen;
}

.dimgrey {
background-color: dimgrey;
color: white;
}

.border-up {
border-top: 1px solid white;
}

.border-up-dashed {
border-top: 1px dashed white;
}

.fix-border {
border-top: 1px solid dimgrey;
}
.newevent {
position: absolute;
top: 0;
left: 0;
margin: 0px;
width: 100%;
height: auto;
z-index: 1;
/*opacity: 0.7;*/
border-radius: 4px;
}

#console {
margin-top: 10px;
font-size: 9px;
background-color: white;
padding: 10px;
}

.selectable {
position: relative;
margin:0;
padding:0;
height: 20px;
}
.moveable {
padding:0;
margin:0;
height: 40px;
}
#response {
height: 20px;
}
<script src="https://code.jquery.com/jquery.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<div id ="mainContainer">
<div class="container">
<div id="day_kalendar" class="ui-selectable">
<div class="row border-up">
<div class="col-1 dimgrey text-right"><span>10AM</span></div>
<div id="a1" data-hour="10 am" data-nexthour="10:30 am" class="col-11 cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row">
<div class="col-1 dimgrey fix-border">&nbsp;</div>
<div id="a2" data-hour="10:30 am" data-nexthour="11 am" class="col-11 border-up-dashed cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row border-up">
<div class="col-1 dimgrey text-right"><span>11AM</span></div>
<div id="a3" data-hour="11 am" data-nexthour="11:30 am" class="col-11 cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row">
<div class="col-1 dimgrey fix-border">&nbsp;</div>
<div id="a4" data-hour="11:30 am" data-nexthour="12 pm" class="col-11 border-up-dashed cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row border-up">
<div class="col-1 dimgrey text-right"><span>12PM</span></div>
<div id="a5" data-hour="12 pm" data-nexthour="12:30 pm" class="col-11 cornsilk selectable ui-selectee">
<span>&nbsp;</span>
<div id="a1463419359713" class="newevent moveable ui-draggable forestgreen" style="">
<span id="eventHours">12 pm - 2 pm</span>
<span id="eventTitle">(No title)</span>
<div class="ui-resizable-handle ui-resizable-s"></div>
</div>

</div>
</div>
<div class="row">
<div class="col-1 dimgrey fix-border">&nbsp;</div>
<div id="a6" data-hour="12:30 pm" data-nexthour="1 pm" class="col-11 border-up-dashed cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row border-up">
<div class="col-1 dimgrey text-right"><span>1PM</span></div>
<div id="a7" data-hour="1 pm" data-nexthour="1:30 pm" class="col-11 cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row">
<div class="col-1 dimgrey fix-border">&nbsp;</div>
<div id="a8" data-hour="1:30 pm" data-nexthour="2 pm" class="col-11 border-up-dashed cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row border-up">
<div class="col-1 dimgrey text-right"><span>2PM</span></div>
<div id="a9" data-hour="2 pm" data-nexthour="2:30 pm" class="col-11 cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row">
<div class="col-1 dimgrey fix-border">&nbsp;</div>
<div id="a10" data-hour="2:30 pm" data-nexthour="3 pm" class="col-11 border-up-dashed cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row border-up">
<div class="col-1 dimgrey text-right"><span>3PM</span></div>
<div id="a11" data-hour="3 pm" data-nexthour="3:30 pm" class="col-11 cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row">
<div class="col-1 dimgrey fix-border">&nbsp;</div>
<div id="a12" data-hour="3:30 pm" data-nexthour="4 pm" class="col-11 border-up-dashed cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
<div class="row border-up">
<div class="col-1 dimgrey text-right"><span>4PM</span></div>
<div id="a13" data-hour="4 pm" data-nexthour="4:30 pm" class="col-11 cornsilk selectable ui-selectee">
<span>&nbsp;</span>
</div>
</div>
</div>
</div>
</div>
<div id="response">

</div>

关于javascript - 使用 jQuery 小部件将 div 标签拖放到网格行上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262268/

24 4 0