zoukankan      html  css  js  c++  java
  • 原生js实现拖拽效果

    面向对象 + 原生js拖拽

    拖拽div等盒子模型,都是日常操作没有什么问题,如果是拖拽图片的话,会有一点小坑要踩......

    那么我们看代码:

    ``` var Move_fn = {}; (function(Move_fn){ function Move_img() {
    }
    Move_img.prototype = {
    	constructor:Move_img,
    	pageInit: function(imgEle, imgContent) {
    		this.Ele = imgEle;
    		this.Box = imgContent;
                        imgEle.className = "_positon";//添加定位属性便于计算拖拽位置
    		this._mw = imgContent.offsetWidth - imgEle.offsetWidth;
    		this._mh = imgContent.offsetHeight - imgEle.offsetHeight;
    		this.mouseDown();
    		this.closeEvt();
    	},
    	closeEvt:function() {
    		var that = this;
    		this.Box.onclick = function(e) {
    			e.preventDefault();
    			e.stopPropagation();
    			if(e.target.tagName == "DIV" || e.srcElement.tagName == "div") {
    				Elf.utils.remove(that.Box.parentNode, that.Box.parentNode.parentNode);
    			}
    		}
    	},
    	mouseDown: function() {
    		var that = this;
    		this.Ele.onmousedown = function(e) {
    			that.offX = e.offsetX;
    			that.offY = e.offsetY;
    			that.mouseMove();
    		}
    	},
    	mouseMove: function(){
    		var that = this;
    		document.onmousemove = function(e) {
    			var l = e.clientX - that.offX;
    			var t = e.clientY - that.offY;
    			//判断边界设置最大最小值
    			if(t <= 0) {
    				t = 0;
    			}
    			if(t >= that._mh) {
    				t = that._mh;
    			}
    			if(l <= 0) {
    				l = 0;
    			}
    			if(l >= that._mw) {
    				l = that._mw;
    			}
    			that.Ele.style.top = t + "px";
    			that.Ele.style.left = l + "px";
    			that.mouseUp();
    		}
    	},
    	mouseUp: function() {
    		var that = this;
    		document.onmouseup = function(e) {
    			document.onmousemove = null;
    			document.onmousedown = null;
    		}
    	}
    }
    Move_fn.move_img = new Move_img();
    

    }(Move_fn));

    <p>使用方式也横简单,Move_fn.move_img.pageInit(imgShow, imgContent);初始化一下就好了。要求imgContent全屏遮盖</p>
    <p>现在来说一下,图片拖拽的小坑。当鼠标移动到图片上的时候,会有一个,图片选中可拖拽的状态,这个时候我们执行的是ondragstart、draggable事件,而不是自行添加的onmousemove事件。会造成的后果是什么呢?拖拽后图片卡顿,自行添加的鼠标抬起事件onmouseup失效,当我们的鼠标抬起后依然会执行鼠标移动事件,即鼠标抬起后图片会跟着鼠标跑</p>
    ![](https://images2018.cnblogs.com/blog/1244681/201809/1244681-20180903161715077-1922354997.png)
    
    <p>解决办法:禁止掉图片自己的拖拽事件<br />
    对要拖拽的图片添加几个属性
    <ul>
    	<li>oncontextmenu:"false"&nbsp;&nbsp;&nbsp;&nbsp;<span>禁止图片右键菜单弹出</span></li>
    	<li>onselectstart:"false"&nbsp;&nbsp;&nbsp;&nbsp;<span>禁止图片选中</span></li>
    	<li>ondragstart:"false"&nbsp;&nbsp;&nbsp;&nbsp;<span>禁止图片拖拽</span></li>
    	<li>draggable:"false"&nbsp;&nbsp;&nbsp;&nbsp;<span>禁止图片拖拽</span></li>
    </ul>
    </p>
  • 相关阅读:
    [转]Git忽略规则及.gitignore规则不生效的解决办法
    动漫(杂)
    《计算机图形学》2.5 ~ 2.7 学习笔记
    《计算机图形学》2.4 输入设备 学习笔记
    《计算机图形学》2.2.2 光栅扫描显示处理器
    Android 使用版本控制工具时添加忽略文件方式
    devexpress表格控件gridcontrol设置隔行变色、焦点行颜色、设置(改变)显示值、固定列不移动(附源码)
    详解shape标签
    以向VS 程序打包集成自动写入注册表功能为例,介绍如何实现自由控制安装过程
    C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有
  • 原文地址:https://www.cnblogs.com/frogblog/p/9579146.html
Copyright © 2011-2022 走看看