Reputation: 33
I want to make a <div>
draggable only when mouse is over first element inside this <div>
. When mouse isn't over that first element, whole <div>
can't be draggable. This is my code:
<div id="containers">
<div id="cont1">foo</div>
<div id="cont2">bar</div>
</div>
<script>
$(document).ready(function() {
$("#cont1").mouseenter(function() {
$("#containers").draggable("enable");
});
$("#cont1").mouseleave(function() {
$("#containers").draggable("disable");
});
});
</script>
When mouse enter or leave #cont1
, there is an error in console:
Uncaught TypeError: undefined is not a function(index):27 (anonymous function)jquery-1.11.0.js:4995 jQuery.event.special.(anonymous function).handlejquery-1.11.0.js:4624 jQuery.event.dispatchjquery-1.11.0.js:4292 elemData.handle
Upvotes: 3
Views: 1000
Reputation: 729
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<div id="containers" style="height: 200px;width: 200px;background-color: blanchedalmond">
<div id="cont1">foo</div>
<div id="cont2">bar</div>
</div>
<script>
$(document).ready(function() {
$("#cont1").mouseenter(function() {
$("#containers").draggable({ disabled: false });
});
$("#cont1").mouseleave(function() {
$("#containers").draggable({ disabled: true });
});
});
</script>
Use this code.
Upvotes: 0
Reputation: 43166
You're looking for the handle
option of draggable widget:
$("#containers").draggable({
handle: "#cont1"
});
#containers {
height: 100px;
background: grey;
}
#cont1 {
width: 50px;
height: 50px;
background: dodgerblue;
}
#cont2 {
width: 50px;
height: 50px;
background: #eee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<div id="containers">
<div id="cont1">foo</div>
<div id="cont2">bar</div>
</div>
Upvotes: 2