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 
版权声明:本文为博主原创文章,转载请附上博文链接!
  • 2020-03-21 12:05:24

    android 自定义组件,使用AttributeSet

    首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:

  • 2020-03-21 12:09:59

    Android使用AttributeSet自定义控件的方法

    我们可以在attrs.xml中声明自己控件的属性,在布局xml文档中声明自己的命名空间,这时就可以对设置自己想要的值了,然后在AttributeSet这个属性中获取对应的值。好了不多说,我们来看下代码,一切尽在不言中:

  • 2020-03-22 21:16:07

    用vue做的跟随鼠标移动的div

    最近接到一个任务,就是在既存用electron-vue开发的桌面端程序上追加随鼠标移动的动画效果,之前一直使用angular和react,没怎么接触过vue,先做一个vue的简单例子,然后再整合。

  • 2020-03-23 15:16:30

    import,require具体用法

    import 和 require 是JS模块化编程使用的,是前端开发者们在性能探索中的又一大进步。

  • 2020-03-30 15:37:12

    PM2下使用 npm run 命令

    npm run xxxx 是 node常用的启动方式之一,本文介绍下如何用PM2来实现该方式的启动。 下面是项目的package.json文件部分代码: