欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS实现的简易拖放效果示例

程序员文章站 2022-05-21 08:41:48
本文实例讲述了js实现的简易拖放效果。分享给大家供大家参考,具体如下:

本文实例讲述了js实现的简易拖放效果。分享给大家供大家参考,具体如下:

<!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>简易拖放效果</title>
</head>
<body>
<script type="text/javascript">
<!--
var isie = (document.all) ? true : false;// 是否ie
var $ = function (id) {
  //返回id对象
  return "string" == typeof id ? document.getelementbyid(id) : id;
};
var class = {
  // 带构造方法的类
  create: function() {
    return function() { this.initialize.apply(this, arguments); } //用apply应用下面的initialize初始化属性
  }
}
var extend = function(destination, source) {
  //继承
  for (var property in source) {
    destination[property] = source[property];
  }
}
var bind = function(object, fun) {
  return function() {
    return fun.apply(object, arguments);
  }
}
/**
用object对象调用fun参数是event||window event
**/
var bindaseventlistener = function(object, fun) {
  return function(event) {
    return fun.call(object, (event || window.event));
  }
}
/**
添加帧听器事件
@otarget 目标对象
@seventtype 事件类型
@fnhandler 目标触发事件对象
**/
function addeventhandler(otarget, seventtype, fnhandler) {
  if (otarget.addeventlistener) {
    otarget.addeventlistener(seventtype, fnhandler, false);//firefox
  } else if (otarget.attachevent) {
    otarget.attachevent("on" + seventtype, fnhandler);//ie
  } else {
    otarget["on" + seventtype] = fnhandler;//other
  }
};
/**
清除添加的帧听器事件
@otarget 目标对象
@seventtype 事件类型
@fnhandler 目标触发事件对象
**/
function removeeventhandler(otarget, seventtype, fnhandler) {
  if (otarget.removeeventlistener) {
    otarget.removeeventlistener(seventtype, fnhandler, false);
  } else if (otarget.detachevent) {
    otarget.detachevent("on" + seventtype, fnhandler);
  } else {
    otarget["on" + seventtype] = null;
  }
};
/*------------------------------相关模型构造完毕-----------------------------------*/
//拖放程序
var simpledrag = class.create();
simpledrag.prototype = {
 //拖放对象,触发对象,初始化
 initialize: function(drag) {
  this.drag = $(drag);
  this._x = this._y = 0;
  this._fm = bindaseventlistener(this, this.move);//_fm方法就是应用move方法参数是event||window.event
  this._fs = bind(this, this.stop);//_fs类方法就是stop方法
  this.drag.style.position = "absolute";
  this.drag.style.cursor = "move";
  addeventhandler(this.drag, "mousedown", bindaseventlistener(this, this.start));
 },
 //准备拖动
 start: function(oevent) {
  this._x = oevent.clientx - this.drag.offsetleft;
  this._y = oevent.clienty - this.drag.offsettop;
  addeventhandler(document, "mousemove", this._fm);
  addeventhandler(document, "mouseup", this._fs);
 },
 //拖动
 move: function(oevent) {
  this.drag.style.left = oevent.clientx - this._x + "px";
  this.drag.style.top = oevent.clienty - this._y + "px";
 },
 //停止拖动
 stop: function() {
  removeeventhandler(document, "mousemove", this._fm);
  removeeventhandler(document, "mouseup", this._fs);
 }
};
// -->
</script>
<div id="iddrag" style="border:5px solid #0000ff; background:#c4e3fd; width:50px; height:50px;"></div>
<script type="text/javascript">
<!--
new simpledrag("iddrag");
// -->
</script>
</body>
</html>

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript切换特效与技巧总结》、《javascript查找算法技巧总结》、《javascript动画特效与技巧汇总》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》、《javascript遍历算法与技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。