鼠标拖动改变DIV等网页元素的大小的实现方法
程序员文章站
2024-01-30 15:14:10
1.初次实现
1.1 html代码
1.初次实现
1.1 html代码
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>div change width by drag</title> <script src="../jquery/jquery-1.8.3.min.js" type="text/javascript"></script> </head> <body> <h1>div change width by drag</h1> <div id="pos" style="color:red"></div> <div id="mydiv" style="border:2px solid red;width:300px;height:50px;margin-left: 100px;margin-top: 20px"></div> </body> </html>
1.2 js代码
var eleleft = $('#mydiv').offset().left; var ismousedown = false; var borderlen = 4; //左右边框 $('#mydiv').bind({ mousedown:function(e){ var ele = $(this); var rightpos = eleleft + ele.width() + borderlen; if(rightpos-5 <= e.pagex && e.pagex <= rightpos){ ismousedown = true; } }, mousemove:function(e){ var ele = $(this); var rightpos = eleleft + ele.width() + borderlen; $('#pos').text("x:"+e.pagex + " eleleft:"+eleleft+" rightpos:"+rightpos); if(rightpos-5 <= e.pagex && e.pagex <= rightpos){ ele.css('cursor','e-resize'); }else{ if(!ismousedown){ ele.css('cursor','auto'); } } if(ismousedown){ ele.width((e.pagex-eleleft-borderlen)+'px'); //新鼠标位置-div距左-borderlen } }, mouseup:function(e){ ismousedown = false; } });
1.3 结果
只能往左拖动使div宽度变小,往右拖动没有用!原因往右拖动鼠标mousemove事件无法被div捕获了。拖动时也很难停下来!所以得改进。
2.再次改进
$('#mydiv').bind({ mousedown:function(e){ var ele = $(this); var rightpos = eleleft + ele.width() + borderlen; if(rightpos-5 <= e.pagex && e.pagex <= rightpos){ ismousedown = true; } } }); $('body').bind({ mousemove:function(e){ var ele = $('#mydiv'); var rightpos = eleleft + ele.width() + borderlen; $('#pos').text("x:"+e.pagex + " eleleft:"+eleleft+" rightpos:"+rightpos); if(rightpos-5 <= e.pagex && e.pagex <= rightpos){ ele.css('cursor','e-resize'); }else{ if(!ismousedown){ ele.css('cursor','auto'); } } if(ismousedown){ ele.width((e.pagex-eleleft-borderlen)+'px'); } }, mouseup:function(e){ ismousedown = false; } });
这次解决了上述问题,可以往右拖,并且随时可以停下来了。到这里就完成了吗?no!
当我引入一个其他div,并且阻止mouseup事件冒泡情况怎么样呢?答案是,拖动到这个其它div上放开鼠标后无法停止下来!
<div id="otherdiv" style="border: 2px solid blue;width: 200px;height: 200px;margin-left: 400px"></div> $('#otherdiv').mouseup(function(e){ //e.preventdefault(); //阻止默认行为 e.stoppropagation(); //阻止事件冒泡(导致body捕获不到mouseup事件) });
3.完美解决
拖动停止可能受到其它元素的干扰,怎么解决?想到一些弹出层点击其它其它地方隐藏的功能,让我想到了,加一个遮罩层,让mouseup事件总是可以响应,不就搞定了嘛!
$('#mydiv').bind({ mousedown:function(e){ var ele = $(this); var rightpos = eleleft + ele.width() + borderlen; if(rightpos-5 <= e.pagex && e.pagex <= rightpos){ ismousedown = true; //创建遮罩层,防止mouseup事件被其它元素阻止冒泡,导致mouseup事件无法被body捕获,导致拖动不能停止 var bodywidth = $('body').width(); var bodyheight = $('body').height(); $('body').append('<div id="mask" style="opacity:0.2;top:0px;left:0px;background-color:green;position:absolute;z-index:9999;width:'+bodywidth+'px;height:'+bodyheight+'px;"></div>'); } } }); $('body').bind({ mousemove:function(e){ var ele = $('#mydiv'); var rightpos = eleleft + ele.width() + borderlen; $('#pos').text("x:"+e.pagex + " eleleft:"+eleleft+" rightpos:"+rightpos); if(rightpos-5 <= e.pagex && e.pagex <= rightpos){ ele.css('cursor','e-resize'); }else{ if(!ismousedown){ ele.css('cursor','auto'); } } if(ismousedown){ ele.width((e.pagex-eleleft-borderlen)+'px'); } }, mouseup:function(e){ ismousedown = false; $('#mask').remove(); } }); $('#otherdiv').mouseup(function(e){ //e.preventdefault(); //阻止默认行为 e.stoppropagation(); //阻止事件冒泡(导致body捕获不到mouseup事件) });
4.完整代码和最终效果
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>div change width by drag</title> <script src="../jquery/jquery-1.8.3.min.js" type="text/javascript"></script> </head> <body> <h1>div change width by drag</h1> <div id="pos" style="color:red"></div> <div id="mydiv" style="border:2px solid red;width:300px;height:50px;margin-left: 100px;margin-top: 20px"></div> <div id="otherdiv" style="border: 2px solid blue;width: 200px;height: 200px;margin-left: 400px"></div> </body> <script type="text/javascript"> $(document).ready(function(){ var eleleft = $('#mydiv').offset().left; var ismousedown = false; var borderlen = 4; //左右边框 $('#mydiv').bind({ mousedown:function(e){ var ele = $(this); var rightpos = eleleft + ele.width() + borderlen; if(rightpos-5 <= e.pagex && e.pagex <= rightpos){ ismousedown = true; //创建遮罩层,防止mouseup事件被其它元素阻止冒泡,导致mouseup事件无法被body捕获,导致拖动不能停止 var bodywidth = $('body').width(); var bodyheight = $('body').height(); $('body').append('<div id="mask" style="opacity:0.2;top:0px;left:0px;background-color:green;position:absolute;z-index:9999;width:'+bodywidth+'px;height:'+bodyheight+'px;"></div>'); } } }); $('body').bind({ mousemove:function(e){ var ele = $('#mydiv'); var rightpos = eleleft + ele.width() + borderlen; $('#pos').text("x:"+e.pagex + " eleleft:"+eleleft+" rightpos:"+rightpos); if(rightpos-5 <= e.pagex && e.pagex <= rightpos){ ele.css('cursor','e-resize'); }else{ if(!ismousedown){ ele.css('cursor','auto'); } } if(ismousedown){ ele.width((e.pagex-eleleft-borderlen)+'px'); } }, mouseup:function(e){ ismousedown = false; $('#mask').remove(); } }); $('#otherdiv').mouseup(function(e){ //e.preventdefault(); //阻止默认行为 e.stoppropagation(); //阻止事件冒泡(导致body捕获不到mouseup事件) }); }); </script> </html>
以上这篇鼠标拖动改变div等网页元素的大小的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: Javascript 一些需要注意的细节(必看篇)
下一篇: SparkSQL
推荐阅读
-
鼠标拖动改变DIV等网页元素的大小的实现方法
-
jQuery实现的鼠标拖动浮层功能示例【拖动div等任何标签】
-
jQuery实现鼠标拖动div改变位置、大小的实践
-
C#实现Winform鼠标拖动窗口大小时设定窗口最小尺寸的方法
-
JQuery获取当前元素的上一个/下一个兄弟级元素,字符串截取,转换大小写等元素等实现方法
-
鼠标拖动改变DIV等网页元素的大小的实现方法
-
javascript实现鼠标拖动改变层大小的方法_javascript技巧
-
jQuery实现的鼠标拖动浮层功能示例【拖动div等任何标签】
-
javascript实现鼠标拖动改变层大小的方法_javascript技巧
-
jquery插件jquery.dragscale.js实现拖拽改变元素大小的方法(附demo源码下载)_jquery