vue拖拽滑动分割面板

需求背景

左边是列表,右边是详情。 想更大范围的查看详情,所以要拖拽滑块,进行面板的分割

整体思路

  • 在布局上,采用flex布局,单位采用百分比。设置flex:1,让其自动伸缩
  • 滑动滑块,计算滑块在滑动过程中,占整个页面的百分比
  • 将百分比,通过动态样式赋值给列表页的宽度
  • 同时改变滑块的位置(也是百分比形式)
  • 其次就是在vue里对鼠标事件的使用

代码实现

在template里 写事件

@mousedown.prevent="mousedown"

在methods里写方法

  /**
     * 按下鼠标
     */
      mousedown() {
        document.addEventListener('mousemove', this.handleMouseMove, false);
        document.addEventListener('mouseup', this.handleMouseUp, false);
    },
    /**
     * 按下滑动器,移动鼠标
     */
    handleMouseMove(e) {
      /**
       * 计算容器总宽度
       * 计算滑块到左边的距离
       * 计算偏移百分比: (滑块距离左边的距离 / 页面宽度) * 100
       * 传递移动百分比和距左边的距离
       */
      const clientRect = this.$refs.mainContent.getBoundingClientRect();
      const offset = e.pageX;
      const panelPercent = (offset / clientRect.width) * 100;
      this.panelPercent = panelPercent;
      this.mainContentWidth = offset;
      this.$refs.imgMove.style.left = panelPercent + '%';
      console.log('拖动中', this.panelPercent);
    },
    /**
     * 松开滑动器
     */
    handleMouseUp () {
      document.removeEventListener('mousemove', this.handleMouseMove, false);
    },

对边界处理

 if (this.panelPercent < this.min) {
        this.panelPercent = this.min
      }
      if (this.panelPercent > this.max) {
        this.panelPercent = this.max
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值