Chinaunix首页 | 论坛 | 博客
  • 博客访问: 459597
  • 博文数量: 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-02-12 14:05:14

  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. ul, li { margin: 0; padding: 0; list-style: none; }
  9. .clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; }
  10. .bor2 { padding: 20px 0 0 0; }
  11. .box { position: static; float: left; width: 200px; height: 800px; margin: 0 auto; margin-top: 15px; }
  12. .bor { position: static; width: 100px; height: 100px; margin: 0 0 5px 0 ; border: 1px solid #ccc; background: #ececec; }
  13. .borp { position: absolute; width: 100px; height: 80px; margin: 10px; padding: 10px; border: 1px solid #ccc; background: #ececec; }
  14. .bg { float:left; width: 100px; height: 100px; margin: 8px 5px 0 auto; padding: 10px; border: 1px solid #ccc; }
  15. .text2 { width: 200px; }
  16. .bgColor { border: 1px dashed #f00; }
  17. </style>
  18. <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
  19. <script type="text/javascript">
  20. $(document).ready(function() {
  21. var move = false; // 移动的初始化
  22. var $bg = $(".bg");
  23. var initDiv, tarDiv, tarDivHalf = 0, wHalf = 0; //拖拽对象 目标对象
  24. var initPos = {x: 0, y: 0}, relPos = {x: 0, y: 0}, temPos = {x: 0, y: 0};
  25. var dragPos = {x1: 0, x2: 0, y1: 0, y2: 0};// 拖拽对象的四个坐标
  26. var tarDivPos = {x1: 0, y1: 0, x2: 0, y2: 0}; //目标对象的四个坐标
  27. $(".bor").each(function() {
  28. $(this).mousedown(function(event) {     
  29. borSub = $(this).index();
  30. initDiv = $(".bor").eq(borSub); //拖拽对象
  31. //initDiv.clone().addClass("clone").appendTo($("body"));
  32. // 鼠标 与 目标元素的相对坐标
  33. relPos.x = event.pageX - $(this).offset().left;
  34. relPos.y = event.pageY - $(this).offset().top;
  35. move = true;
  36. });
  37. $(document).mousemove(function(event) {
  38. if (!move) { return false; }
  39. // 下列代码是 if(move)的 程序
  40. initDiv.removeClass("bor").addClass("borp");
  41. // 目标元素随鼠标移动的坐标
  42. dragPos.x1 = event.pageX - relPos.x;
  43. dragPos.y1 = event.pageY - relPos.y;
  44. dragPos.x2 = dragPos.x1 + initDiv. innerWidth();
  45. dragPos.y2 = dragPos.y1 + initDiv. innerHeight();
  46. initDiv.css({ left: dragPos.x1 +'px', top: dragPos.y1 + 'px' });
  47. $bg.each(function() {
  48. tarDiv = $(this);
  49. // 目标对象的坐标
  50. tarDivPos.x1 = tarDiv.offset().left;
  51. tarDivPos.x2 = tarDivPos.x1 + tarDiv.width();
  52. tarDivPos.y1 = tarDiv.offset().top;
  53. tarDivPos.y2 = tarDivPos.y1 + tarDiv.height();
  54. tarDivHalf = tarDiv.height()/2; //临时变量,以便于在if判断中使用
  55. wHalf = tarDiv.width()/2;
  56. if (dragPos.x2 >= tarDivPos.x1 + wHalf && dragPos.x2 <= tarDivPos.x2 + wHalf && dragPos.y2 >= tarDivPos.y1 + tarDivHalf && dragPos.y2 <= tarDivPos.y2 + tarDivHalf ) {
  57. if(tarDiv.children().length >0 ) {return false;}// 解决重叠元素 移动到一个目标元素
  58. tarDiv.removeClass("bg").addClass("bg bgColor");
  59. } else {
  60. tarDiv.removeClass("bgColor");
  61. }
  62. });
  63. }).mouseup (function(event,source) {    
  64.     initDiv.appendTo($(".bgColor"));// 利用 apppendTo 方法 使拖动元素对象添加到指定的区域。
  65.     
  66.      if(initDiv.html()=="111")
  67.      {
  68.      $(".bgColor").html("

    111111

    "
    );
  69.           }
  70.      if(initDiv.html()=="222")
  71.      {
  72.      $(".bgColor").html("

    222222

    "
    );
  73.           }
  74.      if(initDiv.html()=="333")
  75.      {
  76.      $(".bgColor").html("

    333333

    "
    );
  77.           }
  78.      if(initDiv.html()=="444")
  79.      {
  80.      $(".bgColor").html("

    444444

    "
    );
  81.           }
  82.      if(initDiv.html()=="555")
  83.      {
  84.      $(".bgColor").html("

    555555

    "
    );
  85.           }
  86.      if(initDiv.html()=="666")
  87.      {
  88.      $(".bgColor").html("

    666666

    "
    );
  89.           }
  90.      if(initDiv.html()=="777")
  91.      {
  92.      $(".bgColor").html("

    777777

    "
    );
  93.           }
  94.      initDiv.removeClass("borp").addClass("bor").removeAttr("style"); //恢复拖拽对象初始的样式
  95.     
  96.           move = false;
  97. });
  98. });
  99. });

  100.  
  101. </script>
  102. </head>
  103. <body>
  104. <div class="box">
  105. <div class="bor">111</div>
  106. <div class="bor">222</div>
  107. <div class="bor">333</div>
  108. <div class="bor">444</div>
  109. <div class="bor">555</div>
  110. <div class="bor">666</div>
  111. <div class="bor">777</div>
  112. </div>
  113. <div class="box2">
  114. <div class="bg" id="bg1" onclick="delete();"></div>
  115. <div class="bg" id="bg2" onclick="delete();"></div>
  116. <div class="bg" id="bg3" onclick="delete();"></div>
  117. <div class="bg" id="bg4" onclick="delete();"></div>
  118. </div>
  119. <div class="clear"></div>
  120. </body>
  121. </html>

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