JS实现div宽度、高度拉伸

一、JavaScript实现宽度高度自动缓慢拉伸

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <style type="text/css">
        #testDiv {
            width: 200px;
            height: 200px;
            border: 1px solid red;
        }

        #statusShow {
            width: 200px;
            height: 200px;
            border: 1px solid red;
        }
    </style>
    <script type="text/javascript">
        var addObj;
        var reduceObj;
        var testDiv;
        var divwidth;
        var divheight;
        function addRect() {
            divwidth = testDiv.clientWidth;
            divheight = testDiv.clientHeight;
            if (divwidth <= 500 && divheight <= 500) {
                divwidth += 1;
                divheight += 1;
                testDiv.style.width = divwidth + "px";
                testDiv.style.height = divheight + "px";
            }
        }
        function reduceRect() {
            divwidth = testDiv.clientWidth;
            divheight = testDiv.clientHeight;
            if (divwidth >= 200 && divheight >= 200) {
                divwidth -= 1;
                divheight -= 1;
                testDiv.style.width = divwidth + "px";
                testDiv.style.height = divheight + "px";
            }
        }
        window.onload = function () {
            addObj = document.getElementById("add");
            reduceObj = document.getElementById("reduce");
            testDiv = document.getElementById("testDiv");
            addObj.onclick = function () {
                setInterval(addRect, 1);
            }
            reduceObj.onclick = function () {
                setInterval(reduceRect, 1);
            }
        }
    </script>
</head>

<body>
    <div id="testDiv">
    </div>
    <button id="add">增加</button>
    <button id="reduce">减少</button>
</body>

</html>

二、Jquery实现div宽度拉伸

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>RunJS 演示代码</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        div {
            position: absolute;
            float: left;
            height: 500px;
        }
        div.left {
            background-color: red;
            width: 200px;
            left: 0px;
        }
        div.center {
            background-color: yellow;
            width: 200px;
            right: 205px;
            float: right;
        }
        div.handler {
            width: 5px;
            cursor: col-resize;
            background-color: blue;
            z-index: 1;
            left: 200px;
        }
    </style>
    <script class="jquery library" src="./jquery-2.1.4.min.js" type="text/javascript"></script>
    <script>
        window.onload = function () {
            var doc = $(document), dl = $("div.left"), dc = $("div.center");
            var sum = dl.width() + dc.width();
            $("div.handler").mousedown(function (e) {
                var me = $(this);
                console.log(me);

                var deltaX = e.clientX
                    -
                    (parseFloat(me.css("left")) || parseFloat(me.prop("clientLeft")));
                doc.mousemove(function (e) {
                    var lt = e.clientX - deltaX;
                    lt = lt < 200 ? 200 : lt; //最小宽度200px
                    lt = lt > sum - me.width() ? sum - me.width() : lt;
                    me.css("left", lt + "px");
                    dl.width(lt);
                    dc.width(sum - lt - me.width());
                });
            }).width();
            doc.mouseup(function () {
                doc.unbind("mousemove");
            });
            doc[0].ondragstart
                = doc[0].onselectstart
                = function () {
                    return false;
                };
        }
    </script>
</head>

<body>
    <div class="left" id="left">
        同时在线人数:1000
        点击刷新
    </div>
    <div class="handler" id="handler">
    </div>
    <div class="center" id="center">
        sss
    </div>
</body>

</html>

三、Vue+TS实现div宽度拉伸

<template>
  <div>
    <div class="left" id="left">
      同时在线人数:1000
      点击刷新
    </div>
    <div class="handler" id="handler" @mousedown="drag">
    </div>
    <div class="center" id="center">
      sss
    </div>
  </div>
</template>

<script >
// export default {
//   components: {},
//   computed: {
//   },
//   methods: {
//     drag: function (e, index) {

//       var Left = e.target.parentNode.children[0],
//         leftW = Left.offsetWidth,
//         startX = e.clientX;

//       document.onmousemove = function (e) {
//         e.preventDefault();
//         var distX = e.clientX - startX;
//         Left.style.width = leftW + distX + 'px';
//         handler.style.left = leftW + distX + "px";
//         if (parseInt(Left.style.width) >= 280) {
//           Left.style.width = 280 + 'px';
//           handler.style.left = 280 + "px";
//         }
//       }

//       document.onmouseup = function () {
//         document.onmousemove = null;
//       }
//     }
//   },
//   mounted() { }
// };

</script>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
@Component({
  components: {}
})
export default class Index extends Vue {
    private drag(e: any) {
    // const left = e.target.parentNode.children[0];
    // const handler = e.target.parentNode.children[1];
    const handler = document.getElementById('handler') as HTMLElement;
    const Left = document.getElementById('left') as HTMLElement;
    const center = document.getElementById('center') as HTMLElement;
    const leftW = Left.offsetWidth;
    const startX = e.clientX;


    document.onmousemove = function (e) {
      e.preventDefault();
      const distX = e.clientX - startX;
      
      let moveWidth = leftW + distX;
      moveWidth = moveWidth < 180 ? 180 : moveWidth; //设置最小宽度
      moveWidth = moveWidth > 280 ? 280 : moveWidth; //最大宽度

      Left.style.width = moveWidth + 'px';
      handler.style.left = moveWidth + "px";
      center.style.left = moveWidth + "px";
    }

    document.onmouseup = function () {
      document.onmousemove = null;
    }
  }
}
</script>
 <style>
* {
  margin: 0;
  padding: 0;
}
div {
  position: absolute;
  float: left;
  height: 500px;
}
div.left {
  background-color: red;
  width: 200px;
  left: 0px;
}
div.center {
  background-color: yellow;
  width: 200px;
  right: 205px;
  float: right;
}
div.handler {
  width: 5px;
  cursor: col-resize;
  background-color: blue;
  z-index: 1;
  left: 200px;
}
</style>

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值