JavaScript实现网页对象拖放功能的方法

 更新时间:2015年04月15日 10:47:00   作者:gogo  
这篇文章主要介绍了JavaScript实现网页对象拖放功能的方法,涉及javascript针对浏览器的判断、事件爱你的添加与移除等相关操作技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • javascript对象的创建和访问

    javascript对象的创建和访问

    这篇文章主要为大家详细介绍了javascript对象的创建和访问实现方法,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • 使用webpack构建应用的方法步骤

    使用webpack构建应用的方法步骤

    这篇文章主要介绍了使用webpack构建应用的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • javascript iframe中打开文件,并检测iframe存在否

    javascript iframe中打开文件,并检测iframe存在否

    从iframe中打开文件,并检测iframe存在否如果说只是检测页面存在否,直接设置target用伪协议就可以解决了...
    2008-12-12
  • js点击页面其它地方将某个显示的DIV隐藏

    js点击页面其它地方将某个显示的DIV隐藏

    今天一朋友问我 点击一按钮弹出一个DIV,然后要求点击页面其它地方隐藏这个DIV
    2012-07-07
  • JavaScript无提示关闭窗口(兼容IE/Firefox/Chrome)

    JavaScript无提示关闭窗口(兼容IE/Firefox/Chrome)

    JavaScript无提示关闭当前页面窗口,兼容IE/Firefox/Chrome (Close the current page window without confirm by JavaScript, support all browsers)
    2008-11-11
  • ES6教程之for循环和Map,Set用法分析

    ES6教程之for循环和Map,Set用法分析

    这篇文章主要介绍了ES6教程之for循环和Map,Set用法,结合实例形式分析了ECMAScript6中for循环和Map,Set基本概念、功能、使用方法与相关注意事项,需要的朋友可以参考下
    2017-04-04
  • 详解js中的几种常用设计模式

    详解js中的几种常用设计模式

    这篇文章主要介绍了js中的几种设计模式,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • iframe与主框架跨域相互访问实现方法

    iframe与主框架跨域相互访问实现方法

    今天正好需要用到iframe 与主框架相互访问的实现方法,正好看到了这篇文章,确实不错,特分享一下,需要的朋友可以参考下
    2017-09-09
  • js实现图片粘贴到网页

    js实现图片粘贴到网页

    这篇文章主要为大家详细介绍了js实现图片粘贴到网页,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • JS实现的页面自定义滚动条效果

    JS实现的页面自定义滚动条效果

    这篇文章主要介绍了JS实现的页面自定义滚动条效果,涉及JavaScript结合css设置页面滚动条样式的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10

最新评论