Chinaunix首页 | 论坛 | 博客
  • 博客访问: 462329
  • 博文数量: 226
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2111
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-20 09:02
个人简介

web web web

文章分类

全部博文(226)

文章存档

2020年(2)

2019年(1)

2018年(3)

2017年(26)

2016年(57)

2015年(60)

2014年(77)

我的朋友

分类: Web开发

2015-04-15 09:26:28


  1. <!DOCTYPE HTML>

  2. <html>

  3. <head>

  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  5. <title>测试的拖拽功能</title>

  6. <style type="text/css">

  7. body, div { margin: 0; paading: 0; font-size: 12px;}

  8. .clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; }

  9. .bor2 { padding: 20px 0 0 0; }

  10. .box { height: 200px; margin: 0 auto; margin-top: 15px; }



  11. .box2 { margin: 0 auto; margin-top: 15px; }

  12. .bor { position: static; width: 100px; height: 100px; margin: 0 0 5px 30px ; float:left; color:#fff; font-weight:bold; padding:5px 0 0 5px;}

  13. .bor:hover{border-color: #9a9fa4; box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.85);}

  14. .mousehover{ border-color: #9a9fa4; box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.85); cursor:move;}

  15. .borp { position: absolute; width: 100px; height: 80px; margin: 10px; padding: 10px; border: 1px solid #ccc; background: #ececec; }

  16. /*
  17. .bg { width: 100px; height: 100px; margin: 8px 5px 0 auto; padding: 10px; border: 1px solid #ccc; } */

  18. .bg { width: 160px; height: 130px; margin: 8px 5px 0 auto; padding: 10px; border: 1px dashed #bfbfbf; float:left; border-radius:20px;}

  19. .bg:hover{border-color: #9a9fa4; box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.85);}

  20. .text2 { width: 200px; }


  21. .bgColor { border: 1px dashed #9a9fa4; }

  22. /*
  23. .bgColor { border: 1px dashed #bfbfbf; } */

  24. </style>

  25. <link rel="stylesheet" href="bootstrap.min.css" />

  26. <script type="text/javascript" src="jquery-1.9.1.min.js"></script>

  27. <script type="text/javascript">

  28. $(document).ready(function() {



  29. var $bg = $(".bg");

  30. var initDiv, tarDiv, tarDivHalf = 0, wHalf = 0; //拖拽对象 目标对象

  31. var initPos = {x: 0, y: 0}, relPos = {x: 0, y: 0}, temPos = {x: 0, y: 0};

  32. var dragPos = {x1: 0, x2: 0, y1: 0, y2: 0};// 拖拽对象的四个坐标

  33. var tarDivPos = {x1: 0, y1: 0, x2: 0, y2: 0}; //目标对象的四个坐标
  34.  
  35. $(".bor").each(function(){

  36.     var move = false; // 移动的初始化

  37.      $(this).mousedown(function(event) {

  38.          borSub = $(this).index();

  39.         initDiv = $(".bor").eq(borSub); //拖拽对象

  40.         //initDiv.clone().addClass("clone").appendTo($("body"));

  41.         // 鼠标 与 目标元素的相对坐标

  42.         relPos.x = event.pageX - $(this).offset().left;

  43.         relPos.y = event.pageY - $(this).offset().top;

  44.         move = true;

  45.     });

  46.     $(document).mousemove(function(event) {
  47.      
  48.         if (!move) { return false; }

  49.         // 下列代码是 if(move)的 程序

  50.         initDiv.removeClass("bor").addClass("borp");

  51.         // 目标元素随鼠标移动的坐标

  52.         dragPos.x1 = event.pageX - relPos.x;

  53.         dragPos.y1 = event.pageY - relPos.y;

  54.         dragPos.x2 = dragPos.x1 + initDiv. innerWidth();

  55.         dragPos.y2 = dragPos.y1 + initDiv. innerHeight();

  56.         initDiv.css({ left: dragPos.x1 +'px', top: dragPos.y1 + 'px' });

  57.         $bg.each(function() {

  58.             tarDiv = $(this);

  59.             // 目标对象的坐标

  60.             tarDivPos.x1 = tarDiv.offset().left;

  61.             tarDivPos.x2 = tarDivPos.x1 + tarDiv.width();

  62.             tarDivPos.y1 = tarDiv.offset().top;

  63.             tarDivPos.y2 = tarDivPos.y1 + tarDiv.height();

  64.             tarDivHalf = tarDiv.height()/2; //临时变量,以便于在if判断中使用

  65.             wHalf = tarDiv.width()/2;

  66.             if (dragPos.x2 >= tarDivPos.x1 + wHalf && dragPos.x2 <= tarDivPos.x2 + wHalf && dragPos.y2 >= tarDivPos.y1 + tarDivHalf && dragPos.y2 <= tarDivPos.y2 + tarDivHalf ) {

  67.                 if(tarDiv.children().length >0 ) {

  68.                     tarDiv.children().html(initDiv.html());
  69.                     return false;
  70.                 }// 解决重叠元素 移动到一个目标元素

  71.                 tarDiv.removeClass("bg").addClass("bg bgColor");

  72.             } else {

  73.                 tarDiv.removeClass("bgColor");

  74.             }

  75.         });

  76.     })

  77.     $(document).mouseup (function(event,source) {

  78.         if( $(".bgColor").html()=="")
  79.         {
  80.             initDiv.appendTo($(".bgColor"));//使拖动元素对象添加到指定的区域。
  81.        
  82.             if(initDiv.html()=="111")
  83.             {
  84.                 $(".bgColor").html("

    111

    "
    );
  85.             }

  86.             if(initDiv.html()=="222")
  87.             {
  88.                 $(".bgColor").html("

    222

    "
    );
  89.             }

  90.             if(initDiv.html()=="333")
  91.             {
  92.                  $(".bgColor").html("

    333

    "
    );
  93.             }

  94.             if(initDiv.html()=="444")
  95.             {
  96.                 $(".bgColor").html("

    444

    "
    );
  97.             }

  98.             if(initDiv.html()=="555")
  99.             {
  100.                 $(".bgColor").html("

    555

    "
    );
  101.             }

  102.             if(initDiv.html()=="666")
  103.             {
  104.                $(".bgColor").html("

    666

    "
    );
  105.             }
     
  106.         } 

  107.          // initDiv.removeClass("borp").addClass("bor").removeAttr("style"); //恢复拖拽对象初始的样式
  108.          initDiv.removeClass("borp").addClass("bor"); //恢复拖拽对象初始的样式
  109.          move = false;

  110.     });


  111. });
  112.  

  113. });

  114. function reLoad(){
  115.     window.location.reload();
  116. }

  117. </script>

  118. </head>

  119. <body>

  120. <div class="box" style="margin-left:400px;">

  121. <div class="bor" id="bor1" style="background:url(device.png);">111</div>

  122. <div class="bor" id="bor2" style="background:url(tuopo.png);">222</div>
  123.  
  124. <div class="bor" id="bor3" style="background:url(guzhang.png);">333</div>

  125. <div class="bor" id="bor4" style="background:url(inventory.png);">444</div>

  126. <div class="bor" id="bor5" style="background:url(zhuzhi.png);">555</div>

  127. <div class="bor" id="bor6" style="background:url(system.png);">666</div>
  128.  
  129. </div>

  130. <div class="clear"></div>
  131.  
  132. <div class="box2" style="margin-top:100px;">
  133.     <table align="center">
  134.         <tr>
  135.             <td> <div class="bg" id="bg1" onclick="delete();"></div> </td>
  136.             <td> <div class="bg" id="bg2" onclick="delete();"></div> </td>
  137.         </tr>
  138.         <tr>
  139.             <td> <div class="bg" id="bg3" onclick="delete();"></div> </td>
  140.             <td> <div class="bg" id="bg4" onclick="delete();"></div> </td>
  141.         </tr>
  142.     </table>
  143. </div>


  144. <div style="float:right; margin-top:50px; margin-right:500px;">
  145.   
  146.     <button type="button" class="btn btn-info" onclick="reLoad();">重选</button>
  147.     <button type="button" class="btn btn-primary" onclick="confirm();" style="margin-left:3px;">确定</button>
  148. </div>
  149. </body>

阅读(525) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~