简单的jQuery拖拽排序效果的实现(增强动态)


Posted in Javascript onFebruary 09, 2017

增强动态增加Div效果

简单的jQuery拖拽排序效果的实现(增强动态)

原来没有新建动作,分析代码后发现很容易增强~~

<!DOCTYPE HTML> 
 <html> 
 <head> 
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 <title>测试的拖拽功能</title> 
 <style type="text/css"> 
 body, div { margin: 0; paading: 0; font-size: 12px; } 
 body { width:100%; margin: 0 auto; } 
 ul, li { margin: 0; padding: 0; list-style: none; } 
 .clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; } 
 .drag_module_box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } 
 .drag_module_box1 { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } 
 .drag_module_main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; } 
 .drag_module_maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; } 
 .drag_module_hide { width: 600px; height: 80px; margin-bottom: 5px; } 
 .drag_module_dash { position: sta;tic; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; }; 
 </style> 
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 <script type="text/javascript"> 
 $(document).ready( function () { 
  //来源:http://www.cnblogs.com/web-ed2/archive/2011/09/19/2181819.html 
   var range = { x: 0, y: 0 };//鼠标元素偏移量 
   var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽对象的四个坐标 
   var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目标元素对象的坐标初始化 
   var theDiv = null, move = false;//拖拽对象 拖拽状态 
   var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //拖拽对象的索引、高度、的初始化。 
   var tarDiv = null, tarFirst, tempDiv; //要插入的目标元素的对象, 临时的虚线对象 
  function loopbox(){ //循环初始化 
     $(".drag_module_box").find(".drag_module_main").each(function(){ 
      console.log( 'find' ); 
       $(this).mousedown(function (event){ 
         //拖拽对象 
         theDiv = $(this); 
         //鼠标元素相对偏移量 
         range.x = event.pageX - theDiv.offset().left; 
         range.y = event.pageY - theDiv.offset().top; 
         theDivId = theDiv.index(); 
         theDivHeight = theDiv.height(); 
         theDivHalf = theDivHeight/2; 
         move = true; 
         theDiv.attr("class","drag_module_maindash"); 
         // 创建新元素 插入拖拽元素之前的位置(虚线框) 
         $("<div class='drag_module_dash'></div>").insertBefore(theDiv); 
       }); 
     }); 
  } 
  loopbox(); 
   $(".drag_module_box").mousemove(function(event) { 
    console.log( 'mousemove' ); 
     if (!move) return false; 
     lastPos.x = event.pageX - range.x; 
     lastPos.y = event.pageY - range.y; 
     lastPos.y1 = lastPos.y + theDivHeight; 
     // 拖拽元素随鼠标移动 
     theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'}); 
     // 拖拽元素随鼠标移动 查找插入目标元素 
     var $main = $('.drag_module_main'); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标, 
     tempDiv = $(".drag_module_dash"); //获得临时 虚线框的对象 
     $main.each(function () { 
       tarDiv = $(this); 
       tarPos.x = tarDiv.offset().left; 
       tarPos.y = tarDiv.offset().top; 
       tarPos.y1 = tarPos.y + tarDiv.height()/2; 
       tarFirst = $main.eq(0); // 获得第一个元素 
       tarFirstY = tarFirst.offset().top + theDivHalf ; // 第一个元素对象的中心纵坐标 
       //拖拽对象 移动到第一个位置 
       if (lastPos.y <= tarFirstY) { 
           tempDiv.insertBefore(tarFirst); 
       } 
       //判断要插入目标元素的 坐标后, 直接插入 
       if (lastPos.y >= tarPos.y - theDivHalf && lastPos.y1 >= tarPos.y1 ) { 
         tempDiv.insertAfter(tarDiv); 
       } 
     }); 
   }).mouseup(function(event) { 
    console.log( 'mouseup' ); 
    if(theDiv==null) return false; 
     theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虚线div的位置上 
     theDiv.attr("class", "drag_module_main"); //恢复对象的初始样式 
     $('.drag_module_dash').remove(); // 删除新建的虚线div 
     move=false; 
   }); 
   $("#drag_module_insert").click(function(){ 
    $("#drag_module_box1").html($("#drag_module_box1").html()+$("#drag_module_box2").html()); 
    loopbox(); 
   }); 
   $("#drag_module_seque").click(function(){ 
    $(".drag_module_box").find(".drag_module_main").each(function(){ 
      console.log($(this).attr('id')); 
    }); 
   }); 
 }); 
 </script> 
 </head> 
 <body>  
 <div class="drag_module_box" id="drag_module_box1"> 
   <div class="drag_module_main" id="main1">div1</div> 
   <div class="drag_module_main" id="main2">div2</div> 
   <div class="drag_module_main" id="main3">div3</div> 
   <div class="drag_module_main" id="main4">div4</div> 
   <div class="drag_module_main" id="main5">div5</div> 
   <div class="drag_module_main" id="main6">div6</div> 
 </div> 
 <div class="drag_module_box1" id="drag_module_box2"> 
  <div class="drag_module_main" id="main_first">div7</div> 
 </div> 
 <input type="button" value="新建" id="drag_module_insert"/> 
 <input type="button" value="确定" id="drag_module_seque"/> 
 </body> 
 </html>
