js 鼠标拖拽改变div宽度高度

这个示例展示了如何使用JavaScript实现一个可拖拽调整大小的div元素。通过监听鼠标按下、移动和释放事件,动态更新div的宽度和高度,并在鼠标移动过程中改变背景颜色。此外,还限制了div最小尺寸,防止无限缩小。

js 鼠标拖拽改变div宽度高度,

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title></title>
  </head>
  <style type="text/css">
    body,p{
      margin: 0;
      padding: 0;
    }
    #box {
      position: absolute;
      width: 200px;
      height: 200px;
      background-color: red;
    }
    p {
      font-size: 20px;
      color: white;
    }
  </style>
  <body>
    <div id="box">
      <p></p>
      <p></p>
    </div>
  </body>
  <script>
    var box = document.getElementById("box");
    var p1 = document.getElementsByTagName("p")[0];
    var p2 = document.getElementsByTagName("p")[1];
    box.onmousedown = function (e) {
      var Ev = e;
      var Ex = Ev.clientX; //点击鼠标时储存x轴
      var Ey = Ev.clientY; //点击鼠标时储存y轴
      var dw = box.offsetWidth; //存储默认的div的宽度。
      var dh = box.offsetHeight; //存储默认的div的高度。
      var texb = "";
    
      if (Ev.clientX < box.offsetLeft + box.offsetWidth) {
        //判断鼠标是否在div内
        texb = true;
        console.log(texb);
      }
      document.onmousemove = function (e) {
        if (texb) {
          //判断是在div内移动
          var iE = e;
          if (iE.clientY == 0) {
            //向左移动
            box.style.width = dw + (iE.clientX - Ex) + "px";
          } else if (iE.clientX == 0) {
            //向右移动
            box.style.height = dh + (iE.clientY - Ey) + "px";
          } else if(iE.clientY != 0 && iE.clientX != 0){
            //同时移动
            box.style.width = dw + (iE.clientX - Ex) + "px";
            box.style.height = dh + (iE.clientY - Ey) + "px";
			box.style.background = colorrim()
			
          }else if(box.offsetWidth){
			console.log("sfds")
            //当缩小到一定的范围内不能在缩小
            box.style.width = "20px";
            box.style.height = "20px";
          }
        }
        document.onmouseup = function () {
          //鼠标离开时清除事件
          document.onmousemove = null;
        };
	
          p1.innerText = "宽度:"+box.style.width;
          p2.innerText = "高度:"+box.style.height;
      
      };
    };
    p1.innerText = "宽度:200px";
    p2.innerText = "高度:200px";

	//定义随机颜色
	function colorrim(){
		var a = Math.floor(Math.random() *256);
		 	b = Math.floor(Math.random() *256);
			c = Math.floor(Math.random() *256);
		return "rgb("+ a +","+ b +","+ c +")"
	}
  </script>
</html>

在这里插入图片描述

在前端实现拖拽调整`div`宽度可以使用原生 JavaScript 结合 DOM 事件来完成,以下是具体步骤和示例代码: 1. **HTML 结构**:创建一个包含可调整宽度的`div`元素和一个用于拖拽的手柄元素。 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Drag to Resize Div</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="resizable" id="resizableDiv"> <div class="handle" id="handle"></div> </div> <script src="script.js"></script> </body> </html> ``` 2. **CSS 样式**:设置`div`和手柄的样式,确保手柄可以被点击和拖拽。 ```css .resizable { width: 300px; height: 200px; background-color: #f0f0f0; position: relative; border: 1px solid #ccc; } .handle { position: absolute; top: 0; right: 0; width: 10px; height: 100%; background-color: #ccc; cursor: ew-resize; } ``` 3. **JavaScript 代码**:监听手柄的鼠标事件,根据鼠标移动的距离来调整`div`的宽度。 ```javascript const resizableDiv = document.getElementById(&#39;resizableDiv&#39;); const handle = document.getElementById(&#39;handle&#39;); let isDragging = false; let startX; let startWidth; handle.addEventListener(&#39;mousedown&#39;, (e) => { isDragging = true; startX = e.clientX; startWidth = parseInt(getComputedStyle(resizableDiv).width, 10); }); document.addEventListener(&#39;mousemove&#39;, (e) => { if (isDragging) { const dx = e.clientX - startX; const newWidth = startWidth + dx; resizableDiv.style.width = newWidth + &#39;px&#39;; } }); document.addEventListener(&#39;mouseup&#39;, () => { isDragging = false; }); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vue1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值