欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)矩形塊大小任意縮放

 更新時(shí)間:2020年08月25日 12:31:38   作者:布湫  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)矩形塊大小任意縮放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>原生JavaScript實(shí)現(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é)點(diǎn)
  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)");
  // 類(lèi)邊框?qū)挾?
  let bd = 5;
  //for循環(huán)指定事件對(duì)象
  function changeBox([box, dots, lines, bd]) {
   for (let i = 0; i < 4; i++) {
    let { 0: dot1, 1: dot2, 2: dot3, 3: dot4 } = dots;  //dots對(duì)象解構(gòu)
    let { 0: line1, 1: line2, 2: line3, 3: line4 } = lines;  //lines對(duì)象解構(gòu)
    //矩形頂點(diǎn)點(diǎn)擊拖動(dòng)調(diào)整大小
    dots[i].onmousedown = function (ev) {
     //點(diǎn)擊事件初始化相關(guān)值
     let oldY = ev.clientY;  //頂點(diǎn)原本的y值
     let oldX = ev.clientX;  //頂點(diǎn)原本的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;   //鼠標(biāo)移動(dòng)y軸偏移量
      let offsetX = e.clientX - oldX;   //鼠標(biāo)移動(dòng)x軸偏移量
      //if條件是判斷鼠標(biāo)點(diǎn)擊的節(jié)點(diǎn)目標(biāo)對(duì)象
      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`;
      }
      //邊框線(xiàn)的高度/寬度隨著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ǎn)擊拖動(dòng)調(diào)整大小
    lines[i].onmousedown = function (ev) {
     //點(diǎn)擊事件初始化相關(guān)值
     let oldY = ev.clientY;   //頂點(diǎn)原本的y值   
     let oldX = ev.clientX;   //頂點(diǎn)原本的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;  //鼠標(biāo)移動(dòng)x軸偏移量
      let offsetY = e.clientY - oldY;  //鼠標(biāo)移動(dòng)y軸偏移量
      //if條件是判斷鼠標(biāo)點(diǎn)擊的節(jié)點(diǎn)目標(biāo)對(duì)象
      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`;
      }
      //邊框線(xiàn)的高度/寬度隨著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;
     }
    }
    //鼠標(biāo)抬起后清除鼠標(biāo)移動(dòng)事件
    window.onmouseup = function () {
     window.onmousemove = false;
    }
   }
  }
  changeBox([box, dots, lines, bd])
 </script>
</body>

</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • JavaScript中Hoisting詳解 (變量提升與函數(shù)聲明提升)

    JavaScript中Hoisting詳解 (變量提升與函數(shù)聲明提升)

    函數(shù)聲明和變量聲明總是被JavaScript解釋器隱式地提升(hoist)到包含他們的作用域的最頂端。下面這篇文章主要給大家介紹了關(guān)于JavaScript中Hoisting(變量提升與函數(shù)聲明提升)的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-08-08
  • JavaScript防止表單重復(fù)提交的方法

    JavaScript防止表單重復(fù)提交的方法

    在web開(kāi)發(fā)中,防止表單的重復(fù)提交是一個(gè)非常重要的環(huán)節(jié)。重復(fù)提交會(huì)導(dǎo)致數(shù)據(jù)混亂,甚至可能導(dǎo)致系統(tǒng)崩潰,今天我們將帶領(lǐng)大家從小白級(jí)別到大神級(jí)別的程序員,一起來(lái)學(xué)習(xí)如何在實(shí)際項(xiàng)目中避免表單的重復(fù)提交
    2023-04-04
  • javascript實(shí)現(xiàn)無(wú)縫上下滾動(dòng)特效

    javascript實(shí)現(xiàn)無(wú)縫上下滾動(dòng)特效

    這篇文章主要介紹了javascript實(shí)現(xiàn)無(wú)縫上下滾動(dòng)特效的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • webpack進(jìn)階——緩存與獨(dú)立打包的用法

    webpack進(jìn)階——緩存與獨(dú)立打包的用法

    本篇文章主要介紹了webpack進(jìn)階——緩存與獨(dú)立打包的用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • 原生JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)版計(jì)算器

    原生JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)版計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了原生JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)版計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • JavaScript使用簡(jiǎn)單正則表達(dá)式的數(shù)據(jù)驗(yàn)證功能示例

    JavaScript使用簡(jiǎn)單正則表達(dá)式的數(shù)據(jù)驗(yàn)證功能示例

    這篇文章主要介紹了JavaScript使用簡(jiǎn)單正則表達(dá)式的數(shù)據(jù)驗(yàn)證功能,結(jié)合實(shí)例形式分析了JS針對(duì)表單輸入內(nèi)容的簡(jiǎn)單正則驗(yàn)證操作技巧,需要的朋友可以參考下
    2017-01-01
  • JavaScript獲取頁(yè)面中表單(form)數(shù)量的方法

    JavaScript獲取頁(yè)面中表單(form)數(shù)量的方法

    這篇文章主要介紹了JavaScript獲取頁(yè)面中表單(form)數(shù)量的方法,涉及javascript操作表單document.forms數(shù)組的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 原生JS實(shí)現(xiàn)自定義下拉單選選擇框功能

    原生JS實(shí)現(xiàn)自定義下拉單選選擇框功能

    最近,把項(xiàng)目中用到的下拉框組件重新封裝了一下,以構(gòu)造函數(shù)的方式進(jìn)行封裝,主要方法和事件定義在原型上,下面是主要的實(shí)現(xiàn)代碼并添加了比較詳細(xì)的注釋?zhuān)窒沓鰜?lái)供大家參考
    2018-10-10
  • JavaScript中this詳解

    JavaScript中this詳解

    都說(shuō) JavaScript 是一種很靈活的語(yǔ)言,這其實(shí)也可以說(shuō)它是一個(gè)混亂的語(yǔ)言。它把函數(shù)式編程和面向?qū)ο缶幊挑酆弦黄穑偌由蟿?dòng)態(tài)語(yǔ)言特性,簡(jiǎn)直強(qiáng)大無(wú)比,下面小編給大家介紹Javascript中this詳解,需要的小伙伴可以來(lái)參考下
    2015-09-09
  • 常見(jiàn)JS驗(yàn)證腳本匯總

    常見(jiàn)JS驗(yàn)證腳本匯總

    這篇文章主要介紹了常見(jiàn)JS驗(yàn)證腳本,結(jié)合實(shí)例形式匯總分析了JavaScript用于驗(yàn)證的系統(tǒng)自帶函數(shù)與自定義函數(shù),具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-12-12

最新評(píng)論