JS拽托div移动-宽度高度自适应

#design{
	position: fixed;
	width: auto;
	height: auto;
	width: 15rem;
	min-width: 15rem;
	min-height: 30rem;
	max-width: 70%;
	max-height: 90%;
	top: calc(50% - 15rem);
	left: calc(100% - 15rem);
	
	z-index: 99999;
	background-color: #fafafa;
	cursor: pointer;
}
<div id="design" style=""></div>
$(document).ready(function() {
	var div = document.getElementById("design");
	div.onmousedown = function() {
		var clientX_ = -1;
		var clientY_ = -1;
		var is = false;
		div.onmousemove = function(e) {
			var evt = e || event;
			if (is) {
				var x = $("#design").css("left");
				x = x.substring(0, x.length - 2);
				x = Number(x);
				x=x + evt.clientX - clientX_;
				if(x>0){
					var width_=(Number($("#design").css("width").substring(0,$("#design").css("width").length-2))/2);
					if((document.body.clientWidth-width_)>=(x+width_)){
						$("#design").css("left", x + 'px');
					}
				}
				var y = $("#design").css("top");
				y = y.substring(0, y.length - 2);
				y = Number(y);
				y=y + evt.clientY - clientY_;
				if(y>0){
					var height_=(Number($("#design").css("height").substring(0,$("#design").css("height").length-2))/2);
					if((document.body.clientHeight-height_)>=(y+height_)){
						$("#design").css("top", y + 'px');
					}
				}
			}
			is = true;
			clientX_ = evt.clientX;
			clientY_ = evt.clientY;
		};
		div.onmouseout=function() {
			div.onmousemove = null;
		};
		div.onmouseup = function() {
			div.onmousemove = null;
		};
	};
});

document.body.clientWidth,document.body.clientHeight约束div的位置

 

全部代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			html,body,.all{
				width: 100%;
				height: 100%;
			}
			#design{
				position: fixed;
				width: auto;
				height: auto;
				width: 15rem;
				min-width: 15rem;
				min-height: 30rem;
				max-width: 70%;
				max-height: 90%;
				top: calc(50% - 15rem);
				left: calc(100% - 15rem);
				
				z-index: 99999;
				background-color: #eee;
				cursor: pointer;
			}
		</style>
		
		<script type="application/javascript" src="public/js/jquery-1.8.0.min.js"></script>
	</head>
	<body >
		<div id="design" style="">
			
		</div>
		<script>
			$(document).ready(function() {
				var div = document.getElementById("design");
				div.onmousedown = function() {
					var clientX_ = -1;
					var clientY_ = -1;
					var is = false;
					div.onmousemove = function(e) {
						var evt = e || event;
						if (is) {
							var x = $("#design").css("left");
							x = x.substring(0, x.length - 2);
							x = Number(x);
							x=x + evt.clientX - clientX_;
							if(x>0){
								var width_=(Number($("#design").css("width").substring(0,$("#design").css("width").length-2))/2);
								if((document.body.clientWidth-width_)>=(x+width_)){
									$("#design").css("left", x + 'px');
								}
							}
							var y = $("#design").css("top");
							y = y.substring(0, y.length - 2);
							y = Number(y);
							y=y + evt.clientY - clientY_;
							if(y>0){
								var height_=(Number($("#design").css("height").substring(0,$("#design").css("height").length-2))/2);
								if((document.body.clientHeight-height_)>=(y+height_)){
									$("#design").css("top", y + 'px');
								}
							}
						}
						is = true;
						clientX_ = evt.clientX;
						clientY_ = evt.clientY;
					};
					div.onmouseout=function() {
						div.onmousemove = null;
					};
					div.onmouseup = function() {
						div.onmousemove = null;
					};
				};
			});
		</script>
	</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值