gpt4 book ai didi

javascript - Jquery冲突: draggable and target collision event

转载 作者:行者123 更新时间:2023-12-02 18:46:35 26 4
gpt4 key购买 nike

我正在使用 jQuery 与可拖动的碰撞。我有代码可以在放置时更改目标(较大的框)的颜色。但是,我希望它在 dragMe 接触到它时立即改变颜色。

预先感谢您的帮助!

代码如下: https://jsfiddle.net/rebeccaoutofourmindsstudios/mgL968x4/2/

$(".dragMe").draggable({
obstacle: ".obstacle",
preventCollision: true,
containment: "#moveInHere"
});

$(".obstacle").draggable({
obstacle: ".dragMe",
preventCollision: true,
containment: "#moveInHere"
});

$(document).ready(function() {
$("#dragMe").draggable({
containment: ".moveInHere",
obstacle: ".obstacle"
});
$("#obstacle").draggable({
containment: ".moveInHere"
});
$("#Target1").droppable({
tolerance: "touch",
preventCollision: true,
drop: dropItem
});
});

function dropItem(ev, ui) {
$("#Target1").css({
background: "blue"
});

}
#moveInHere {
with: 500px;
height: 500px;
border: 1px solid black;
background: #eee;
}

.dragMe {
width: 50px;
height: 50px;
border: 1px solid black;
background: #eee;
}

.obstacle {
width: 50px;
height: 50px;
border: 1px solid black;
background: #eee;
}

.target {
postition: absolute;
top: 100px;
left: 200px;
width: 100px;
height: 100px;
border: 1px solid black;
background: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">


<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">

<script type="text/javascript" src="http://eruciform.com/static//jquidragcollide/jquery-collision.js"></script>
<script type="text/javascript" src="http://eruciform.com/static//jquidragcollide/jquery-ui-draggable-collision.js"></script>
</head>


<body>
<div class="moveInHere" id="moveInHere">
<div class="dragMe" id="dragMe">Drag me...</div>
<div class="obstacle" id="obstacle">...but not in here.</div>
<div class="target" id="Target1">I want to change color</div>
</div>

最佳答案

您可以使用over事件回调如下:

over: function(event, ui) {
$(this).css('background', 'dodgerblue');
}

$(document).ready(function() {

$(".dragMe").draggable({
obstacle: ".obstacle",
preventCollision: true,
containment: "#moveInHere"
});

$(".obstacle").draggable({
obstacle: ".dragMe",
preventCollision: true,
containment: "#moveInHere"
});

$("#Target1").droppable({
tolerance: "touch",
preventCollision: true,
over: function(event, ui) {
$(this).css('background', 'dodgerblue');
},
out: function(event, ui) {
$(this).css('background', '#eee');
},
drop: dropItem
});
});

function dropItem(ev, ui) {
$(this).css({
background: "blue"
});
}
#moveInHere {
width: 500px;
height: 500px;
border: 1px solid black;
background: #eee;
}

.dragMe {
width: 50px;
height: 50px;
border: 1px solid black;
background: #eee;
}

.obstacle {
width: 50px;
height: 50px;
border: 1px solid black;
background: #eee;
}

.target {
position: absolute;
top: 100px;
left: 200px;
width: 100px;
height: 100px;
border: 1px solid black;
background: #eee;
}
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://eruciform.com/static//jquidragcollide/jquery-collision.js"></script>
<script type="text/javascript" src="http://eruciform.com/static//jquidragcollide/jquery-ui-draggable-collision.js"></script>

<div class="moveInHere" id="moveInHere">
<div class="dragMe" id="dragMe">Drag me...</div>
<div class="obstacle" id="obstacle">...but not in here.</div>
<div class="target" id="Target1">I want to change color</div>
</div>

关于javascript - Jquery冲突: draggable and target collision event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67357496/

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