欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

JavaScript實現(xiàn)矩形塊大小任意縮放-創(chuàng)新互聯(lián)

最近寫了一個原生JavaScript實現(xiàn)矩形塊大小任意縮放的案例,感覺里面的東西比較的繞,,里分享源碼給大家,一起學(xué)習(xí)一下。

創(chuàng)新互聯(lián)公司專注于下城企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),成都商城網(wǎng)站開發(fā)。下城網(wǎng)站建設(shè)公司,為下城等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>原生JavaScript實現(xiàn)矩形塊大小任意縮放</title>
</head>
<style>
  * {
    margin: 0;
    padding: 0;
  }

  .box {
    position: relative;
    top: 200px;
    left: 500px;
    width: 500px;
    height: 300px;
    background-color: blueviolet;
  }

  .box>div:nth-child(-n+4) {
    position: absolute;
    width: 5px;
    height: 5px;
    background-color: #cc00ff;
  }

  .box>div:nth-child(n+5) {
    position: absolute;
    background-color: #f30497;
  }

  .box>.dot1 {
    cursor: nw-resize;
    top: -5px;
    left: -5px;
  }

  .box>.dot2 {
    cursor: ne-resize;
    top: -5px;
    right: -5px;
  }

  .box>.dot3 {
    cursor: se-resize;
    bottom: -5px;
    right: -5px;
  }

  .box>.dot4 {
    cursor: sw-resize;
    bottom: -5px;
    left: -5px;
  }

  .box>.line1,
  .box>.line3 {
    width: 500px;
    height: 5px;
  }

  .box>.line1 {
    top: -5px;
    cursor: n-resize;
  }

  .box>.line3 {
    bottom: -5px;
    cursor: s-resize;
  }

  .box>.line2,
  .box>.line4 {
    width: 5px;
    height: 300px;
  }

  .box>.line2 {
    right: -5px;
    cursor: e-resize;
  }

  .box>.line4 {
    left: -5px;
    cursor: e-resize;
  }
</style>

<body>
  <div class="box">
    <div class="dot1"></div>
    <div class="dot2"></div>
    <div class="dot3"></div>
    <div class="dot4"></div>
    <div class="line1"></div>
    <div class="line2"></div>
    <div class="line3"></div>
    <div class="line4"></div>
  </div>
  <script>
    //獲取相關(guān)元素節(jié)點
    let box = document.querySelector(".box");
    let dots = document.querySelectorAll(".box>div:nth-child(-n+4)");
    let lines = document.querySelectorAll(".box>div:nth-child(n+5)");
    // 類邊框?qū)挾?    let bd = 5;
    //for循環(huán)指定事件對象
    function changeBox([box, dots, lines, bd]) {
      for (let i = 0; i < 4; i++) {
        let { 0: dot1, 1: dot2, 2: dot3, 3: dot4 } = dots;    //dots對象解構(gòu)
        let { 0: line1, 1: line2, 2: line3, 3: line4 } = lines;    //lines對象解構(gòu)
        //矩形頂點點擊拖動調(diào)整大小
        dots[i].onmousedown = function (ev) {
          //點擊事件初始化相關(guān)值
          let oldY = ev.clientY;   //頂點原本的y值
          let oldX = ev.clientX;   //頂點原本的x值
          let h = box.clientHeight;  //box的高度
          let w = box.clientWidth;  //box的寬度
          let t = box.offsetTop;   //box距離頂部的top值
          let l = box.offsetLeft;   //box距離左邊的left值
          window.onmousemove = function (e) {
            let offsetY = e.clientY - oldY;     //鼠標移動y軸偏移量
            let offsetX = e.clientX - oldX;     //鼠標移動x軸偏移量
            //if條件是判斷鼠標點擊的節(jié)點目標對象
            if (i == 0) {
              box.style.height = `${h - offsetY + bd}px`;   //box高度變化
              box.style.top = `${t + offsetY - bd}px`;    //box的top值變化(定位)
              box.style.width = `${w - offsetX + bd}px`;   //box的寬度變化
              box.style.left = `${l + offsetX - bd}px`;    //box的left值變化(定位)
            } else if (i == 1) {
              box.style.height = `${h - offsetY + bd}px`;
              box.style.top = `${t + offsetY - bd}px`;
              box.style.width = `${w + offsetX + bd}px`;
            } else if (i == 2) {
              box.style.height = `${h + offsetY + bd}px`;
              box.style.width = `${w + offsetX + bd}px`;
            } else if (i == 3) {
              box.style.height = `${h + offsetY + bd}px`;
              box.style.width = `${w - offsetX + bd}px`;
              box.style.left = `${l + offsetX - bd}px`;
            }
            //邊框線的高度/寬度隨著box的大小變化而變化
            line1.style.width = box.style.width;
            line2.style.height = box.style.height;
            line3.style.width = box.style.width;
            line4.style.height = box.style.height;
          }
        }
        //邊框點擊拖動調(diào)整大小
        lines[i].onmousedown = function (ev) {
          //點擊事件初始化相關(guān)值
          let oldY = ev.clientY;     //頂點原本的y值     
          let oldX = ev.clientX;     //頂點原本的x值
          let h = box.clientHeight;    //box的高度
          let w = box.clientWidth;    //box的寬度
          let t = box.offsetTop;     //box距離頂部的top值
          let l = box.offsetLeft;     //box距離左邊的left值
          window.onmousemove = function (e) {
            let offsetX = e.clientX - oldX;   //鼠標移動x軸偏移量
            let offsetY = e.clientY - oldY;   //鼠標移動y軸偏移量
            //if條件是判斷鼠標點擊的節(jié)點目標對象
            if (i == 0) {
              box.style.height = `${h - offsetY + bd}px`;
              box.style.top = `${t + offsetY - bd}px`;
            } else if (i == 1) {
              box.style.width = `${w + offsetX + bd}px`;
            } else if (i == 2) {
              box.style.height = `${h + offsetY + bd}px`;
            } else {
              box.style.width = `${w - offsetX + bd}px`;
              box.style.left = `${l + offsetX - bd}px`;
            }
            //邊框線的高度/寬度隨著box的大小變化而變化
            line1.style.width = box.style.width;
            line2.style.height = box.style.height;
            line3.style.width = box.style.width;
            line4.style.height = box.style.height;
          }
        }
        //鼠標抬起后清除鼠標移動事件
        window.onmouseup = function () {
          window.onmousemove = false;
        }
      }
    }
    changeBox([box, dots, lines, bd])
  </script>
</body>

</html>

本文名稱:JavaScript實現(xiàn)矩形塊大小任意縮放-創(chuàng)新互聯(lián)
本文URL:http://www.chinadenli.net/article30/dheiso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司電子商務(wù)ChatGPT微信公眾號定制網(wǎng)站動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站