vue自定義事件---拖拽


margin布局拖拽

Vue.directive('drag', {
    bind(el, binding, vnode, oldVnode) {

                const dialogHeaderEl = el.querySelector('#top');
                // const dragDom = el.querySelector('.alert_child');
                dialogHeaderEl.style.cursor = 'move';


        let dragBox = el; //獲取當前元素
        dialogHeaderEl.onmousedown = e => {
          //算出鼠標相對元素的位置
          let disX = e.clientX - dragBox.offsetLeft;
          let disY = e.clientY - dragBox.offsetTop;
          document.onmousemove = e => {
            //用鼠標的位置減去鼠標相對元素的位置,得到元素的位置
            let left = e.clientX - disX;
            let top = e.clientY - disY;
            //移動當前元素
            dragBox.style.marginLeft = left + "px";
            dragBox.style.marginTop = top + "px";
          };
          document.onmouseup = e => {
            //鼠標彈起來的時候不再移動
            document.onmousemove = null;
            //預防鼠標彈起來后還會循環(即預防鼠標放上去的時候還會移動)
            document.onmouseup = null;
          };
        };

    }
})

定位拖拽

Vue.directive('show_drag', {
    bind(el, binding, vnode, oldVnode) {
        const dialogHeaderEl = el.querySelector('.bt') || el.querySelector('.top') || el.querySelector('.header')||el.querySelector(".head");
        const dragDom = el;
        dialogHeaderEl.style.cursor = 'move';
        // 獲取原有屬性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
        const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);

        dialogHeaderEl.onmousedown = (e) => {
            // 鼠標按下,計算當前元素距離可視區的距離
            const disX = e.clientX - dialogHeaderEl.offsetLeft;
            const disY = e.clientY - dialogHeaderEl.offsetTop;

            // 獲取到的值帶px 正則匹配替換
            let styL, styT;

            // 注意在ie中 第一次獲取到的值為組件自帶50% 移動之后賦值為px
            if (sty.left.includes('%')) {
                styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100);
                styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100);
            } else {
                styL = +sty.left.replace(/\px/g, '');
                styT = +sty.top.replace(/\px/g, '');
            };

            document.onmousemove = function (e) {
                // 通過事件委托,計算移動的距離 
                var l = e.clientX < 50 || e.clientX > (document.body.clientWidth - 50) ? 0 : e.clientX - disX;
                var t = e.clientY < 50 || e.clientY > (document.body.clientHeight - 50) ? 0 : e.clientY - disY;

                // 移動當前元素
                if (l) {
                    dragDom.style.left = `${l + styL}px`;
                }
                if (t) {
                    dragDom.style.top = `${t + styT}px`;
                }


                //將此時的位置傳出去
                //binding.value({x:e.pageX,y:e.pageY})
            };

            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }
    }
})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM