美文网首页
左右布局,可拖拽重新布局

左右布局,可拖拽重新布局

作者: gem_Y | 来源:发表于2020-04-03 09:18 被阅读0次

    先看效果:


    左右布局可拖拽.gif
    <div id="metadata-box">
      <div id="left">左边的div</div>
      <div id="middle"></div>
      <div id="right">右边的div</div>
    </div>
    
      methods: {
        $dragControllerDiv() {
          // debugger
          const resizeBtn = document.getElementById('resize-btn')
          const left = document.getElementById('left')
          const middle = document.getElementById('middle')
          const right = document.getElementById('right')
          const box = document.getElementById('metadata-box')
    
          left.style.width = this.leftBoxWidth * 1 + 'px'
          right.style.width = (box.clientWidth - this.leftBoxWidth - 24) + 'px'
    
          resizeBtn.onmousedown = (resizeEvent) => {
            const startX = resizeEvent.clientX
            middle.left = middle.offsetLeft
            document.onmousemove = (documentEvent) => {
              const endX = documentEvent.clientX
              let moveLen = middle.left + (endX - startX)
              const maxT = box.clientWidth - middle.offsetWidth
              if (moveLen < 200) moveLen = 200
              if (moveLen > maxT - 500) moveLen = maxT - 500
              left.style.width = moveLen + 'px'
              right.style.width = (box.clientWidth - moveLen - 24) + 'px'
            }
            document.onmouseup = () => {
              document.onmousemove = null
              document.onmouseup = null
              setItem('metaDataLeftBoxWidth', left.clientWidth)
              if (resizeBtn.releaseCapture) resizeBtn.releaseCapture()
            }
            if (resizeBtn.setCapture) resizeBtn.setCapture()
            return false
          }
        }
      },
      mounted() {
        this.$dragControllerDiv()
      },
      created() {
        this.leftBoxWidth = getItem('metaDataLeftBoxWidth') || 360
      }
    

    相关文章

      网友评论

          本文标题:左右布局,可拖拽重新布局

          本文链接:https://www.haomeiwen.com/subject/gdmcphtx.html