Showing posts with label drag. Show all posts
Showing posts with label drag. Show all posts

Friday 3 September 2010

jQuery Drag and Drop

The example below is based largely on a demonstration at : http://jqueryui.com/demos/droppable/
I've just tidied it up, and added a couple of things which make it possible for you to copy and paste. It's so simple, even I understood it.

See demo.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Drag and drop</title>
<style type="text/css">
body
{
font-family:Sans-serif;
}
#draggable, #droppable
{
border:1px solid #CCCCCC;
padding:0.5em;
float:left;
}
#draggable
{
width:100px;
height:100px;
margin:10px 10px 10px 0;
}
#droppable
{
width:150px;
height:150px;
margin: 10px;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script>
 google.load("jquery", "1");
 google.load("jqueryui", "1");
</script>
<script type="text/javascript">
$(function()
{
$("#draggable").draggable();
$("#droppable").droppable(
{
drop: function(event, ui)
{
$(this).addClass('ui-state-highlight').find('p').html('Dropped!');
},
over: function(event, ui)
{
$(this).addClass('ui-state-highlight').find('p').html('Over!');
},
out: function(event, ui)
{
$(this).addClass('ui-state-highlight').find('p').html('Out!');
}
});
});
</script>
</head>
<body>
<div class="demo">
<div id="draggable" class="ui-widget-content">
<p>Drag me to my target</p>
</div>
<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>
</div>
</body>
</html>