高级拖拽

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" value = '创建div' id="btn" />
</body>
</html>
<script type="text/javascript">
var oBtn = document.getElementById('btn');
oBtn.onclick = function(){
var dd = new DragDiv(100,100);
dd.createDiv();
}
function DragDiv(w,h){
this.w = w;
this.h = h;
}
//创建div方法
DragDiv.prototype.createDiv = function(){
this.oDiv = document.createElement("div");
this.oDiv.style.width = this.w + "px";
this.oDiv.style.height = this.h + "px";
this.oDiv.style.background ="#"+getColor();
this.oDiv.style.position = "absolute";
this.oDiv.style.left = this.rand(100,window.innerWidth-100) +"px";
this.oDiv.style.top = this.rand(100,window.innerHeight-100) +"px";
document.body.appendChild(this.oDiv);
this.drag();
}
//随机方法
DragDiv.prototype.rand = function(min,max){
return Math.floor(Math.random()*(max-min+1)) + min;
}
//拖拽
DragDiv.prototype.drag = function(){
var that = this;
this.oDiv.onmousedown = function(e){
var e = e || event;
var rex = e.offsetX;
var rey = e.offsetY;
document.onmousemove = function(e){
var e = e || event;
var x = e.clientX - rex;
var y = e.clientY - rey;
var maxL = window.innerWidth - 100;
var maxT = window.innerHeight - 100;
x = x<=0 ? 0 : (x>maxL ? maxL : x);
    y = y<=0 ? 0 : (y>maxT ? maxT : y);
that.oDiv.style.left = x + "px";
that.oDiv.style.top = y + "px";
}
}
document.onmouseup = function(){
document.onmousemove = "";
}
}
function getRand(min,max){
return Math.floor( Math.random()*(max-min+1) + min );
}
function getColor(){
var r = getRand(0,255).toString(16);
var g = getRand(0,255).toString(16);
var b = getRand(0,255).toString(16);
return (r.length<2?"0"+r:r) + (g.length<2?"0"+g:g) + (b.length<2?"0"+b:b) ;
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值