js实现div上下左右拉伸

2018-10-26 08:30:01

js实现div上下左右拉伸

<!doctype html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>div的拉伸</title>
    <style type="text/css">
        *{padding: 0;margin: 0;}
        body{padding: 100px;}
        #box{position: absolute;width: 150px;height: 150px;background: orangered;border: 10px solid lightcoral;}
    </style>
</head>
<body>
    <div id="box"></div>
</body>
<script type="text/javascript">
    /*
     * 移动得距离就为点击位置坐标(clientX) - 移动后的位置坐标(clientX),那么现在盒子总共的宽度就是其本身宽度(oBox.offsetWidth)加上前面坐标之差。向左拉伸原理差不多,就是多加个改变盒子的位置,盒子的offsetLeft等于光标移动后的位置坐标。我们对盒子就行绝对定位,获取它的left值,将它left值减去改变的距离,他就会向左边拉伸了。上下同理
     */

    var oBox = document.getElementById('box');
    oBox.onmousedown = function(e){
        e = e ||event;
        var x = e.clientX;
        var y = e.clientY;
        var oBoxL = oBox.offsetLeft;
        var oBoxT = oBox.offsetTop;
        var oBoxW = oBox.offsetWidth;
        var oBoxH = oBox.offsetHeight;

        var d = 0;
        if(x < oBoxL + 10){
            d = 'left';
        }
        else if(x > oBoxL + oBoxW -10){
            d = 'right';
        }

        if(y < oBoxT + 10){
            d = 'top';
        }
        else if(d < oBoxT + oBoxH -10){
            d = 'bottom';
        }
        if(x < oBoxL + 10 && y < oBoxT + 10){
            d ='LT';
        }
        document.onmousemove = function(e){
            e = e ||event;
            var xx = e.clientX;
            var yy = e.clientY;
            if(d == 'left'){
                oBox.style.width = oBoxW + x -xx + 'px'
                oBox.style.left = xx  + 'px';
            }
            else if(d == 'right'){
                oBox.style.width = oBoxW + xx -x + 'px'
            }

            if(d == 'top'){
                oBox.style.height = oBoxH + y - yy + 'px';
                oBox.style.top = yy + 'px';
            }
            else if(d == 'bottom'){
                oBox.style.height = oBoxH + yy - y + 'px';
            }
            if(d == 'LT'){
                oBox.style.width = oBoxW + x -xx + 'px'
                oBox.style.left = xx  + 'px';
                oBox.style.height = oBoxH + y - yy + 'px';
                oBox.style.top = yy + 'px';
            }
            return false;
        }
        document.onmouseup = function(){
            document.onmousemove = null;
            document.onmouseup = null;
        }
        if(e.preventDefault){
            e.preventDefault();
        }
    }
</script>
</html>
--------------------- 
作者:sweetllh 
来源:CSDN 
原文:https://blog.csdn.net/sweetllh/article/details/71367818 
版权声明:本文为博主原创文章,转载请附上博文链接!
  • 2017-04-12 11:22:43

    WebView你真的熟悉吗?看了才知道

    在Android手机中内置了一款高性能webkit内核浏览器,在SDK中封装为一个叫做WebView组件。下面总结一下使用webview遇到的那些事、那些坑。

  • 2017-04-12 11:28:14

    史上最全webview详解

    WebView在现在的项目中使用的频率应该还是非常高的。 我个人总觉得HTML5是一种趋势。找了一些东西,在此总结。 本篇最后有一个非常不错 的 Html5Activity 加载类,不想看的可以直接跳下载。

  • 2017-04-12 23:11:59

    Android Studio签名打包的两种方式

    给我们自己开发的app签名,就代表着我自己的版权,以后要进行升级,也必须要使用相同的签名才行。签名就代表着自己的身份(即keystore),多个app可以使用同一个签名。

  • 2017-04-13 10:57:08

    webstorm 怎么关闭jshint

    You need to configure the inspections in Settings/Editor/Inspections, then in the list on the right, find JavaScript/JavaScript validity issues. That worked for me to remove the Chai warnings. (I am using the WebStorm 11 EAP at the moment.)

  • 2017-04-13 14:11:14

    MySQL中的describe关键字

    今天写代码的时候,有一个类的数据始终不能插入,老是提示在You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'describe, picPath1, picPath2, picPath3, picPath4, picPath5, agentId, belongStore' at line 1。因为用的是SSH进行插入,所以SQL语句不用自己写,检查了每个字段也没写错。

  • 2017-04-15 13:34:08

    Android官方技术文档翻译——清单合并

    一般情况下,有三种类型的清单文件需要合并成一个最终的应用程序清单,这里按照优先级顺序列出: Product flavors 和构建类型所指定的清单文件。 应用程序的主清单文件。 类库的清单文件。

  • 2017-04-15 21:47:44

    Android开发笔记——圆角和边框们

    在做Android界面开发时,我们往往希望它尽可能优美,尽可能显得专业。于是你看了看其他应用,哇,好多边框和圆角啊。你是不是也想给自己的应用加上边框和圆角效果?呃……那怎么做呢?如果你是从web前端跑到Android来的,那么我想你一定想到了不下三种解决方案。如用图片替代,用CSS3定义,用JS画。在Android中,其实也有类似的用法,本文将简单介绍两种Android圆角和边框的实现。