jQuery div拖拽用法实例

这里需要引用好jquery 和 jqueryui两个包:

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta content="EditPlus®"> <meta content=""> <meta content=""> <meta content=""> <title>Document</title> <script src="https://www.jb51.net/jquery1.8.3.js"></script> <script src="https://www.jb51.net/jquery-ui.js"></script> <style> .yuanjian{ float:left; width:180px; height:22px; padding-left:5px; margin-left:5px; margin-top:5px; cursor:pointer; border: 1px solid orange; } .fish{ float:left; width:180px; height:22px; padding-left:5px; margin-left:15px; margin-top:15px; cursor:pointer; border: 1px solid red; } </style> <script> $(function (){ $('#add_div').droppable({ accept:" .yuanjian ", hoverClass: "droppable-hover", drop: function(event, ui){ if(ele!=''){ if(ele.id.substr(0,13)=="div_yuanjian_"){ var tmpId = "fish_"+ele.id.substr(13,ele.id.length-13); var new_div = "<div class=\"fish\" id=\""+tmpId+"\">"+$('#'+ele.id).html() +" </div>"; $(this).before(new_div); //可以在这里绑定tmpId事件 } } } }); }); var ele = ''; var add_div_num = 0; function add_yuanjian(){ add_div_num++; var div_id = "div_yuanjian_"+add_div_num; var add_div = "<div class=\"yuanjian\" id=\""+div_id+"\">原件"+add_div_num+"</div>"; $('#add_yuanjian_div').before(add_div); $('#'+div_id).mouseover(function(){ $(this).css({background:"#E0E0E0"}); }).mouseout(function(){ $(this).css({background:"#FFFFFF"}); }).draggable({ helper:'clone', opacity:0.5, start:function(event,ui){ ele = event.srcElement || event.target; }}); } </script> </head> <body> <div> <div>展示列表 </div> <div> </div> </div> <div> <div>原件列表 <button>增加原件</button> </div> <div> </div> </div> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwdxf.html