js实现简单的拖曳

在web中的一些需要随意拖曳和排序的问题,参考http://www.cnblogs.com/web-ed2/archive/2011/09/19/2181819.html,下面的代码做了些简单的修改:

<pre name="code" class="html"><!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: 960px; 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; }

.box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; }
.main td{ position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; }
.maindash { position:absolute; width: 600px; height: 80px; margin-bottom: 1px; border: 1px dashed blue; background: #ececec; opacity: 0.7; }

.hide { width: 600px; height: 80px; margin-bottom: 5px; }
.dash td{ position: sta;tic; width:600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; };
</style>
<script type="text/javascript" src="./js/jquery.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;  //要插入的目标元素的对象, 临时的虚线对象
    var preleft;
    var pretop;
    var videos=[];//存放视频的数组
    $(".main").each(function(i){
        //var data[i]={};
        videos[i]=$(this).find("td").attr("data-video");
    });

    $('.main').live('mousedown',function (event){
            //拖拽对象
            theDiv = $(this);
           //鼠标元素相对偏移量
            range.x = event.pageX - theDiv.offset().left ;
            range.y = event.pageY - theDiv.offset().top;
            preleft=theDiv.offset().left;
            preleft=preleft+10;
            pretop=theDiv.offset().top;
            pretop=pretop+10;
           // theDivId = theDiv.index();
            theDivHeight = theDiv.height();
            theDivHalf = theDivHeight/2;
            move = true;
            theDiv.attr("class","maindash");//改变类
            theDiv.css({left: preleft+'px',top:pretop+'px'});
            // 创建新元素 插入拖拽元素之前的位置(虚线框)
            $("<tr class='dash'><td></td></tr>").insertBefore(theDiv);//在选中元素之前插入一个新的div
        });

    $(document).live('mousemove',function(event) {
   
        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 = $('.main'); // 局部变量:按照重新排列过的顺序  再次获取 各个元素的坐标,
        tempDiv = $(".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);
            }
           
        });

    }).live('mouseup',function(event){
       theDiv.insertBefore(tempDiv);  // 拖拽元素插入到 虚线div的位置上
        theDiv.attr("class", "main"); //恢复对象的初始样式
        $('.dash').remove(); // 删除新建的虚线div
        $('.main').each(function(i){
            videos[i]=$(this).find("td").attr("data-video");
        });
        move=false;
         //alert(videos[2]);
    });

    /**动态加载**/
    var $btn=$('button'); 
    var tpl='<tr class="main" id="main6"><td data-video="v6">div6</td></tr>';
    $btn.live('click',function(){
        //alert('yes');
       var $table=$('.mytable');
       $table.append(tpl);
    });

});
</script>
<script type="text/javascript">
   
</script>
</head>

<body> 
<div class="box" id="box">
    <table class="mytable">
    <tr class="main" id="main1" ><td data-video="v1">div1</td></tr>
    <tr class="main" id="main2" ><td data-video="v2">div2</td></tr>
    <tr class="main" id="main3" ><td data-video="v3">div3</td></tr>
    <tr class="main" id="main4" ><td data-video="v4">div4</td></tr>
    <tr class="main" id="main5" ><td data-video="v5">div5</td></tr>
    </table>
    <button>按钮</button>
</div>
</body>
</html>


 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值