记录实现拖拽的几种方式

function moveBox1(e,drag) {  
    //拖拽功能(主要是触发三个事件:onmousedown\onmousemove\onmouseup)  
    var drag = drag.parentNode;  
    					
    //点击某物体时,用drag对象即可,move和up是全局区域,也就是整个文档通用,应该使用document对象而不是drag对象(否则,采用drag对象时物体只能往右方或下方移动)  
    drag.onmousedown = function(e) {  
        var e = e || window.event; //兼容ie浏览器  
        var diffX = e.clientX - drag.offsetLeft; //鼠标点击物体那一刻相对于物体左侧边框的距离=点击时的位置相对于浏览器最左边的距离-物体左边框相对于浏览器最左边的距离  
        var diffY = e.clientY - drag.offsetTop;  
  
        /*低版本ie bug:物体被拖出浏览器可是窗口外部时,还会出现滚动条,  
            解决方法是采用ie浏览器独有的2个方法setCapture()\releaseCapture(),这两个方法,  
            可以让鼠标滑动到浏览器外部也可以捕获到事件,而我们的bug就是当鼠标移出浏览器的时候,  
            限制超过的功能就失效了。用这个方法,即可解决这个问题。注:这两个方法用于onmousedown和onmouseup中*/  
            if(typeof drag.setCapture!='undefined'){  
                drag.setCapture();  
            }  
  
        document.onmousemove = function(e) {  
        	console.log("----------");
            var e = e || window.event; //兼容ie浏览器  
            var left=e.clientX-diffX;  
            var top=e.clientY-diffY;  
  
            //控制拖拽物体的范围只能在浏览器视窗内,不允许出现滚动条  
            if(left<0){  
                left=0;  
            }else if(left >window.innerWidth-drag.offsetWidth){  
                left = window.innerWidth-drag.offsetWidth;  
            }  
            if(top<0){  
                top=0;  
            }else if(top >window.innerHeight-drag.offsetHeight){  
                top = window.innerHeight-drag.offsetHeight;  
            }  
  
            //移动时重新得到物体的距离,解决拖动时出现晃动的现象  
            drag.style.left = left+ 'px';  
            drag.style.top = top + 'px';  
        };  
        document.onmouseup = function(e) { //当鼠标弹起来的时候不再移动
        	console.log("mouseup");
        	document.onmousemove = null;  
            document.onmouseup = null; //预防鼠标弹起来后还会循环(即预防鼠标放上去的时候还会移动)  
  
            //修复低版本ie bug  
            if(typeof drag.releaseCapture!='undefined'){  
                drag.releaseCapture();  
            }  
        };  
    };  
}; 

