ドラッグアンドドロップを可能にするプログラムの作成を行います。
はじめに、ドラッグアンドドロップさせる要素をHTMLで作成します。
ここでは、32pxの赤い正方形を使います。
<div id="block" style="background-color: red; width: 32px; height: 32px;"></div>
$("#block").mousedown(function(){
$(window).mousemove(function(){
// 移動処理
});
});
次に、移動処理の実際の内容について説明していきます。
$("#block").mousedown(function(){
$(window.document).mousemove(function(e){
$("#block").css({
position: "absolute",
top: e.pageY,
left: e.pageX
});
});
});
$("#block2").mousedown(function(){
$(window.document).mousemove(function(e){
$("#block2").css({
position: "absolute",
top: e.pageY,
left: e.pageX
});
});
});
$(window.document).mouseup(function(){
$(window.document).unbind("mousemove");
});