js 实现三个div上下排列各占屏幕的三分之一推动其中一个div的上下边框改变高度,相邻的div高度跟着改变

1、功能页面

 2、需求描述

  • 板块调整大小操作

要求1:3个板块上下可自由拉伸,当拉伸时,相邻的板块自动对应缩小。例如:第一个板块向下拉伸时,相邻的第二个板块自动缩小;第二个板块若向上拉伸时,相邻第一个板块自动缩小,若向下拉伸时,第三个板块自动缩小。

要求2:每个板块右上角放大窗口按钮点击后,可在当前页面中对该窗口进行放大,在放大的窗口中,可点击右上角缩小窗口按钮进行缩小。

3、实现代码如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
    <style>
      body,
      html {
        width: 100%;
        height: 100%;
        margin: 0;
      }
      .nav{
        width: 100%;
        height: 100%;
      }
      #container1 {
        /* width: 200px; */
        /* height: 200px; */
        width: 100%;
        height: 50%;
        /* padding: 15px; */
        margin: 0px 0 0px 0;
        border: #00cdcd 2px solid;
        box-sizing: border-box;
        -webkit-user-select: none;
        -moz-user-select: none;
        -o-user-select: none;
        user-select: none;
      }
      #container2{
        /* width: 200px;
        height: 200px; */
        width: 100%;
        height: calc(25% - 15px);
        /* padding: 15px; */
        margin: 15px 0 15px 0;
        border: #00cdcd 2px solid;
        box-sizing: border-box;
        -webkit-user-select: none;
        -moz-user-select: none;
        -o-user-select: none;
        user-select: none;
      }
      #container3{
        /* width: 200px;
        height: 200px; */
        width: 100%;
        /* height: 25%; */
        height: calc(25% - 15px);
        /* padding: 15px; */
        margin: 0px 0 0px 0;
        border: #00cdcd 2px solid;
        box-sizing: border-box;
        -webkit-user-select: none;
        -moz-user-select: none;
        -o-user-select: none;
        user-select: none;
      }
      .item1 {
        cursor: default;
        width: 100%;
        height: 100%;
        background: #757575;
      }
      .item2 {
        cursor: default;
        width: 100%;
        height: 100%;
        background: #757575;
      }
      .item3 {
        cursor: default;
        width: 100%;
        height: 100%;
        background: #757575;
      }
    </style>
  </head>
  <body id="body">
    <div class="nav">
      <div id="container1">
        <div class="item1"></div>
      </div>

      <div id="container2">
        <div class="item2"></div>
      </div>

      <div id="container3">
        <div class="item3"></div>
      </div>
    </div>
    <script>
      //需要调整尺寸的div
      let container1 = document.getElementById('container1')
      let container2 = document.getElementById('container2')
      let container3 = document.getElementById('container3')
      // body监听移动事件
      document.getElementById('body').addEventListener('mousemove', move)//鼠标移动事件
      // 鼠标按下事件
      container1.addEventListener('mousedown', down1)//鼠标按下
      // +++
      container2.addEventListener('mousedown', down2)//鼠标按下
      container3.addEventListener('mousedown', down3)//鼠标按下
      // +++
      // 鼠标松开事件
      document.getElementById('body').addEventListener('mouseup', up)//鼠标松开
      
      // 是否开启尺寸修改
      let resizeable = false
      let numInt;
      // 鼠标按下时的坐标,并在修改尺寸时保存上一个鼠标的位置
      let clientX, clientY
      // div可修改的最小宽高
      // let minW = 8, minH = 8
      let minW = 15, minH = 15
      // 鼠标按下时的位置,使用n、s、w、e表示
      let direc = ''
      
      // 鼠标松开时结束尺寸修改
      function up() {//鼠标松开
       resizeable = false
       console.log("鼠标松开",resizeable)
      }
      
      var c1H,c2H,c3H
      // 鼠标按下时开启尺寸修改
      function down1(e) {//鼠标按下
        downCn(e,1)
      }
      function down2(e) {//鼠标按下
        downCn(e,2)
      }
      function down3(e) {//鼠标按下
        downCn(e,3)
      }
      function downCn(e,n){
        let d = getDirection(e,n)
        console.log("鼠标按下",d)
        // 当位置为四个边和四个角时才开启尺寸修改
        if (d !== '') {
          resizeable = true
          numInt = n
          direc = d
          // 获取container1,container2,container3 div 的高度
          c1H = container1.offsetHeight;
          c2H = container2.offsetHeight;
          c3H = container3.offsetHeight;
          clientX = e.clientX
          clientY = e.clientY
        }
      }

      var count = 0;

      // 鼠标移动事件
      function move(e) {//鼠标移动事件
        console.log("往上拖拽",resizeable)
        /*count++;
        // 当计数器为偶数的时候不执行mousemove
        if( count % 2 === 0 ){
          return;
        }*/
        // 实现拖拽功能的代码...

        //var Height = document.documentElement.clientHeight  //屏幕可视高度
        
        let d = getDirection(e,1)
        let d2 = getDirection(e,2)
        let d3 = getDirection(e,3)
        let cursor,cursor2,cursor3
        if (d === '') cursor = 'default';
        else cursor = d + '-resize';

        if (d2 === '') cursor2 = 'default';
        else cursor2 = d2 + '-resize';

        if (d3 === '') cursor3 = 'default';
        else cursor3 = d3 + '-resize';
        // 修改鼠标显示效果
        container1.style.cursor = cursor;
        container2.style.cursor = cursor2;
        container3.style.cursor = cursor3;
        setResizeable(e,numInt)
      }
      
      function setResizeable(e,nIt){
        var Height = document.documentElement.clientHeight  //屏幕可视高度
        // 当开启尺寸修改时,鼠标移动会修改div尺寸
        /*if(nIt == 1){
          if(resizeable){
            // 鼠标按下的位置在底部,修改高度
            if (direc.indexOf('s') !== -1) {
              if((e.clientY - clientY) >= 0){//下
                if(c2H != 15){
                  container1.style.height = Math.max(minH, container1.offsetHeight + (e.clientY - clientY)) + 'px'
                  container2.style.height = Math.max(minH, container2.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container1.style.height = Height - c3H - 45 + 'px'
                  container2.style.height = 15 + 'px'
                  up()
                }
              }else if((e.clientY - clientY) < 0){//上
                if(c1H != minH){
                  container1.style.height = Math.max(minH, container1.offsetHeight + (e.clientY - clientY)) + 'px'
                  container2.style.height = Math.max(minH, container2.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container1.style.height = 15 + 'px'
                  container2.style.height = Height - c3H - 45 + 'px'
                  up()
                }
              } 
              c1H = container1.offsetHeight
              c2H = container2.offsetHeight
              c3H = container3.offsetHeight
              clientY = e.clientY
            }
          }
        }else if(nIt == 2){

        }*/

        if(resizeable){
          if(nIt == 1){
            if (direc.indexOf('s') !== -1) {
              if((e.clientY - clientY) >= 0){//下
                if(c2H != 15){
                  container1.style.height = Math.max(minH, container1.offsetHeight + (e.clientY - clientY)) + 'px'
                  container2.style.height = Math.max(minH, container2.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container1.style.height = Height - c3H - 45 + 'px'
                  container2.style.height = 15 + 'px'
                  up()
                }
              }else if((e.clientY - clientY) < 0){//上
                if(c1H != minH){
                  container1.style.height = Math.max(minH, container1.offsetHeight + (e.clientY - clientY)) + 'px'
                  container2.style.height = Math.max(minH, container2.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container1.style.height = 15 + 'px'
                  container2.style.height = Height - c3H - 45 + 'px'
                  up()
                }
              } 
              c1H = container1.offsetHeight
              c2H = container2.offsetHeight
              c3H = container3.offsetHeight
              clientY = e.clientY
            }
          }else if(nIt == 2){
            // 鼠标按下的位置在底部,修改高度
            if (direc.indexOf('s') !== -1) {
              if((e.clientY - clientY) >= 0){//下
                if(c3H != 15){
                  container2.style.height = Math.max(minH, container2.offsetHeight + (e.clientY - clientY)) + 'px'
                  container3.style.height = Math.max(minH, container3.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container2.style.height = Height - c1H - 45 + 'px'
                  container3.style.height = 15 + 'px'
                  up()
                }
              }else if((e.clientY - clientY) < 0){//上
                if(c2H != minH){
                  container2.style.height = Math.max(minH, container2.offsetHeight + (e.clientY - clientY)) + 'px'
                  container3.style.height = Math.max(minH, container3.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container2.style.height = 15 + 'px'
                  container3.style.height = Height - c1H - 45 + 'px'
                  up()
                }
              } 
              c1H = container1.offsetHeight
              c2H = container2.offsetHeight
              c3H = container3.offsetHeight
              clientY = e.clientY
            }
            // 鼠标按下的位置在上部,修改高度
            if (direc.indexOf('n') !== -1) {
              if((e.clientY - clientY) >= 0){//下
                if(c2H != 15){
                  container1.style.height = Math.max(minH, container1.offsetHeight + (e.clientY - clientY)) + 'px'
                  container2.style.height = Math.max(minH, container2.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container1.style.height = Height - c3H - 45 + 'px'
                  container2.style.height = 15 + 'px'
                  up()
                }
              }else if((e.clientY - clientY) < 0){//上
                if(c1H != minH){
                  container1.style.height = Math.max(minH, container1.offsetHeight + (e.clientY - clientY)) + 'px'
                  container2.style.height = Math.max(minH, container2.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container1.style.height = 15 + 'px'
                  container2.style.height = Height - c3H - 45 + 'px'
                  up()
                }
              } 
              c1H = container1.offsetHeight
              c2H = container2.offsetHeight
              c3H = container3.offsetHeight
              clientY = e.clientY
            }
          }else if(nIt == 3){
            if (direc.indexOf('n') !== -1) {
              if((e.clientY - clientY) >= 0){//下
                if(c3H != 15){
                  container2.style.height = Math.max(minH, container2.offsetHeight + (e.clientY - clientY)) + 'px'
                  container3.style.height = Math.max(minH, container3.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container2.style.height = Height - c1H - 45 + 'px'
                  container3.style.height = 15 + 'px'
                  up()
                }
              }else if((e.clientY - clientY) < 0){//上
                if(c2H != minH){
                  container2.style.height = Math.max(minH, container2.offsetHeight + (e.clientY - clientY)) + 'px'
                  container3.style.height = Math.max(minH, container3.offsetHeight + (clientY - e.clientY) ) + 'px'
                }else{
                  container2.style.height = 15 + 'px'
                  container3.style.height = Height - c1H - 45 + 'px'
                  up()
                }
              } 
              c1H = container1.offsetHeight
              c2H = container2.offsetHeight
              c3H = container3.offsetHeight
              clientY = e.clientY
            }
          }
        }
        
      }

      // 获取鼠标所在div的位置
      function getDirection(ev,n) {
        let xP, yP, offset, dir;
        dir = '';
        
        xP = ev.offsetX;
        yP = ev.offsetY;
        offset = 10;
        
        if (yP < offset) dir += 'n';
        else if (yP > window[`container${n}`].offsetHeight - offset) dir += 's';
        if (xP < offset) dir += 'w';
        else if (xP > window[`container${n}`].offsetWidth - offset) dir += 'e';
        
        return dir;
      }
     </script>
  </body>
</html>

运行效果如下:

    鼠标移入边框按住鼠标可以上下拖到

 类似与这种效果

猜你喜欢

转载自blog.csdn.net/qq_45404003/article/details/126748325