简单的Dom拖动Dome
来源:互联网 发布:c语言好不好学 编辑:程序博客网 时间:2024/05/22 09:04
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <title></title> <!-- <script type="text/javascript" src="jquery-1.2.6.js"></script>--> <script type="text/javascript" src="cssJs.js"></script> <!--http://blog.csdn.net/xxb2008/article/details/9137621--></head><style type="text/css"> .content { margin: 0 auto; width: 400px; height: 300px; padding: 10px; margin: 10px; border: 10px solid black; position: relative;} .clientBox{padding: 10px; margin: 10px; cursor: move; width: 10px; overflow: hidden; height: 10px; border: 10px solid red; position: absolute; left: 0px;top:0px;background: white} #msg{position: fixed;right: 0; top: 20px}</style><body><div class="content"> <div class="clientBox" id="clientBox"></div></div><div id="msg"></div></body></html>
<script type="text/javascript"> var Event = { addEventListener:function (target, type, listener) { var _type = "on" + type; if (target.attachEvent) { //ie target.attachEvent(_type, listener); } else if (target.addEventListener) { //ff target.addEventListener(type, listener, false); } else { target[_type] = listener; } }, removeEventListener:function (target, type, listener) { var _type = "on" + type; if (target.detachEvent) { //ie target.detachEvent(_type, listener); } else if (target.removeEventListener) { //ff target.removeEventListener(type, listener, false); } else { target[_type] = null; } }, fix:function (event) { event = window.event || event; var e = { originalEvent:event } for (var i in event) { e[i] = event[i]; } if (!e.pageX) { var doc = document.documentElement, body = document.body; e.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0); e.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0); } e.preventDefault = function () { if (this.originalEvent.preventDefault) this.originalEvent.preventDefault(); this.originalEvent.returnValue = false; }; e.stopPropagation = function () { if (this.originalEvent.stopPropagation) this.originalEvent.stopPropagation(); this.originalEvent.cancelBubble = true; }; return e; }, callEvent : function(obj, func){ return function(event){ return func.call(obj,Event.fix(event)); } }, apply : function(obj, func){ return function() { return func.apply(obj, arguments); } } } var Dom = { $:function (id) { return typeof id === "string" ? document.getElementById(id) : id; } } function Drag(boxId){ this.init(boxId); }; Drag.prototype = { box:null, x:0, y:0, isIE:(document.all) ? true : false, init:function (boxId) { this.box = Dom.$(boxId); this._mouseDown = Event.callEvent(this, this.mouseDown); this._move = Event.callEvent(this, this.move); this._mouseUp = Event.callEvent(this, this.mouseUp); Event.apply(this, this.start)(); }, move:function (event) { event = Event.fix(event); event.preventDefault(); event.stopPropagation(); //清除选择 window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); this.box.style.left = event.pageX - this.x + "px"; this.box.style.top = event.pageY - this.y + "px"; }, mouseDown:function (event) { event = Event.fix(event); event.preventDefault(); event.stopPropagation(); /*box的top,left为0,margin为10,offsetLeft包含了margin,所在要加回来--IE需要再加clientLeft*/ //this.x = event.pageX - this.box.offsetLeft + parseInt(CssJs.get(this.box, "marginLeft")); //this.y = event.pageY - this.box.offsetTop + parseInt(CssJs.get(this.box, "marginTop")); this.x = event.pageX - parseInt(CssJs.get(this.box, "left")); this.y = event.pageY - parseInt(CssJs.get(this.box, "top")); Event.addEventListener(document, "mousemove", this._move); Event.addEventListener(document, "mouseup", this._mouseUp); if (this.isIE) { Event.addEventListener(this.box, "losecapture", this._mouseUp);//焦点丢失 this.box.setCapture(); //设置鼠标捕获 } else { Event.addEventListener(window, "blur", this._mouseUp);//焦点丢失 } }, mouseUp:function (event) { this.stop(); }, stop:function () { if (this.isIE) { Event.removeEventListener(this.box, "losecapture", this._mouseUp);//焦点丢失 this.box.releaseCapture(); //设置鼠标捕获 } else { Event.removeEventListener(window, "blur", this._mouseUp);//焦点丢失 } Event.removeEventListener(document, "mousemove", this._move); Event.removeEventListener(document, "mouseup", this._mouseUp); }, start:function () { Event.addEventListener(this.box, "mousedown", this._mouseDown); } } new Drag("clientBox"); /* box的top,left为0,margin为10,offsetLeft包含了margin,所在要加回来 this.x = event.pageX - this.box.offsetLeft + parseInt(CssJs.get(this.box,"marginLeft")); this.y = event.pageY - this.box.offsetTop + parseInt(CssJs.get(this.box,"marginTop")); */</script>
- 简单的Dom拖动Dome
- 简单的Dom拖动Dome
- 简单的Dom拖动Dome
- 简单的拍照dome
- andriod获取联系人的简单dome
- andriod显示录制视频的简单dome
- Xutils3.0简单的应用Dome
- js对dom的拖动
- 简单联网判断Dome
- AlertDialog 对话框简单Dome
- iOS 搜索框控件 最简单的dome
- 搜索框控件,写了个最简单的dome
- XML使用Pull进行解析的简单Dome
- XML使用Pull进行解析的简单Dome
- 最简单的拖动。
- 简单的jquery拖动
- Jquery的简单拖动
- 拖动的简单实现
- Tomcat与IIS整合手记
- 移动应用用户使用成本控制管理
- NSDateFormatter setDateFormat 自定義日期/時間格式
- Android 使用bundle在activity之间交换数据 DEMO (填写注册信息实例)
- Android定义color资源
- 简单的Dom拖动Dome
- R cannot be resolved to a variable 处理办法
- 一位好的软件工程应该具备什么?
- Shrink space合并表的碎片
- coreseek 增量索引模拟实时索引
- Android电池信息获得问题
- 计算文件的行数.
- EJB3.0 中事务的处理自定义异常类并回滚
- directshow开发中的一个错误