自定义拖拽框插件

1、HTML代码如下 注意引入jquery和自定仪的drag.js , 路径正确打开页面就能看到效果

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus?">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>拖拽jQuery  </title>
  <style type="text/css">
	*{margin:0;padding:0;border:0;}
  </style>
 </head>
 <body>
	
	<div class="divbox" arrow="left" style="border:1px solid red;width:500px;height:200px;background:#f7f7f7;position:absolute;top:100px;left:100px;">
		<h3 style="height:30px;background:#181818;color:#fff;">1111111</h3>
		左右移动 由arrow="left"控制
	</div>
	
	<div class="divbox" arrow="top" style="border:1px solid red;width:500px;height:200px;background:#f7f7f7;position:absolute;top:100px;right:100px;">
		<h3 style="height:30px;background:#181818;color:#fff;">22222222</h3>
		上下移动 由arrow="top"控制
	</div>
	
	<div class="divbox"  style="border:1px solid red;width:500px;height:200px;background:#f7f7f7;position:absolute;bottom:100px;left:100px;">
		<h3 style="height:30px;background:#181818;color:#fff;">22222222</h3>
		全局移动 没有arrow属性就没有限制
	</div>

	<div class="divbox" handle="" style="border:1px solid red;width:500px;height:200px;background:#f7f7f7;position:absolute;bottom:100px;right:100px;">
		全局移动 没有h3标签 需要添加handle属性 如果为空则整体都能拖动
	</div>

	
	<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
	<script type="text/javascript" src="drag.js"></script>
	<script type="text/javascript">
		$(function(){
			$('.divbox').WanglDrag({handle:'h3'});
		});
	</script>
 </body>
</html>

2、自定义drag.js

/**
 * @author:wangl
 */
(function($){
	/**插件入口**/
	$.fn.WanglDrag = function(options){
		var z_index = 0;
		this.each(function(){
			//参数优先级从高到低 为: 属性参数 、 调用参数 、 默认参数
			var opts = $.extend({},$.fn.WanglDrag.defaults,options,$.fn.WanglDrag.parseOptions(this));
			alert(JSON.stringify(opts));
			opts.box = $(this);
			init(opts);
		});
	};
	var z_index = -1;
	/**初始化**/
	function init(opts){
		var $box = opts.box,
			x = 0,
			y = 0,
			left = 0,
			top = 0,
			b_width = $(document).width(),
			b_height = $(document).height(),
			maxWidth = b_width - $box.outerWidth(),
			maxHeight = b_height - $box.outerHeight();
		$handle = opts.handle ? $box.find(opts.handle) : $box;
		
		$handle.css("cursor",'move');
		
		$box.on('mousemove',function(e){
			$box.css({"z-index":z_index++});
		});
		
		$handle.on('mousedown',function(e){
			x = e.clientX;
			y = e.clientY;
			var $this = $(this);
			var offset = $(this).offset();
			left = offset.left;
			top = offset.top;
			var isDrag = true;
			//alert('x:'+x+',y:'+y+',left:'+left+',top:'+top);
			$(document).on('mousemove',function(e){
				if(isDrag){
					var new_left = left + e.clientX - x;
					var new_top = top + e.clientY - y;
					if(opts.arrow && opts.arrow=="top")new_left=left;//控制垂直方向
					if(opts.arrow && opts.arrow=="left")new_top=top;//控制水平方向
					
					if(new_left<=0)new_left = 2;
					if(new_top<=0)new_top = 2;
					
					if(new_left >= maxWidth)new_left = maxWidth;
					if(new_top >= maxHeight)new_top = maxHeight;
					$box.css({"top":new_top,"left":new_left});
				}
			}).on('mouseup',function(){
				isDrag = false;
			});
		});
	};
	/**默认参数*/
	$.fn.WanglDrag.defaults = {
		handle : "",
		arrow : ""
	}
	/**属性参数*/
	$.fn.WanglDrag.parseOptions = function(target){
		var $target = $(target);
		return {
			handle : $target.attr('handle'),
			arrow : $target.attr('arrow')
		}
	}
})(jQuery)


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值