鼠标对div框拖拽缩放叠层操作(vue3版本+js)

 先设置了三个模拟框,当鼠标移动到div上删除按钮和缩放表示可以显示出来

8afac35ecaad454592e1d7983e774ed0.png

布局代码示例如下:

类似于<el-xxxx/>等标签是取自elementplus,没有导入的需要自行导入

    <div id="container">
    <div
      id="box"
      v-for="index in 3"
      :key="index"
      @mousedown="dragAble(index)"
      :itemid="index"
      :style="{ zIndex: index }"
    >
      <span class="close-btn" @click.prevent="deleteChart(index)"
        ><el-icon><Close /></el-icon
      ></span>
      <div id="main"></div>
      <div id="scale" class="double-arrow-btn" @mousedown="scaleAble(index)">
        <el-icon><TopLeft /></el-icon><el-icon><BottomRight /></el-icon>
      </div>
    </div>
    </div>
/**
*页面样式
*/
#box {
  width: 40%;
  height: 40%;
  position: absolute;
  cursor: move;
  border: solid 1px #ddd;
  padding: 2px;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  background-color: white;
}
#main {
  width: 95%;
  height: 95%;
  position: relative;
}
.double-arrow-btn {
  position: absolute;
  bottom: 0px;
  right: 0px;
  display: none;
}
.close-btn {
  position: absolute;
  top: 0px;
  right: 0px;
  display: none;
}
#box:hover .close-btn {
  display: block;
  cursor: pointer;
}
#box:hover .double-arrow-btn {
  display: block;
  cursor: se-resize;
}
#container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  position: absolute;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}

 拖拽方法dragAble()

const dragAble = (index, e: any) => {
  console.log("index:"+index);
  
  //获取drag元素
  console.log(document.querySelectorAll("#box"));
  var drag1 = document.querySelectorAll("#box") as NodeListOf<HTMLElement>;

  let drag = drag1[index - 1];

  //当鼠标按下时
  // drag.onmousedown = function (e: any) {
  //阻止冒泡 避免缩放触发移动事件
  // e.stopPropagation();
  // e.preventDefault();
  // console.log("正在拖拽");

  // drag.style.zIndex = (drag.style.zIndex)+1;
  console.log(drag.style.zIndex);

  //做到浏览器兼容
  e = e || window.event;
  let diffX = e.clientX - drag.offsetLeft;
  let diffY = e.clientY - drag.offsetTop;

  document.onmousemove = function (e: any) {
    e.preventDefault();
    let left = e.clientX - diffX;
    let top = e.clientY - diffY;
    if (left < 0) {
      left = 0;
    } else if (left > window.innerWidth - drag.offsetWidth) {
      left = window.innerWidth - drag.offsetWidth;
    }
    if (top < 0) {
      top = 0;
    } else if (top > window.innerHeight - drag.offsetHeight) {
      top = window.innerHeight - drag.offsetHeight;
    }
    drag.style.left = left + "px";
    drag.style.top = top + "px";
  };

  // 当鼠标抬起时
  document.onmouseup = function () {
    this.onmousemove = null;
    this.onmouseup = null;
  };
  drag.onmouseleave = function () {
    drag.onmousemove = null;
    drag.onmouseup = null;
  };
};

缩放方法scaleAble()

const scaleAble = (index, e: any) => {
  //获取drag元素
  const drag1 = document.querySelectorAll("#box") as NodeListOf<HTMLElement>;
  let drag = drag1[index - 1];
  const scale1 = document.querySelectorAll(
    ".double-arrow-btn"
  ) as NodeListOf<HTMLElement>;
  let scale = scale1[index - 1];
  const box = document.getElementById("container");
  // scale.onmousedown = function (e: any) {
  e = e || window.event;
  console.log("正在缩放");
  //阻止冒泡 避免缩放触发移动事件
  e.stopPropagation();
  // 取消事件的默认动作
  e.preventDefault();
  // 定义position
  var position = {
    w: drag.offsetWidth, // 被缩放元素的offsetWidth
    h: drag.offsetHeight, // 被缩放元素的offsetHeight
    x: e.clientX, // 当前窗口鼠标指针的水平坐标
    y: e.clientY, // 当前窗口鼠标指针的垂直坐标
  };

  drag.onmousemove = function (e: any) {
    e = e || window.event;
    e.preventDefault();
    // 设置最大缩放为30*30 Math.max取最大值

    var w = Math.max(30, e.clientX - position.x + position.w);
    var h = Math.max(30, e.clientY - position.y + position.h);
    console.log(h);
    // 设置最大的宽高
    w =
      w >= box.offsetWidth - drag.offsetLeft
        ? box.offsetWidth - drag.offsetLeft
        : w;
    h =
      h >= box.offsetHeight - drag.offsetTop
        ? box.offsetHeight - drag.offsetTop
        : h;
    console.log(w);
    console.log(h);

    drag.style.width = w + "px";
    drag.style.height = h + "px";
  };
  document.onmouseup = function () {
    drag.onmousemove = null;
    drag.onmouseup = null;
  };
  drag.onmouseleave = function () {
    drag.onmousemove = null;
    drag.onmouseup = null;
  };
};

叠层方法changePlace()

  var drag1 = document.querySelectorAll("#box") as NodeListOf<HTMLElement>;
  let drag = drag1[index - 1];
  var maxIndex = index - 1;
  var maxDrag = drag;
  if(drag1.length>1){
    for (var i = 0; i < drag1.length; i++) {
    if (drag1[i].style.zIndex > maxDrag.style.zIndex) {
      maxDrag = drag1[i];
      maxIndex = i;
    }
  }
  var temp=maxDrag.style.zIndex
  for(var k=0;k<drag1.length;k++){
    if(drag1[k].style.zIndex>drag.style.zIndex){
      drag1[k].style.zIndex=String(parseInt(drag1[k].style.zIndex)-1);
      continue
    }
  }
  drag.style.zIndex=temp
  }

假删除只具有提示作用,此处只是测试删除按钮是否可用deleteChart ()

const deleteChart = (index:any) => {
alert("此处为假删除!!!")
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PCB叠层2+N+2是一种常见的印制电路板(PCB)叠层结构。在这种结构中,板件的层数为2层,中间有N层内部层,再加上2层外部层。这种结构的设计可以满足特定的电路需求,并且在制造过程中可以进行优化以提高生产效率和降低成本。 引用\[1\]中提到了一种常规的二次积层HDI印制板的叠层结构为(1+1+4+1+1),这种结构可以看作是2+N+2的一种特例。引用\[2\]中也提到了对于三次积层及以上的PCB板,可以通过类似的设计思路进行优化,减少压合次数,提高成品率。 另外,引用\[3\]中提到了多层PCB的应用,特别是在高频电路设计中。多层PCB由多个覆铜基片和结合层组成,可以满足尺寸和体积的限制,并满足多层电路设计的特殊要求。 综上所述,PCB叠层2+N+2是一种常见的印制电路板叠层结构,可以根据具体需求进行优化和设计,以满足特定的电路要求。 #### 引用[.reference_title] - *1* *2* [HDI-PCB板常用叠层结构-详细](https://blog.csdn.net/icifan/article/details/80397751)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【射频知识】PCB材料/层叠/信号注入设计与传输线实现形式的性能比对](https://blog.csdn.net/weixin_42017484/article/details/125787045)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值