js实现拖动组件移动效果

2020-07-01 17:54:53 浏览数 (1)

2015-04-15 14:06:09

今天我来给大家介绍一种js特效,这种特效是当你用鼠标点击组件移动到其他地方后,这个组件就定在了那个地方,这种效果通常用来做视图化排版的。下面我来把代码贴出来给大家看看

代码语言:javascript复制
 <div id="container">
			<div class="column">
				<div class="item">
					<h3>Header</h3>
					<p>column1 item1</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column1 item2<br />Some text here, so that the height is different!</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column1 item3</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column1 item4</p>
				</div>
			</div>
			<div class="column" style="width:300px;">
				<div class="item">
					<h3>Header</h3>
					<p>column2 item1</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column2 item2</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column2 item3<br />Some text here, so that the height is different!</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column2 item4<br />Some text here, so that the height is different!</p>
				</div>
			</div>
			<div class="column">
				<div class="item">
					<h3>Header</h3>
					<p>column3 item1</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column3 item2</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column3 item3</p>
				</div>
				<div class="item">
					<h3>Header</h3>
					<p>column3 item4</p>
				</div>
			</div>
        </div>

这段代码是html代码,主要是大体的div布局,我们在这里的组件用的是div,通过下面的js代码来实现组件的移动

代码语言:javascript复制
<script type="text/javascript" src="Drag.js"></script>
        <script type="text/javascript">
//------------------------Utility------------------------
function findPosX(obj) {//辅助函数 得到元素左边与浏览器左边的边距
	var curleft = 0;
	if (obj && obj.offsetParent) {
		while (obj.offsetParent) {
			curleft  = obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj && obj.x) curleft  = obj.x;
	return curleft;//   document.body.scrollLeft - document.body.clientLeft;
}

function findPosY(obj) {//辅助函数 得到元素上边与浏览器上边的边距
	var curtop = 0;
	if (obj && obj.offsetParent) {
		while (obj.offsetParent) {
			curtop  = obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj && obj.y) curtop  = obj.y;
	return curtop;//   document.body.scrollTop - document.body.clientTop;
}


var dragGhost = document.createElement("div");
dragGhost.style.border = "dashed 1px #CCCCCC";
dragGhost.style.background = "white";
dragGhost.style.display = "none";
dragGhost.style.margin = "10px";

var container;
var columns = [];
//------------------------Start Here------------------------
window.onload = function(){
	container = document.getElementById("container");
	
	for(var i=0;i<container.childNodes.length;i  ){
		if(container.childNodes[i].className == "column"){//筛选出所有的列 ff下的childNodes不可靠 :
			columns.push(container.childNodes[i]);
		}
	}
	for(var i=0;i<columns.length;i  ){
		var column = columns[i];
		for(var j=0;j<column.childNodes.length;j  ){
			var item = column.childNodes[j];
			if(item.className == "item"){
				item.column = column;//给每个拖拽对象要指明它属于哪一列 而且这个属性会随着拖动而更新的
				
				new dragItem(item);
			}
		}
	}
}
var isIE = document.all;

//------------------------Drag Item------------------------
function dragItem(item){
	//item实际上是dragBody(拖动的时候移动的整体)
	//在这里需要根据item找到handle(能够拖动的把手)
	
	var handle;
	for(var i=0;i<item.childNodes.length;i  ){
		if(item.childNodes[i].nodeName.toLowerCase() == "h3"){
			handle = item.childNodes[i];
			break;
		}
	}
	if(!handle)return;
	Drag.init(handle,item);
	item.onDragStart = function(left,top,mouseX,mouseY){
		//开始拖动的时候设置透明度
		
		this.style.opacity = "0.5";
		this.style.filter = "alpha(opacity=50)";
		dragGhost.style.height = (isIE?this.offsetHeight:this.offsetHeight - 2)   "px";
		
		//this指的是item
		
		this.style.width = this.offsetWidth   "px";//因为初始的width为auto
		this.style.left = (findPosX(this) - 5)   "px";
		this.style.top = (findPosY(this) - 5)   "px";
		this.style.position = "absolute";
		//将ghost插入到当前位置
		dragGhost.style.display = "block";
		this.column.insertBefore(dragGhost,this);
		
		
		//记录每一列的左边距 在拖动过程中判断拖动对象所在的列会用到
		this.columnsX = [];
		for(var i=0;i<columns.length;i  ){
			this.columnsX.push(findPosX(columns[i]));
		}
			
	}
	item.onDrag = function(left,top,mouseX,mouseY){
	
		//先要判断在哪一列移动
		var columnIndex = 0; 
		
		for(var i=0;i<this.columnsX.length;i  ){
			if((left   this.offsetWidth/2) > this.columnsX[i]){
				columnIndex = i;
			}
		}
		//如果columnIndex在循环中没有被赋值 则表示当前拖动对象在第一列的左边
		//此时也把它放到第一列
		
		var column = columns[columnIndex];
		
		if(this.column != column){
			//之前拖动对象不在这个列
			//将ghost放置到这一列的最下方
			
			column.appendChild(dragGhost);
			this.column = column;
		}
		
		//然后在判断放在这一列的什么位置
		
		var currentNode = null;
		for(var i=0;i<this.column.childNodes.length;i  ){
			if(this.column.childNodes[i].className == "item"
			&& this.column.childNodes[i] != this//不能跟拖动元素自己比较 否则不能在本列向下移动
			&& top < findPosY(this.column.childNodes[i])){//从上到下找到第一个比拖动元素的上边距大的元素
			
				currentNode = this.column.childNodes[i];
				break;
			}
		}
		if(currentNode)
			this.column.insertBefore(dragGhost,currentNode);
		else//拖到最下边 没有任何一个元素的上边距比拖动元素的top大 则添加到列的最后
		
			this.column.appendChild(dragGhost);
	}
	item.onDragEnd = function(left,top,mouseX,mouseY){
		this.style.opacity = "1";
		this.style.filter = "alpha(opacity=100)";
		
		this.column.insertBefore(this,dragGhost);
		
		this.style.position = "static";
		this.style.display = "block";
		this.style.width = "auto";
		dragGhost.style.display = "none";
	}
}
        </script>
        <style type="text/css">
			#container{width:800px;}
			.column{width:200px;border:solid 1px #CCCCCC;background:#FCFCFC;padding:0px;float:left;margin:10px;}
			.item{text-align:center;padding:0px;margin:10px;border:solid 1px #CCCCCC;background:white;width:auto;}
			.item h3{margin:0px;height:20px;border-bottom:solid 1px #CCCCCC;background:#CCCCCC;color:white;cursor:move;}
        </style>

这样就可以实现组件的移动了,在上面这段代码中需要引入Drag.js文件,本站提供下载链接,点击下面的下载即可。

0 人点赞