鼠标拖动改变DIV等网页元素的大小的最佳实践

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>

时间: 2024-12-08 18:16:06

鼠标拖动改变DIV等网页元素的大小的最佳实践的相关文章

jQuery实现鼠标拖动改变Div高度

最近项目中需要在DashBoard页面做一个事件通知栏,该通知栏固定位于页面底部,鼠标拖动该DIV实现自动改变高度扩展内容显示区域. 以下是一个设计原型,基于jQuery实现,只实现了拖动效果,没有做页面美化,可以根据需求做相应修改. 直接上代码 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quo

jquery 鼠标拖动改变div大小

一个非常简单的例子: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

鼠标拖动改变层的大小

<html><head><title>拖动改变层的大小</title><meta content="text/html; charset=gb2312" http-equiv="Content-Type"><style> {box-sizing: border-box; moz-box-sizing: border-box}#testDiv { background-color: buttonf

HTML中关于鼠标指向改变div显示

利用样式表中 display属性来实现 例如 <html> <title>aaaaa</title> <head> <style> #changing1{ width:980px; height:760px; left:0px; top:50px; position:absolute; background-image:url(images/xs1.png); display:block;}#changing2{ width:980px; hei

拖动鼠标改变div层的大小宽度

<html> <head> <title>拖动鼠标改变div层的大小宽度-石家庄色彩顾问-亿诚</title> <meta content="text/html; charset=gb2312" http-equiv="Content-Type"> <style> { box-sizing: border-box; moz-box-sizing: border-box } #testDiv {

Javascript 简单实现鼠标拖动DIV

http://zhangbo-peipei-163-com.iteye.com/blog/1740078 比较精简的Javascript拖动效果函数代码 http://www.jb51.net/article/10578.htm <html> <head><title>拖动效果函数演示 by Longbill.cn</title> <style> body { font-size:12px; color:#333333; border : 0px

如果将div的可拖动范围限定在指定元素内

如果将div的可拖动范围限定在指定元素内:拖动效果大家可能比较熟悉,但是通常会对拖动范围进行一下限定,通过一个实例代码介绍一下如何将一个div的拖动限定在一个指定元素范围内,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/&qu

禁用网页右键菜单和鼠标拖动选择

一.禁止鼠标右键菜单: 有如下两种方法来禁止鼠标右键 1.在HTML元素的Body标签中加入Javascript事件句柄,代码如下: <body onContextMenu="return false"> 说明:您也可以在网页指定的位置禁止右键,比如您只想在网页中的某个图片上禁右键防下载,或您只想在网页中的一段文字或表格上禁右键,您也只需将如上红色代码加入到相应的HTML标签元素中,例: <img src="logo.gif" width=88 h

前端新技术:HTML5实现网页元素的拖动操作

HTML5之前,要实现网页元素的拖动操作,需要依靠mousedown.mousemove.mouseup等API,通过大量的JS代码来实现:HTML5 中引入了直接支持拖动操作的API,大大简化了网页元素的拖动操作编程难度,并且这些API除了支持浏览器内部元素的拖动外,同时支持浏览器和其它应用程序之间的数据互相拖动. 本文通过一个简单示例,演示HTML5中拖动API的使用方法. 场景: 如下图所示,我们要实现: 通过拖动将照片从左侧"相册"区域拖拽到右侧"垃圾箱"区