Jquery EasyUI的Draggable与Droppable插件

 

中文网帮助文档:http://www.jeasyui.net/plugins/152.html

Draggable 与 Droppable 插件: 配合一起使用

一、EasyUI Draggable 可拖动       

属性

名称类型描述默认值
proxystring,function拖动时要使用的代理元素,设置为 'clone' 时,克隆元素将被用作代理。如果指定一个函数,它必须返回一个 jQuery 对象。
下面的实例演示了如何创建简单的代理对象。
 
  1. $('.dragitem').draggable({
  2. proxy: function(source){
  3. var p = $('<div style="border:1px solid #ccc;width:80px"></div>');
  4. p.html($(source).html()).appendTo('body');
  5. return p;
  6. }
  7. });
null
revertboolean如果设置为 true,拖动结束后元素将返回它的开始位置。false
cursorstring拖动时的 css 光标(cursor)move
deltaXnumber拖动的元素相对于当前光标的 X 轴位置。null
deltaYnumber拖动的元素相对于当前光标的 Y 轴位置。null
handleselector启动可拖动(draggable)的处理(handle)。null
disabledboolean如果设置为 true,则停止可拖动(draggable)。false
edgenumber能够在其中开始可拖动(draggable)的拖动宽度。0
axisstring定义拖动元素可在其上移动的轴,可用的值是 'v' 或 'h',当设为 null,将会沿着 'v' 和 'h' 的方向移动。null

事件

名称参数描述
onBeforeDrage拖动前触发,返回 false 就取消拖动。
onStartDrage目标对象开始拖动时触发
onDrage拖动期间触发。返回 false 将不进行实际的拖动。
onStopDrage拖动停止时触发

方法

名称参数描述
optionsnone返回选项(options)属性(property)。
proxynone如果设置了代理(proxy)属性就返回拖动代理(proxy)。
enablenone启用拖动动作。
disablenone禁用拖动动作。

使用 JS 创建可拖动(draggable)元素。

      1、基本拖动:拖拽标题实现可拖动

	<h3>欢迎进入首页</h3>
	<div id="dg">
		<div id="title">标题</div>
		EasyUI Draggable 可拖动
	</div>

---index.css--
@charset "UTF-8";
#dg{
	width: 400px;
	height: 200px;
	border: 1px solid #000;
}
#title{
	width: 100%;
	height: 35px;
	background-color: #ccc;
}

---index.js--
$(function(){
	$("#dg").draggable({
		proxy: "clone",
		handle: "#title",
		cursor: "move",//pointer
	});
});

   

  2、约束拖动:一个子div只能在其父div里拖拽

	<div id="context">
		<div id="dg">Draggable 可拖动div</div>
		父div
	</div>

--index.css--
@charset "UTF-8";
#context{
	width: 400px;
	height: 200px;
	border: 5px solid #abc;
	margin-left: 50px;
	position:relative; //父div必须为相对定位
}
#dg{
	width: 80px;
	height: 80px;
	border: 3px solid red;
	background-color: #ccc;
}
--index.js--
$(function(){	
	$("#dg").draggable({
		onDrag: function(event){ //拖动期间触发事件
			var e = event.data;
			if(e.left < 0){
				e.left = 0;
			}
			if(e.top < 0){
				e.top = 0;
			}
			if (e.left + $(e.target).outerWidth() > $(e.parent).width()){
				e.left = $(e.parent).width() - $(e.target).outerWidth();
			}
			if (e.top + $(e.target).outerHeight() > $(e.parent).height()){
				e.top = $(e.parent).height() - $(e.target).outerHeight();
			}
		}
	});
});

 

二、EasyUI Droppable 可放置

    

属性

名称类型描述默认值
acceptselector确定将被接受的可拖动元素null
disabledboolean如果设置为 true,则停止可放置(droppable)。false

事件

名称参数描述
onDragEntere,source当可拖动元素被拖进来时触发。source 参数指被拖动的 DOM 元素。
onDragOvere,source当可拖动元素被拖过时触发。source 参数指被拖动的 DOM 元素。
onDragLeavee,source当可拖动元素被拖离开时触发。source 参数指被拖动的 DOM 元素。
onDrope,source当可拖动元素被放下时触发。source 参数指被拖动的 DOM 元素。

方法

名称参数描述
optionsnone返回选项(options)对象。
enablenone启用可放置功能。
disablenone禁用可放置功能。

使用 javascript 创建可放置(droppable)区域

1、基本拖拽放置改变项目排序

     insertAfter() 方法在被选元素之后插入 HTML 标记或已有的元素。

	<ul>
		<li class="drag-item">Drag 1</li>
		<li class="drag-item">Drag 2</li>
		<li class="drag-item">Drag 3</li>
		<li class="drag-item">Drag 4</li>
		<li class="drag-item">Drag 5</li>
		<li class="drag-item">Drag 6</li>
	</ul>

---index.css--
@charset "UTF-8";
ul {
	margin-left: 10px;
}

