element ui 中的彈框的拖動(dòng)和拉伸以及其他問(wèn)題

代碼:

//移動(dòng)
<template>
<el-dialog v-x>
      <div class="dialog-body"><div class="line" v-dialogDragWidth="$refs.dialog__wrapper"></div>
      </div>
    </el-dialog>
//寬高
    <el-dialog v-dialogDragWidth>
      <div class="dialog-body">
          <div 
              class="line"
              v-dialogDragWidth="$refs.dialog__wrapper"></div>
          </div>
    </el-dialog>
</template>
<script>
      Vue.directive('x', {
                    bind(el, binding, vnode, oldVnode) {
                            const dialogHeaderEl = el.querySelector('.el-dialog__header');
                            const dragDom = el.querySelector('.el-dialog');
                            dialogHeaderEl.style.cursor = 'move';
              // 獲取原有屬性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
                            const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
                            dialogHeaderEl.onmousedown = (e) => {
                                    // 鼠標(biāo)按下,計(jì)算當(dāng)前元素距離可視區(qū)的距離
                                    const disX = e.clientX - dialogHeaderEl.offsetLeft;
                                    const disY = e.clientY - dialogHeaderEl.offsetTop;
                                    // 獲取到的值帶px 正則匹配替換
                                    let styL, styT;
                  // 注意在ie中 第一次獲取到的值為組件自帶50% 移動(dòng)之后賦值為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) {
                // 通過(guò)事件委托,計(jì)算移動(dòng)的距離 
                const l = e.clientX - disX;
                const t = e.clientY - disY;

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

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

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

// v-dialogDragWidth: 彈窗寬度拖大 拖小
Vue.directive('dialogDragWidth', {
    bind(el, binding, vnode, oldVnode) {
        const dragDom = binding.value.$el.querySelector('.el-dialog');

        el.onmousedown = (e) => {
            
            // 鼠標(biāo)按下,計(jì)算當(dāng)前元素距離可視區(qū)的距離
            const disX = e.clientX - el.offsetLeft;
            
            document.onmousemove = function (e) {
                e.preventDefault(); // 移動(dòng)時(shí)禁用默認(rèn)事件

                // 通過(guò)事件委托,計(jì)算移動(dòng)的距離 
                const l = e.clientX - disX;
                dragDom.style.width = `${l}px`;
            };

            document.onmouseup = function (e) {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }  
    }
})
</script>

script 必須寫(xiě)在new vue前面

彈框中內(nèi)容過(guò)多的時(shí)候,會(huì)給一個(gè)固定高,多出的會(huì)出現(xiàn)滾動(dòng)條。


多個(gè)播放按鈕用公用一個(gè)彈框

問(wèn)題:第一個(gè)播放滾動(dòng)到一個(gè)位置,打開(kāi)第二個(gè)播放的時(shí)候滾動(dòng)條會(huì)記得上一個(gè)滾動(dòng)的位置。
解決方法,在彈框的beforeClose事件中加上一個(gè)js代碼

document.getElementsByClassName('dialogueList')[0].scrollTop=0

這樣就可以讓彈框每次關(guān)閉的時(shí)候就可以讓滾動(dòng)條到最上面

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 學(xué)習(xí)過(guò)程中突發(fā)奇想 程序語(yǔ)言都是處理0和1 肯定很多地方是一樣的,那么肯定有書(shū)教你怎么寫(xiě)程序 學(xué)習(xí)編程的第一要素是...
    joker731閱讀 1,086評(píng)論 0 3
  • 一、直接引入`import vue from 'vue'` 報(bào)vue-warn錯(cuò)誤 //如果直接使用import ...
    代小代isDelenDelen閱讀 7,147評(píng)論 1 16
  • 一:什么是閉包?閉包的用處? (1)閉包就是能夠讀取其他函數(shù)內(nèi)部變量的函數(shù)。在本質(zhì)上,閉包就 是將函數(shù)內(nèi)部和函數(shù)外...
    xuguibin閱讀 10,019評(píng)論 1 52
  • ## 框架和庫(kù)的區(qū)別?> 框架(framework):一套完整的軟件設(shè)計(jì)架構(gòu)和**解決方案**。> > 庫(kù)(lib...
    Rui_bdad閱讀 3,150評(píng)論 1 4
  • 從你上小學(xué)的第一天開(kāi)始,我就思考過(guò)一個(gè)問(wèn)題,如果哪天你開(kāi)始寫(xiě)日記了,我作為媽媽是看還是不看? 現(xiàn)在,你四年級(jí)了,你...
    芳與華閱讀 575評(píng)論 2 4

友情鏈接更多精彩內(nèi)容