完美拖拽+拖动改变Div的宽高+关闭按钮
2016-08-17 17:39
309 查看
/** * Created by Spades-k on 2016/8/17. */ define('h5/js/page/imageMagnification', [ 'jquery' ],function($){ var dragBox=function(rddrag,selector,drag,right,bottom,close){ var oDiv=document.getElementById(rddrag); var oDiv2=document.getElementById(selector); var h2=document.getElementById(drag); var right=document.getElementById(right); var bottom=document.getElementById(bottom); var sClose=document.getElementById(close); var mouseStart={}; var divStart={}; var rightStart={}; var bottomStart={}; //往右拽 right.onmousedown=function(ev) { var oEvent=ev||event; mouseStart.x=oEvent.clientX; mouseStart.y=oEvent.clientY; rightStart.x=right.offsetLeft; if(right.setCapture) { right.onmousemove=doDrag1; right.onmouseup=stopDrag1; right.setCapture(); } else { document.addEventListener("mousemove",doDrag1,true); document.addEventListener("mouseup",stopDrag1,true); } }; function doDrag1(ev) { var oEvent=ev||event; var l=oEvent.clientX-mouseStart.x+rightStart.x; var w=l+oDiv.offsetWidth; if(w<oDiv.offsetWidth) { w=oDiv.offsetWidth; } else if(w>document.documentElement.clientWidth-oDiv2.offsetLeft) { w=document.documentElement.clientWidth-oDiv2.offsetLeft-2; } oDiv2.style.width=w+"px"; } function stopDrag1() { if(right.releaseCapture) { right.onmousemove=null; right.onmouseup=null; right.releaseCapture(); } else { document.removeEventListener("mousemove",doDrag1,true); document.removeEventListener("mouseup",stopDrag1,true); } } //往下拽 bottom.onmousedown=function(ev) { var oEvent=ev||event; mouseStart.x=oEvent.clientX; mouseStart.y=oEvent.clientY; bottomStart.y=bottom.offsetTop; if(bottom.setCapture) { bottom.onmousemove=doDrag2; bottom.onmouseup=stopDrag2; bottom.setCapture(); } else { document.addEventListener("mousemove",doDrag2,true); document.addEventListener("mouseup",stopDrag2,true); } }; function doDrag2(ev) { var oEvent=ev||event; var t=oEvent.clientY-mouseStart.y+bottomStart.y; var h=t+oDiv.offsetHeight; if(h<oDiv.offsetHeight) { h=oDiv.offsetHeight; } else if(h>document.documentElement.clientHeight-oDiv2.offsetTop) { h=document.documentElement.clientHeight-oDiv2.offsetTop-2; } oDiv2.style.height=h+"px"; } function stopDrag2() { if(bottom.releaseCapture) { bottom.onmousemove=null; bottom.onmouseup=null; bottom.releaseCapture(); } else { document.removeEventListener("mousemove",doDrag2,true); document.removeEventListener("mouseup",stopDrag2,true); } } //往左右同时拽 oDiv.onmousedown=function(ev) { var oEvent=ev||event; mouseStart.x=oEvent.clientX; mouseStart.y=oEvent.clientY; divStart.x=oDiv.offsetLeft; divStart.y=oDiv.offsetTop; if(oDiv.setCapture) { oDiv.onmousemove=doDrag; oDiv.onmouseup=stopDrag; oDiv.setCapture(); } else { document.addEventListener("mousemove",doDrag,true); document.addEventListener("mouseup",stopDrag,true); } }; function doDrag(ev) { var oEvent=ev||event; var l=oEvent.clientX-mouseStart.x+divStart.x; var t=oEvent.clientY-mouseStart.y+divStart.y; var w=l+oDiv.offsetWidth; var h=t+oDiv.offsetHeight; if(w<oDiv.offsetWidth) { w=oDiv.offsetWidth; } else if(w>document.documentElement.clientWidth-oDiv2.offsetLeft) { w=document.documentElement.clientWidth-oDiv2.offsetLeft-2; } if(h<oDiv.offsetHeight) { h=oDiv.offsetHeight; } else if(h>document.documentElement.clientHeight-oDiv2.offsetTop) { h=document.documentElement.clientHeight-oDiv2.offsetTop-2; } oDiv2.style.width=w+"px"; oDiv2.style.height=h+"px"; } function stopDrag() { if(oDiv.releaseCapture) { oDiv.onmousemove=null; oDiv.onmouseup=null; oDiv.releaseCapture(); } else { document.removeEventListener("mousemove",doDrag,true); document.removeEventListener("mouseup",stopDrag,true); } } //h2完美拖拽 h2.onmousedown=function(ev) { var oEvent=ev||event; mouseStart.x=oEvent.clientX; mouseStart.y=oEvent.clientY; divStart.x=oDiv2.offsetLeft; divStart.y=oDiv2.offsetTop; if(h2.setCapture) { h2.onmousemove=doDrag3; h2.onmouseup=stopDrag3; h2.setCapture(); } else { document.addEventListener("mousemove",doDrag3,true); document.addEventListener("mouseup",stopDrag3,true); } }; function doDrag3(ev) { var oEvent=ev||event; var l=oEvent.clientX-mouseStart.x+divStart.x; var t=oEvent.clientY-mouseStart.y+divStart.y; if(l<0) { l=0; } else if(l>document.documentElement.clientWidth-oDiv2.offsetWidth) { l=document.documentElement.clientWidth-oDiv2.offsetWidth; } if(t<0) { t=0; } else if(t>document.documentElement.clientHeight-oDiv2.offsetHeight) { t=document.documentElement.clientHeight-oDiv2.offsetHeight; } oDiv2.style.left=l+"px"; oDiv2.style.top=t+"px"; } function stopDrag3() { if(h2.releaseCapture) { h2.onmousemove=null; h2.onmouseup=null; h2.releaseCapture(); } else { document.removeEventListener("mousemove",doDrag3,true); document.removeEventListener("mouseup",stopDrag3,true); } } //关闭 sClose.onmousedown=function (ev){(ev||event).cancelBubble=true;} sClose.onclick=function() { oDiv2.style.display="none"; } } return {dragBox:dragBox}; })
dragBox('div1','box','h2','right','',''); 调用
相关文章推荐
- 完美拖拽+拖动改变Div的宽高+关闭按钮[带演示](转)
- 完美拖拽+拖动改变Div的宽高+关闭按钮[带演示]
- 可拖动、可关闭、透明标题的DIV弹出窗口,完美兼容各个浏览器
- 漂亮的Js拖动层,窗口拖拽(改变大小/最小化/最大化/还原/关闭)
- 【完美解决方案】控制div拖拽范围,让层在范围内拖动,不拖拽出边界值
- 兼容ie ff div 层 打开+关闭+ 拖动+遮罩+移动+动画改变高宽
- 兼容ie ff div 层 打开+关闭+ 拖动+遮罩+移动+动画改变高宽
- 鼠标拖动改变div高度
- 可以拖动排版可以关闭的DIV层
- Jquery实现鼠标拖动改变div高度
- js通过八个点 拖动改变div大小
- jquery拖拽改变div大小效果
- jQuery弹出Div窗口居中显示、滚动跟随、关闭按钮以及几秒后自动消失
- iPhone手机解锁效果&&自定义滚动条&&拖拽--Clone&&窗口拖拽(改变大小/最小化/最大化/还原/关闭)
- div弹出层的效果带关闭按钮
- 源代码:用层DIV模拟的类似C/S模式下的小窗口(可以拖动,展开关闭)
- c# 控制台程序禁用关闭按钮完美解决
- 点击单选按钮改变div显示隐藏
- 有关div页面拖动、缩放、关闭、遮罩效果代码
- js完美的div拖拽实例代码