.drag-item {
	list-style-type: none; /* 设置列表项标记的类型: 无标记。 */
	display: block;
	margin: 10px;
	padding: 5px;
	border: 1px solid #ccc;
	width: 300px;
	background: #fafafa;
	color: #444;
	border: 1px solid #ccc;
	padding: 5px;
}

.arrows {
	display: none; /* 通过display来控制箭头元素生成的显示框类型 */
	position: absolute;
	font-size: 9px;
	width: 10px;
	height: 10px;
	color: red;
}

---index.js--
$(function(){	
	var arrows = $("<div class='arrows'>&gt;&gt;</div>").appendTo("body");
	$(".drag-item").draggable({
		revert: true
	});
	$(".drag-item").droppable({
		onDragOver: function(e,source){
			arrows.css({
				display: "block",
				left: $(this).offset().left - 15,
				top: $(this).offset().top + $(this).outerHeight()-5
			});
		},
		onDragLeave: function(e,source){
			arrows.hide();
		},
		onDrop: function(e,source){
			$(source).insertAfter(this);
			arrows.hide();
		}
	});

});

       

2、基本拖拽放置:可互相接收一个拖拽

     拖过去,也可以拖回来,

	<div id="source">
		drag me!
		<div id="dg1" class="drag">Drag 1</div>
		<div id="dg2" class="drag">Drag 2</div>
		<div id="dg3" class="drag">Drag 3</div>
	</div>
	<div id="target">
		drop here!
	</div>

--index.css--
@charset "UTF-8";
#source {
	border: 1px solid #ccc;
	width: 200px;
	height: 300px;
	float: left;
	margin: 5px;
}

#target {
	width: 200px;
	height: 300px;
	float: left;
	margin: 5px;
	border: 1px solid #ccc;
}

.drag {
	width: 100px;
	height: 30px;
	padding: 10px;
	margin: 5px;
	border: 1px solid #ccc;
	background: #AACCFF;
}

.over {
	background: #FBEC88;
}
.overs {
	background: #00ff00;
}
#dg{
	width: 80px;
	height: 80px;
	border: 3px solid red;
	background-color: #ccc;
}

--index.js--
$(function(){	
	$(".drag").draggable({
		proxy: 'clone',
		revert: true,
		cursor: 'auto',
		onStartDrag: function(){ //目标对象开始拖动时触发
			$(this).draggable('options').cursor='move';
		},
		onStopDrag:function(){ //拖动停止时触发
			$(this).draggable('options').cursor='auto';
		}
	});
	$('#target').droppable({
		accept:'#dg1,#dg3',
		onDragEnter:function(e,source){ //当可拖动元素被拖进来时触发
			$(source).draggable('options').cursor='pointer';
			$(source).draggable('proxy').css('border','1px solid red');
			$(this).addClass('over');
		},
		onDragLeave:function(e,source){ //当可拖动元素被拖离开时触发
			$(source).draggable('options').cursor='not-allowed';
			$(source).draggable('proxy').css('border','1px solid #ccc');
			$(this).removeClass('over');
		},
		onDrop:function(e,source){ //当可拖动元素被放下时触发
			$(this).append(source)
			$(this).removeClass('over');
		}
	});
	$('#source').droppable({
		accept:'#dg1,#dg2,#dg3',
		onDragEnter:function(e,source){ //当可拖动元素被拖进来时触发
			$(source).draggable('options').cursor='pointer';
			$(source).draggable('proxy').css('border','1px solid blue');
			$(this).addClass('overs');
		},
		onDragLeave:function(e,source){ //当可拖动元素被拖离开时触发
			$(source).draggable('options').cursor='not-allowed';
			$(source).draggable('proxy').css('border','1px solid #ccc');
			$(this).removeClass('overs');
		},
		onDrop:function(e,source){ //当可拖动元素被放下时触发
			$(this).append(source)
			$(this).removeClass('overs');
		}
	});
});

     

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EasyUI DraggableEasyUI框架中的一个插件,它提供了拖拽功能,可以让用户通过鼠标拖动元素来改变其位置。使用EasyUI Draggable插件,可以轻松地实现元素的拖拽功能,无需编写复杂的JavaScript代码。 使用EasyUI Draggable插件,需要引入EasyUI框架的相关文件,并在需要应用拖拽功能的元素上添加相应的class和属性。以下是使用EasyUI Draggable的基本步骤: 1. 引入EasyUI框架的CSS和JavaScript文件。 2. 在需要应用拖拽功能的元素上添加class="easyui-draggable"属性。 3. 可选:通过设置属性来自定义拖拽的行为,如设置axis属性限制只能在水平或垂直方向拖拽,设置handle属性指定拖拽的手柄元素等。 下面是一个示例代码,演示了如何使用EasyUI Draggable插件: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>EasyUI Draggable示例</title> <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/easyui/dist/themes/default/easyui.css"> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/easyui/dist/jquery.easyui.min.js"></script> </head> <body> <div class="easyui-draggable" style="width: 200px; height: 200px; background-color: #ccc;"> 拖拽我! </div> </body> </html> ``` 在上述示例中,我们引入了EasyUI框架的CSS和JavaScript文件,并在一个div元素上添加了class="easyui-draggable"属性。这样,该div元素就可以被拖拽了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值