div拖动布局drag.js

html 复制代码
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>drag.js</title>
    <script type="text/javascript" src="jquery-1.12.4.min.js"></script> 
    <script type="text/javascript" src="drag.js"></script> 
    <style>
		html,body{background-color:#101028;height:100%;width:100%;padding:0;margin:0}
		.box {
			width:300px;
			height:200px;
			border: 1px solid #fff;
			color: #fff
		}
    </style>
	<script>
		$(document).ready(function(){
			$("#box1").dragging({ 
				move : 'both', 
				randomPosition : true, //是否随机显示位置
				left: 20,
				top: 30
			});
			$("#box2").dragging({ 
				move : 'both', 
				randomPosition : false,
				left: 150,
				top: 200
			});
		})
	</script>
</head>
<body>
    <div class="box" id="box1">abcde</div>
    <div class="box" id="box2">234</div>
</body>
</html>

drag.js

javascript 复制代码
$.fn.extend({
	dragging:function(data){
		var $this = $(this);
		var xPage;
		var yPage;
		var X;
		var Y;
		var xRand = 0;
		var yRand = 0;
		var father = $this.parent();
		var defaults = {
			move : 'both',
			randomPosition : true,
			hander:1,
			top: 0,
			left: 0
		}
		var opt = $.extend({},defaults,data);
		var movePosition = opt.move;
		var random = opt.randomPosition;

		var hander = opt.hander;

		if(hander == 1){
			hander = $this; 
		}else{
			hander = $this.find(opt.hander);
		}

		//---初始化
		father.css({"position":"relative","overflow":"hidden"});
		$this.css({"position":"absolute"});
		hander.css({"cursor":"move"});

        //元素使用box-sizing:content-box时不包括padding和border,加上
		var faWidth = Math.ceil(father.width());
		var thisWidth = Math.ceil($this.width())
							+ Math.ceil(parseFloat($this.css('padding-left')))
							+ Math.ceil(parseFloat($this.css('padding-right')))
							+ Math.ceil(parseFloat($this.css('border-left-width')))
							+ Math.ceil(parseFloat($this.css('border-right-width')));

		var faHeight = Math.ceil(father.height());
		var thisHeight = Math.ceil($this.height())
							+ Math.ceil(parseFloat($this.css('padding-top')))
							+ Math.ceil(parseFloat($this.css('padding-bottom')))
							+ Math.ceil(parseFloat($this.css('border-top-width')))
							+ Math.ceil(parseFloat($this.css('border-bottom-width')));

		var mDown = false;
		var positionX;
		var positionY;
		var moveX ;
		var moveY ;

		(function(){ //随机函数
			$this.each(function(index){
				var randY = random ? parseInt(Math.random()*(faHeight-thisHeight)) : opt.top;
				var randX = random ? parseInt(Math.random()*(faWidth-thisWidth)) : opt.left;
				if(movePosition.toLowerCase() == 'x'){
					$(this).css({
						left:randX
					});
				}else if(movePosition.toLowerCase() == 'y'){
					$(this).css({
						top:randY
					});
				}else if(movePosition.toLowerCase() == 'both'){
					$(this).css({
						top:randY,
						left:randX
					});
				}
				
			});
		})();

		hander.mousedown(function(e){
			father.children().css({"zIndex":"0"});
			$this.css({"zIndex":"1"});
			mDown = true;
			X = e.pageX;
			Y = e.pageY;
			positionX = $this.position().left;
			positionY = $this.position().top;
			return false;
		});

		$this.mouseup(function(e){
			mDown = false;
			//保存位置信息
			console.log($this.attr("id"));
			console.log("top:" + $this.css("top"), "left:" + $this.css("left"));
		});

		$this.mousemove(function(e){
			xPage = e.pageX;
			moveX = positionX+xPage-X;

			yPage = e.pageY;
			moveY = positionY+yPage-Y;

			function thisXMove(){ //x轴移动
				if(mDown == true){
					$this.css({"left":moveX});
				}else{
					return;
				}
				if(moveX < 0){
					$this.css({"left":"0"});
				}
				if(moveX > (faWidth-thisWidth)){
					$this.css({"left":faWidth-thisWidth});
				}
				return moveX;
			}

			function thisYMove(){ //y轴移动
				if(mDown == true){
					$this.css({"top":moveY});
				}else{
					return;
				}
				if(moveY < 0){
					$this.css({"top":"0"});
				}
				if(moveY > (faHeight-thisHeight)){
					$this.css({"top":faHeight-thisHeight});
				}
				return moveY;
			}

			function thisAllMove(){ //全部移动
				if(mDown == true){
					$this.css({"left":moveX,"top":moveY});
				}else{
					return;
				}
				if(moveX < 0){
					$this.css({"left":5});
				}
				if(moveX > (faWidth-thisWidth)){
					$this.css({"left":faWidth-thisWidth-5});
				}

				if(moveY < 0){
					$this.css({"top":5});
				}
				if(moveY > (faHeight-thisHeight)){
					$this.css({"top":faHeight-thisHeight-5});
				}
			}

			if(movePosition.toLowerCase() == "x"){
				thisXMove();
			}else if(movePosition.toLowerCase() == "y"){
				thisYMove();
			}else if(movePosition.toLowerCase() == 'both'){
				thisAllMove();
			}
		});

	}
});
相关推荐
牧羊狼的狼4 小时前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手5 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
luckys.one5 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
魔云连洲5 小时前
深入解析:Vue与React的异步批处理更新机制
前端·vue.js·react.js
mCell6 小时前
JavaScript 的多线程能力:Worker
前端·javascript·浏览器
weixin_437830947 小时前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr
超级无敌攻城狮7 小时前
3 分钟学会!波浪文字动画超详细教程,从 0 到 1 实现「思考中 / 加载中」高级效果
前端
excel8 小时前
用 TensorFlow.js Node 实现猫图像识别(教学版逐步分解)
前端
gnip9 小时前
JavaScript事件流
前端·javascript
CodeCraft Studio9 小时前
国产化Word处理组件Spire.DOC教程:使用 Python 将 Markdown 转换为 HTML 的详细教程
python·html·word·markdown·国产化·spire.doc·文档格式转换