Javascript 相关文章推荐
javascript自适应宽度的瀑布流实现思路
Feb 20 Javascript
jquery $.fn $.fx是什么意思有什么用
Nov 04 Javascript
javascript中不提供sleep功能如何实现这个功能
May 27 Javascript
jQuery中noConflict()用法实例分析
Feb 08 Javascript
IE8下jQuery改变png图片透明度时出现的黑边
Aug 30 Javascript
javascript每日必学之运算符
Feb 16 Javascript
JS实现类似51job上的地区选择效果示例
Nov 17 Javascript
Bootstrap面板使用方法
Jan 16 Javascript
jQuery表单验证之密码确认
May 22 jQuery
详解适配器在JavaScript中的体现
Sep 28 Javascript
javascript浅层克隆、深度克隆对比及实例解析
Feb 09 Javascript
uniapp微信小程序:key失效的解决方法
Jan 20 Javascript
bootstrapValidator.min.js表单验证插件
Feb 09 #Javascript
js 原型对象和原型链理解
Feb 09 #Javascript
AngularJs表单校验功能实例代码
Feb 09 #Javascript
javascript 显示全局变量与隐式全局变量的区别
Feb 09 #Javascript
JS获取本周周一,周末及获取任意时间的周一周末功能示例
Feb 09 #Javascript
简单谈谈Javascript函数中的arguments
Feb 09 #Javascript
javascript 中设置window.location.href跳转无效问题解决办法
Feb 09 #Javascript
You might like
yii2中使用Active Record模式的方法
2016/01/09 PHP
浅谈PHP安全防护之Web攻击
2017/01/03 PHP
PHPCrawl爬虫库实现抓取酷狗歌单的方法示例
2017/12/21 PHP
PHP PDOStatement::rowCount讲解
2019/02/01 PHP
xmlHTTP实例
2006/10/24 Javascript
JavaScript中关于indexOf的使用方法与问题小结
2010/08/05 Javascript
Javascript和Java获取各种form表单信息的简单实例
2014/02/14 Javascript
Jquery给基本控件的取值、赋值示例
2014/05/23 Javascript
AngularJS 2.0新特性有哪些
2016/02/18 Javascript
学习Javascript面向对象编程之封装
2016/02/23 Javascript
基于JQuery实现分隔条的功能
2016/06/17 Javascript
jQuery EasyUI ProgressBar进度条组件
2017/02/28 Javascript
js实现音频控制进度条功能
2017/04/01 Javascript
微信小程序实战之自定义抽屉菜单(7)
2017/04/18 Javascript
docker中编译nodejs并使用nginx启动
2017/06/23 NodeJs
浅谈使用mpvue开发小程序需要注意和了解的知识点
2018/05/23 Javascript
bootstrap tooltips在 angularJS中的使用方法
2019/04/10 Javascript
使用vue-cli3新建一个项目并写好基本配置(推荐)
2019/04/24 Javascript
nuxt踩坑之Vuex状态树的模块方式使用详解
2019/09/06 Javascript
基于javascript canvas实现五子棋游戏
2020/07/08 Javascript
[01:14:05]《加油DOTA》第四期
2014/08/25 DOTA
[原创]教女朋友学Python3(二)简单的输入输出及内置函数查看
2017/11/30 Python
python通过tcp发送xml报文的方法
2018/12/28 Python
详解Python二维数组与三维数组切片的方法
2019/07/18 Python
django多文件上传,form提交,多对多外键保存的实例
2019/08/06 Python
python打包多类型文件的操作方法
2020/09/21 Python
世界最大的票务市场:viagogo
2017/02/16 全球购物
薇诺娜官方网上商城:专注敏感肌肤
2017/05/25 全球购物
加拿大时装零售商:Influence U
2018/12/22 全球购物
机械专业应届生求职信
2013/09/21 职场文书
大学生活自我评价
2014/04/09 职场文书
面试自我介绍演讲稿
2014/04/29 职场文书
医院院务公开实施方案
2014/05/03 职场文书
幼儿园课题方案
2014/06/09 职场文书
python3使用diagrams绘制架构图的步骤
2021/04/08 Python
Python数据分析入门之数据读取与存储
2021/05/13 Python