转载:一个封装好的拖动层效果

本文介绍了一种封装的鼠标拖动层类,通过简单的JavaScript实现网页元素的拖拽功能。该类支持不同浏览器环境,提供了拖动入口、拖动初始化、清除拖动ID和拖动过程等核心功能。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>鼠标拖动层JS</title>
<style type="text/css">
<!--
body{margin:0px;padding:0px;font-size:12px;background:#eee;line-height:20px;}
.bodyer{width:760px;margin:20px auto auto;border:1px dotted #ccc;background:#fff;}
.t_rt{text-align:right;}
h1,h2,h3,h4,h5,h6{font-weight:bold;margin:0px;padding:0px;font-size:12px;}
ul,li{margin:0px;padding:0px;}
li{list-style-type:none;}
h1{margin:10px;padding-right:10px;padding-bottom:5px;border-bottom:1px dotted #ccc;}
.preview{margin:10px;padding:10px;overflow:hidden;background:#eee;}
.cont{padding:10px;}
.cls{clear:both;}
.hidden{display:none;}
#sourse{border:1px dotted #ccc;width:600px;height:300px;margin:0px auto;}
.textDiv{margin:10px 40px 10px;text-align:center;}
h2{margin:0px 10px;background:#ccc;padding:5px;}
.example{margin:10px;background:#FFF;border:1px dotted #ccc;padding:10px;}
.authorInfo{width:760px; margin:10px auto 10px;text-align:center;}
.c_666{color:#666;}
.red{color:red;}
.scrolldoorFrame{width:600px;border:1px solid #FFF;margin:0px auto;overflow:hidden;position:relative;height:400px;}
.scrollUl{width:400px;border-bottom:1px solid #CCC;overflow:hidden;height:35px;}
.scrollUl li{float:left;}
.bor03{border:1px solid #ccc;border-top-width:0px;}
.sd01{cursor:pointer;border:1px solid #CCC;background:#FFF;margin:5px;padding:2px;font-weight:bold;}
.sd02{cursor:pointer;border:0px solid #CCC;margin:5px;padding:2px;}
-->
</style>
<script type="text/javascript">
function dragClass(){
 this.mouseState = document.all ? 1 : 0 ;
 this.x;
 this.y;
 this.dragId = null;
 this.index = 100;
 document.onselectstart = function(){return false;};
}
dragClass.prototype = {
 dragStar:function(dragId,moveId){//拖动入口函数
  var _this = this;
  _this.$(dragId).style.cursor = "move";
  _this.$(dragId).onmousedown = function(e){
   var e = e ? e : event;
   //_this.$(moveId).style.zIndex = _this.index++;
   if(e.button == _this.mouseState)
   {
    _this.setDragInfo(e,moveId,moveId);
    _this.dragPro(moveId);
   }
  },
  _this.$(moveId).onmousedown = function(e){
   _this.$(moveId).style.zIndex = _this.index++;
  },
  _this.$(dragId).onmouseup = function(){
   _this.clearDragId();
  }
  document.onmouseup = function(){
   _this.clearDragId();
  }
 },
 setDragInfo:function(e,dragId,moveId){//拖动初始化
  this.x = e.clientX;
  this.y = e.clientY;
  this.dragId = dragId;
  if(this.$(moveId).style.position != "absolute")
  {
   this.$(moveId).style.width = this.$(moveId).offsetWidth;
   this.$(moveId).style.height = this.$(moveId).offsetHeight;
   this.$(moveId).style.position = "absolute";
   this.$(moveId).style.left = this.$(moveId).offsetLeft;
   this.$(moveId).style.top = this.$(moveId).offsetTop;
  }
 },
 clearDragId:function(){ //清除拖动ID
  this.dragId = null;
 },
 dragPro:function(moveId){
  var _this = this;
  document.onmousemove = function(e){
   var e = e ? e : event;
   if(e.button == _this.mouseState && _this.dragId != null)
   {
    var x = e.clientX;
    var y = e.clientY;
    _this.$(moveId).style.left = (_this.$(moveId).offsetLeft + (x - _this.x)) + "px";
    _this.$(moveId).style.top = (_this.$(moveId).offsetTop + (y - _this.y)) + "px";
    _this.x = x;
    _this.y = y;
    //alert(_this.$(dragId).style.left);
   }
  }
 },
 $:function(o){//获取对象
  if(typeof(o) == "string")
  {
   if(document.getElementById(o))
   {
    return document.getElementById(o);
   }
   else
   {
    alert("errId \""+ o + "\"!");
    return false;
   }
  }
  else
  {
   return o;
  }
 }
}
window.onload = function(){
 var c = new dragClass();
 c.dragStar("b","a");
 c.dragStar("d","c");
}
</script>
</head>
<body>
<div class="bodyer">
 <h1 class="t_rt">
  鼠标拖动层封装类
 </h1>
 <h2>
  效果预览
 </h2>
 <div class="preview">
  <div class="scrolldoorFrame">
   <div id="a" style="width:200px;height:150px;background:#FFF;border:1px solid #DDD;">
    <div id="b" style="height:20px;margin:1px;line-height:22px;overflow:hidden;background:#999;font-size:12px;font-weight:bold;color:#FFF;padding-left:15px;">
    拖动层1
    </div>
    &nbsp;&nbsp;&nbsp;&nbsp;移动层
   </div>
   <div id="c" style="width:200px;height:150px;background:#FFF;border:1px solid #DDD;">
    <div id="d" style="height:20px;margin:1px;line-height:22px;overflow:hidden;background:#999;font-size:12px;font-weight:bold;color:#FFF;padding-left:15px;">
    拖动层2
    </div>
    &nbsp;&nbsp;&nbsp;&nbsp;移动层
   </div>
  </div>
 </div>
 <h2>
  源代码
 </h2>
 <div class="textDiv">
  <textarea id="sourse">
function dragClass(){
 this.mouseState = document.all ? 1 : 0 ;
 this.x;
 this.y;
 this.dragId = null;
 this.index = 100;
 document.onselectstart = function(){return false;};
}
dragClass.prototype = {
 dragStar:function(dragId,moveId){//拖动入口函数
  var _this = this;
  _this.$(dragId).onmousedown = function(e){
   var e = e ? e : event;
   if(e.button == _this.mouseState)
   {
    _this.setDragInfo(e,moveId,moveId);
    _this.dragPro(moveId);
   }
  }
  _this.$(dragId).onmouseup = function(){
   _this.clearDragId();
  }
  document.onmouseup = function(){
   _this.clearDragId();
  }
 },
 setDragInfo:function(e,dragId,moveId){//拖动初始化
  this.x = e.clientX;
  this.y = e.clientY;
  this.dragId = dragId;
  if(this.$(moveId).style.position != "absolute")
  {
   this.$(moveId).style.width = this.$(moveId).offsetWidth;
   this.$(moveId).style.height = this.$(moveId).offsetHeight;
   this.$(moveId).style.position = "absolute";
   this.$(moveId).style.left = this.$(moveId).offsetLeft;
   this.$(moveId).style.top = this.$(moveId).offsetTop;
  }
  this.$(moveId).style.zIndex = this.index++;
 },
 clearDragId:function(){ //清除拖动ID
  this.dragId = null;
 },
 dragPro:function(moveId){
  var _this = this;
  document.onmousemove = function(e){
   var e = e ? e : event;
   if(e.button == _this.mouseState && _this.dragId != null)
   {
    var x = e.clientX;
    var y = e.clientY;
    _this.$(moveId).style.left = (_this.$(moveId).offsetLeft + (x - _this.x)) + "px";
    _this.$(moveId).style.top = (_this.$(moveId).offsetTop + (y - _this.y)) + "px";
    _this.x = x;
    _this.y = y;
    //alert(_this.$(dragId).style.left);
   }
  }
 },
 $:function(o){//获取对象
  if(typeof(o) == "string")
  {
   if(document.getElementById(o))
   {
    return document.getElementById(o);
   }
   else
   {
    alert("errId \""+ o + "\"!");
    return false;
   }
  }
  else
  {
   return o;
  }
 }
}
  </textarea>
 </div>
 <h2>
  使用方法
 </h2>
  <div class="preview">
  1.把以上代码引进你的页面 &lt;script type="text/javascript" src="dragClass.js"&gt;&lt;/script&gt;<br/>
  2.在页面的"&lt;/body&gt;"标签前加入以下代码:<br/>
  <div class="example">
   &lt;script type="text/javascript"&gt;<br/>
   window.onload = function(){<br />
 var c = new dragClass();//创建对象<br />
 c.dragStar("拖动id","移动层id");<br />
}<br/>
   &lt;/script&gt;
  </div>
  其中sd方法中的参数为:<br/>
   参数一 "拖动id":鼠标经过时候鼠标样式变成移动样式模样的元素的id<br/>
   参数二 "移动层id":即将移动的元素的id<br/>
  <span class="red">3.建议移动层是绝对定位(position:absolute;).</span>
  </div>
</div>
</body>
</html>

转载于:https://www.cnblogs.com/yuzhongwusan/archive/2008/10/31/1323535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值