vue拖拽调整宽度指令

Vue.directive(
‘catlogDragResize’, {
bind: function (el, binding) {
const dragbar = el.querySelector(‘.dragbar’);

        dragbar.onmousedown = (e) => {
            // 鼠标按下,计算当前元素距离可视区的距离
            const disX = e.clientX
            const w = el.clientWidth
            const minW = 0
            const maxW = 500
            var nw
            document.onmousemove = function (e) {
                // 通过事件委托,计算移动的距离
                const l = e.clientX - disX
                // 改变当前元素宽度,不可超过最小最大值
                nw = w + l
                nw = nw < minW ? minW : nw
                nw = nw > maxW ? maxW : nw
                el.style.width = `calc(0px + ${nw}px)`

                if (nw < 140) {
                    store.commit('SET_HIDE_CATLOG', true)
                } else {
                    store.commit('SET_HIDE_CATLOG', false)
                }
                if (binding.arg) {
                    // 兼容小屏幕宽度超出问题
                    if (nw <= 60) {
                        document.getElementById('rightContainer').style.marginLeft = `0`
                    } else {
                        document.getElementById('rightContainer').style.marginLeft = `calc(0px + ${nw + 3}px)`
                    }
                }
            }
            document.onmouseup = function () {
                document.onmousemove = null
                document.onmouseup = null
            }

        };
    },
    // update(el, binding) {
    //     if (!binding.value.contentShow) {
    //         el.style.marginLeft = -el.offsetWidth + 'px';
    //     } else {
    //         el.style.marginLeft = '0px';
    //     }
    // },
}

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值