时间:2021-05-26
本文实例讲述了JavaScript实现网页对象拖放功能的方法。分享给大家供大家参考。具体如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Drag</title><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><style>#myDiv{width:50px; height:50px; background-color:red}</style></head><body><div id="myDiv"></div></body><script type="text/javascript"> var isIE = document.all ? true : false; //判断是否是IE浏览器 function addEventHandler(oTarget, sEventType, fnHandler){ //添加事件 if(oTarget.addEventListener){ oTarget.addEventListener(sEventType, fnHandler, false); }else if(oTarget.attachEvent){ oTarget.attachEvent("on" + sEventType, fnHandler); }else{ oTarget["on" + 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 BindAsEventListener = function(object, fun) { //以另一个对象替换当前对象 return function(event) { return fun.call(object, (event || window.event)); } } var $ = function(id){ return document.getElementById(id); } var Class = { create: function() { return function() {this.initialize.apply(this, arguments);} } } var drag = Class.create(); drag.prototype = { initialize: function(id){//初始化 this._drag = $(id); this._flag = false; addEventHandler(this._drag,'mousedown',BindAsEventListener(this,this.start)); this._fM = BindAsEventListener(this, this.move); this._fS = BindAsEventListener(this, this.stop); this._drag.style.position = "absolute"; }, start: function(oEvent){//相当于onmousedown事件 //return this._name; this._x = oEvent.clientX - this._drag.offsetLeft; this._y = oEvent.clientY - this._drag.offsetTop; addEventHandler(document, 'mousemove', this._fM); addEventHandler(document, 'mouseup', this._fS); if(isIE){ addEventHandler(this._drag, "losecapture", this._fS); //焦点丢失 this._Handle.setCapture();//设置鼠标捕获 }else{ addEventHandler(window, "blur", this._fS);//焦点丢失 oEvent.preventDefault();//阻止默认动作 } }, move: function(oEvent){ //相当于onmonusemove事件 this._drag.style.left = oEvent.clientX - this._x + "px"; this._drag.style.top = oEvent.clientY - this._y + "px"; }, stop: function(){ //相当于onmouseup事件 removeEventHandler(document, 'mousemove', this._fM); removeEventHandler(document, 'mouseup', this._fS); if(isIE){ removeEventHandler(this._drag, "losecapture", this._fS); this._Handle.releaseCapture(); }else{ removeEventHandler(window, "blur", this._fS); } } } var ndrag = new drag("myDiv");</script></html>希望本文所述对大家的javascript程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Javascript----文件操作一、功能实现核心:FileSystemObject对象要在javascript中实现文件操作功能,主要就是依靠FileSys
本文实例讲述了JavaScript数组,JSON对象实现动态添加、修改、删除功能。分享给大家供大家参考,具体如下:javascript里面的数组,json对象,
本文实例讲述了javascript用函数实现对象的方法。分享给大家供大家参考。具体实现方法如下:利用函数实现对象(重要)functionPerson(name,
本文实例讲述了JavaScript仿静态分页实现方法。分享给大家供大家参考。具体如下:这里基于JavaScript模仿网页不刷新静态分页的功能,实际用的时候估计
本文实例讲述了JS深入学习之数组对象排序功能。分享给大家供大家参考,具体如下:JavaScript实现多维数组、对象数组排序,其实用的就是原生的sort()方法