jquery拖拽排序简单实现方法(效果增强版)


Posted in Javascript onFebruary 16, 2016

本文实例讲述了jquery拖拽排序简单实现方法。分享给大家供大家参考,具体如下:

运行效果截图如下:

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="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready( function () {
   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>

希望本文所述对大家jQuery程序设计有所帮助。

Javascript 相关文章推荐
List Information About the Binary Files Used by an Application
Jun 11 Javascript
JQuery.closest(),parent(),parents()寻找父结点
Feb 17 Javascript
JavaScript获取onclick、onchange等事件值的代码
Jul 22 Javascript
解析JavaScript中instanceof对于不同的构造器或许都返回true
Dec 03 Javascript
百度地图API之本地搜索与范围搜索
Jul 30 Javascript
js学习之----深入理解闭包
Nov 21 Javascript
JS批量替换内容中关键词为超链接
Feb 20 Javascript
收藏AngularJS中最重要的核心功能
Jul 09 Javascript
微信小程序使用scroll-view标签实现自动滑动到底部功能的实例代码
Nov 09 Javascript
JavaScript高阶教程之“==”隐藏下的类型转换
Apr 11 Javascript
layui扩展上传组件模拟进度条的方法
Sep 23 Javascript
JavaScript 空间坐标的使用
Aug 19 Javascript
jquery实现具有收缩功能的垂直导航菜单
Feb 16 #Javascript
学习使用AngularJS文件上传控件
Feb 16 #Javascript
JS中call/apply、arguments、undefined/null方法详解
Feb 15 #Javascript
谷歌showModalDialog()方法不兼容出现对话窗口的解决办法
Feb 15 #Javascript
仅30行代码实现Javascript中的MVC
Feb 15 #Javascript
理解javascript中的with关键字
Feb 15 #Javascript
使用基于Node.js的构建工具Grunt来发布ASP.NET MVC项目
Feb 15 #Javascript
You might like
PHP开发大型项目的一点经验
2006/10/09 PHP
php完全过滤HTML,JS,CSS等标签
2009/01/16 PHP
php中$_REQUEST、$_POST、$_GET的区别和联系小结
2011/11/23 PHP
php去除二维数组的重复项方法
2015/11/03 PHP
php json_encode与json_decode详解及实例
2016/12/13 PHP
JS模拟的QQ面板上的多级可展开的菜单
2009/10/10 Javascript
JQuery拖拽元素改变大小尺寸实现代码
2012/12/10 Javascript
基于JQuery制作可编辑的表格特效
2014/12/23 Javascript
JS遍历数组及打印数组实例分析
2016/01/21 Javascript
AngularJS中处理多个promise的方式
2016/02/02 Javascript
js定义类的几种方法(推荐)
2016/06/08 Javascript
JavaScript对象封装的简单实现方法(3种方法)
2017/01/03 Javascript
基于jQuery实现定位导航位置效果
2017/11/15 jQuery
微信小程序实现点击按钮修改字体颜色功能【附demo源码下载】
2017/12/05 Javascript
vue中路由验证和相应拦截的使用详解
2017/12/13 Javascript
JS实现select选中option触发事件操作示例
2018/07/13 Javascript
vue移动端使用appClound拉起支付宝支付的实现方法
2019/11/21 Javascript
解决echarts图表使用v-show控制图表显示不全的问题
2020/07/19 Javascript
jquery实现点击左右按钮切换图片
2021/01/27 jQuery
vue-router路由懒加载及实现的3种方式
2021/02/28 Vue.js
使用Python读写文本文件及编写简单的文本编辑器
2016/03/11 Python
Python实现PS滤镜Fish lens图像扭曲效果示例
2018/01/29 Python
python正则表达式之对号入座篇
2018/07/24 Python
Django MEDIA的配置及用法详解
2019/07/25 Python
python matplotlib折线图样式实现过程
2019/11/04 Python
pycharm2020.1.2永久破解激活教程,实测有效
2020/10/29 Python
Coggles美国/加拿大:高级国际时装零售商
2018/10/23 全球购物
MAC Cosmetics官方网站:魅可专业艺术彩妆
2019/04/10 全球购物
俄罗斯皮肤健康中心:Pharmacosmetica.ru
2020/02/22 全球购物
优秀教师感人事迹材料
2014/05/04 职场文书
2014年全国爱牙日宣传活动方案
2014/09/21 职场文书
个人作风建设自查报告
2014/10/22 职场文书
草房子读书笔记
2015/06/29 职场文书
党员转正大会主持词
2015/07/02 职场文书
变长双向rnn的正确使用姿势教学
2021/05/31 Python
Spring Boot两种全局配置和两种注解的操作方法
2021/06/29 Java/Android