VUE實現(xiàn)可隨意拖動的彈窗組件
背景:項目需要,我們引入了前端框架就是目前最流行的框架之一vue,同時引入了一套由餓了嗎維護的ui庫,由于我們是在pc端使用發(fā)現(xiàn)它竟然沒有提供可隨意拖動的窗口,可能用的更多的時移動端吧吧,于是就隨手寫了一個,比較簡單吧,但是做過的就會知道也是有一些小小的技巧,記錄下吧留作備用。
由于不是很難,就不做過多解釋了,直接上代碼:
<template> <el-container v-bind:id="id" v-if="dialogVisible"> <el-header> <div @mousedown="mousedown"> <h2 v-html="title"></h2> <div @click.stop="closeDialog()" style="position: absolute;top: 0px; right: 20px;"> <span> <svg class="icon" aria-hidden="false"> <use xlink:href='#el-icon-ext-close'></use> </svg> </span> </div> </div> </el-header> <el-main> <slot>這里是內(nèi)容</slot> </el-main> <el-footer> <span class="dialog-footer"> <el-button @click="closeDialog">取 消</el-button> <el-button type="primary" @click="closeDialog">確 定</el-button> </span> </el-footer> </el-container> </template> <script> export default { name: 'Window', props: { titlex: String, id: [String, Number] }, data() { return { title: '標題', selectElement: '' } }, computed: { dialogVisible: { get: function () { return this.$store.state.dialogVisible }, set: function (newValue) { this.$store.commit('newDialogVisible', newValue) } } }, methods: { closeDialog(e) { this.dialogVisible = false // alert(this.dialogVisible) this.$store.commit('newDialogVisible', false) }, mousedown(event) { this.selectElement = document.getElementById(this.id) var div1 = this.selectElement this.selectElement.style.cursor = 'move' this.isDowm = true var distanceX = event.clientX - this.selectElement.offsetLeft var distanceY = event.clientY - this.selectElement.offsetTop // alert(distanceX) // alert(distanceY) console.log(distanceX) console.log(distanceY) document.onmousemove = function (ev) { var oevent = ev || event div1.style.left = oevent.clientX - distanceX + 'px' div1.style.top = oevent.clientY - distanceY + 'px' } document.onmouseup = function () { document.onmousemove = null document.onmouseup = null div1.style.cursor = 'default' } } } } </script> <style scoped> .el-container { position: absolute; height: 500px; width: 500px; border: 1px; top: 20%; left: 35%; border-radius: 2px; } .dialog-footer { text-align: right; } .el-main { background-color: white; } .el-footer { background-color: white; } .el-header { background-color: white; color: #333; line-height: 60px; } .el-aside { color: #333; } </style>
備注:解決了鼠標拖動過快移除窗口后終端問題,其它優(yōu)點請自測,如有問題請留言!
以上這篇VUE實現(xiàn)可隨意拖動的彈窗組件就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue3+Three.js實現(xiàn)為模型添加點擊事件
本文主要介紹了如何在Vue3和Three.js環(huán)境中為模型添加點擊事件監(jiān)聽,具體方法是利用Three.js的Vector2和Raycaster,首先,創(chuàng)建一個Vector2對象來獲取鼠標位置;然后,創(chuàng)建一個Raycaster對象來投射光線2024-10-10ElementUI日期選擇器時間選擇范圍限制的實現(xiàn)
在日常開發(fā)中,我們會遇到一些情況,限制日期的范圍的選擇,本文就詳細的介紹了ElementUI日期選擇器時間選擇范圍限制的實現(xiàn),文中通過示例代碼介紹的非常詳細,感興趣的可以了解一下2022-04-04茶余飯后聊聊Vue3.0響應式數(shù)據(jù)那些事兒
這篇文章主要介紹了茶余飯后聊聊Vue3.0響應式數(shù)據(jù)那些事兒,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10Vue計算屬性computed與方法methods的區(qū)別及說明
這篇文章主要介紹了Vue計算屬性computed與方法methods的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08通過debug搞清楚.vue文件如何變成.js文件(案例詳解)
這篇文章主要介紹了通過debug搞清楚.vue文件如何變成.js文件,本文以@vitejs/plugin-vue舉例,通過debug的方式帶你一步一步的搞清楚vue文件是如何編譯為js文件的,需要的朋友可以參考下2024-07-07