function moveBox2(){
	$(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(){ //循环初始化
		     $(".BIbgPanel").find(".BIPanel").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","BIPaneldash");
		         // 创建新元素 插入拖拽元素之前的位置(虚线框)
		         $("<div class='drag_module_dash'></div>").insertBefore(theDiv);
		       });
		     });
		  }
		  loopbox();
		   $(".BIbgPanel").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 = $('.BIPanel'); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标,
		     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", "BIPanel"); //恢复对象的初始样式
		     $('.drag_module_dash').remove(); // 删除新建的虚线div
		     move=false;
		   });
		});
}
function moveBox3(panelId){
	var Sys = (function(ua){ 
	    var s = {}; 
	    s.IE = ua.match(/msie ([\d.]+)/)?true:false; 
	    s.Firefox = ua.match(/firefox\/([\d.]+)/)?true:false; 
	    s.Chrome = ua.match(/chrome\/([\d.]+)/)?true:false; 
	    s.IE6 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6))?true:false; 
	    s.IE7 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 7))?true:false; 
	    s.IE8 = (s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 8))?true:false; 
	    return s; 
	})(navigator.userAgent.toLowerCase());
	var $ = function (id) { 
	    return document.getElementById(id); 
	};
	var Css = function(e,o){ 
	    for(var i in o) 
	    e.style[i] = o[i]; 
	};
	var Extend = function(destination, source) { 
	    for (var property in source) { 
	        destination[property] = source[property]; 
	    } 
	};
	var Bind = function(object, fun) { 
	    var args = Array.prototype.slice.call(arguments).slice(2); 
	    return function() { 
	        return fun.apply(object, args); 
	    } 
	};
	var BindAsEventListener = function(object, fun) { 
	    var args = Array.prototype.slice.call(arguments).slice(2); 
	    return function(event) { 
	        return fun.apply(object, [event || window.event].concat(args)); 
	    } 
	};
	var CurrentStyle = function(element){ 
	    return element.currentStyle || document.defaultView.getComputedStyle(element, null); 
	};
	function addListener(element,e,fn){ 
	    element.addEventListener?element.addEventListener(e,fn,false):element.attachEvent("on" + e,fn); 
	}; 
	function removeListener(element,e,fn){ 
	    element.removeEventListener?element.removeEventListener(e,fn,false):element.detachEvent("on" + e,fn) 
	};
	var Class = function(properties){ 
	    var _class = function(){return (arguments[0] !== null && this.initialize && typeof(this.initialize) == 'function') ? this.initialize.apply(this, arguments) : this;}; 
	    _class.prototype = properties; 
	    return _class; 
	};
	var Resize =new Class({ 
	    initialize : function(obj){ 
	        this.obj = obj; 
	        this.resizeelm = null; 
	        this.fun = null; //记录触发什么事件的索引 
	        this.original = []; //记录开始状态的数组 
	        this.width = null; 
	        this.height = null; 
	        this.fR = BindAsEventListener(this,this.resize); 
	        this.fS = Bind(this,this.stop);     
	    }, 
	    set : function(elm,direction){ 
	        if(!elm)return; 
	        this.resizeelm = elm; 
	        addListener(this.resizeelm,'mousedown',BindAsEventListener(this, this.start, this[direction])); 
	        return this; 
	    }, 
	    start : function(e,fun){ 
	        this.fun = fun; 
	        this.original = [parseInt(CurrentStyle(this.obj).width),parseInt(CurrentStyle(this.obj).height),parseInt(CurrentStyle(this.obj).left),parseInt(CurrentStyle(this.obj).top)];
	        this.width = (this.original[2]||0) + this.original[0]; 
	        this.height = (this.original[3]||0) + this.original[1]; 
	        addListener(document,"mousemove",this.fR); 


	        addListener(document,'mouseup',this.fS); 
	    }, 
	    resize : function(e){ 
	        this.fun(e); 
	        Sys.IE?(this.resizeelm.onlosecapture=function(){this.fS()}):(this.resizeelm.οnblur=function(){this.fS()}) 
	    }, 
	    stop : function(){ 
	        removeListener(document, "mousemove", this.fR); 
	        removeListener(document, "mousemove", this.fS); 
	        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();     
	    }, 
	    up : function(e){ 
	        this.height>e.clientY?Css(this.obj,{top:e.clientY + "px",height:this.height-e.clientY + "px"}):this.turnDown(e); 
	    }, 
	    down : function(e){ 
	        e.clientY>this.original[3]?Css(this.obj,{top:this.original[3]+'px',height:e.clientY-this.original[3]+'px'}):this.turnUp(e);     
	    }, 
	    left : function(e){ 
	        e.clientX<this.width?Css(this.obj,{left:e.clientX +'px',width:this.width-e.clientX + "px"}):this.turnRight(e);         
	    }, 
	    right : function(e){ 
	        e.clientX>this.original[2]?Css(this.obj,{left:this.original[2]+'px',width:e.clientX-this.original[2]+"px"}):this.turnLeft(e)    ; 
	    }, 
	    leftUp:function(e){ 
	        this.up(e);this.left(e); 
	    }, 
	    leftDown:function(e){ 
	        this.left(e);this.down(e); 
	    }, 
	    rightUp:function(e){ 
	        this.up(e);this.right(e); 
	    }, 
	    rightDown:function(e){ 
	        this.right(e);this.down(e); 
	    },                 
	    turnDown : function(e){ 
	        Css(this.obj,{top:this.height+'px',height:e.clientY - this.height + 'px'}); 
	    }, 
	    turnUp : function(e){ 
	        Css(this.obj,{top : e.clientY +'px',height : this.original[3] - e.clientY +'px'}); 
	    }, 
	    turnRight : function(e){ 
	        Css(this.obj,{left:this.width+'px',width:e.clientX- this.width +'px'}); 
	    }, 
	    turnLeft : function(e){ 
	        Css(this.obj,{left:e.clientX +'px',width:this.original[2]-e.clientX+'px'});          
	    }         
	}); 
	 new Resize($(panelId)).set($(panelId+'rUp'),'up').set($(panelId+'rDown'),'down').set($(panelId+'rLeft'),'left').set($(panelId+'rRight'),'right').set($(panelId+'rLeftUp'),'leftUp').set($(panelId+'rLeftDown'),'leftDown').set($(panelId+'rRightDown'),'rightDown').set($(panelId+'rRightUp'),'rightUp');
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值