【sgDragSize】自定義拖拽修改DIV尺寸組件,適用于窗體大小調整

核心原理就是在四條邊、四個頂點加上透明的div,給不同方向提供按下移動鼠標監聽?,對應計算寬度高度、坐標變化?

特性:

  1. 支持設置拖拽的最小寬度、最小高度、最大寬度、最大高度
  2. 可以雙擊某一條邊,最大化對應方向的尺寸;再一次雙擊,則會恢復到原始大小

sgDragSize源碼

<template><div :class="$options.name" :disabled="disabled" draggable="false"><div :class="`resize-handle resize-${a}`" draggable="false" @mousedown.stop="clickResizeHandle(a)"@dblclick.stop="dblclickResizeHandle(a)" v-for="(a, i) in sizeIndexs" :key="i"></div></div>
</template>
<script>
export default {name: 'sgDragSize',data() {return {dragSizeIndex: '',originRect: {},dblclickOriginRect: {},sizeIndexs: ['top','right','bottom','left','top-left','top-right','bottom-left','bottom-right',],}},props: ["disabled",//屏蔽"minWidth",//拖拽的最小寬度"minHeight",//拖拽的最小高度"maxWidth",//拖拽的最大寬度"maxHeight",//拖拽的最大高度],watch: {disabled: {handler(newValue, oldValue) {newValue && this.__removeWindowEvents();}, deep: true, immediate: true,},},destroyed() {this.__removeWindowEvents();},methods: {clickResizeHandle(d) {this.dragSizeIndex = d;this.mousedown(d);},dblclickResizeHandle(d) {let rect = this.$el.getBoundingClientRect();rect.width < innerWidth && rect.height < innerHeight && (this.dblclickOriginRect = rect);this.dblResize(d, rect);},__addWindowEvents() {this.__removeWindowEvents();addEventListener('mousemove', this.mousemove_window);addEventListener('mouseup', this.mouseup_window);},__removeWindowEvents() {removeEventListener('mousemove', this.mousemove_window);removeEventListener('mouseup', this.mouseup_window);},mousedown(e) {this.originRect = this.$el.getBoundingClientRect();this.originRect.bottomRightX = this.originRect.x + this.originRect.width;//右下角坐標.xthis.originRect.bottomRightY = this.originRect.y + this.originRect.height;//右下角坐標.ythis.$emit('dragStart', e);this.__addWindowEvents();},mousemove_window({ x, y }) {let minWidth = this.minWidth || 50, minHeight = this.minHeight || 50, maxWidth = this.maxWidth || innerWidth, maxHeight = this.maxHeight || innerHeight;x < 0 && (x = 0), y < 0 && (y = 0), x > innerWidth && (x = innerWidth), y > innerHeight && (y = innerHeight);let style = {};switch (this.dragSizeIndex) {case 'top-left':style.left = x;style.top = y;style.width = this.originRect.bottomRightX - x;style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.bottomRightX - minWidth);style.height = this.originRect.bottomRightY - y;style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.bottomRightY - minHeight);break;case 'top':style.left = this.originRect.x;style.top = y;style.width = this.originRect.width;style.height = this.originRect.bottomRightY - y;style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.bottomRightY - minHeight);break;case 'top-right':style.left = this.originRect.x;style.top = y;style.width = x - this.originRect.x;style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.x);style.height = this.originRect.bottomRightY - y;style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.bottomRightY - minHeight);break;case 'left':style.left = x;style.top = this.originRect.y;style.width = this.originRect.bottomRightX - x;style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.bottomRightX - minWidth);style.height = this.originRect.height;break;case 'right':style.left = this.originRect.x;style.top = this.originRect.y;style.width = x - this.originRect.x;style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.x);style.height = this.originRect.height;break;case 'bottom-left':style.left = x;style.top = this.originRect.y;style.width = this.originRect.bottomRightX - x;style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.bottomRightX - minWidth);style.height = y - this.originRect.y;style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.y);break;case 'bottom':style.left = this.originRect.x;style.top = this.originRect.y;style.width = this.originRect.width;style.height = y - this.originRect.y;style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.y);break;case 'bottom-right':style.left = this.originRect.x;style.top = this.originRect.y;style.width = x - this.originRect.x;style.width <= minWidth && (style.width = minWidth, style.left = this.originRect.x);style.height = y - this.originRect.y;style.height <= minHeight && (style.height = minHeight, style.top = this.originRect.y);break;default:}style.width > maxWidth && (style.width = maxWidth);style.height > maxHeight && (style.height = maxHeight);Object.keys(style).forEach(k => style[k] = `${style[k]}px`);style['transition-property'] = 'width,height';style['transition-duration'] = '0,0';this.$emit('dragging', style);},dblResize(d, rect) {let style = {};switch (d) {case 'top-left':break;case 'top':case 'bottom':style.left = this.originRect.x;style.top = rect.height >= innerHeight ? this.dblclickOriginRect.y : 0;style.width = this.originRect.width;style.height = rect.height >= innerHeight ? this.dblclickOriginRect.height : innerHeight;break;case 'top-right':break;case 'left':case 'right':style.left = rect.width >= innerWidth ? this.dblclickOriginRect.x : 0;style.top = this.originRect.y;style.width = rect.width >= innerWidth ? this.dblclickOriginRect.width : innerWidth;style.height = this.originRect.height;break;case 'bottom-left':break;case 'bottom-right':break;default:}Object.keys(style).forEach(k => style[k] = `${style[k]}px`);style['transition-property'] = 'width,height';style['transition-duration'] = '0.1s,0.1s';this.$emit('dragging', style);},mouseup_window(e) {this.$emit('dragEnd', e);this.__removeWindowEvents();},}
};
</script> 
<style lang="scss">
.sgDragSize {position: absolute;width: 100%;height: 100%;left: 0;top: 0;pointer-events: none;.resize-handle {position: absolute;z-index: 100;display: block;pointer-events: auto;}&[disabled] {.resize-handle {pointer-events: none;}}.resize-top {cursor: n-resize;top: -3px;left: 0px;height: 7px;width: 100%;}.resize-right {cursor: e-resize;right: -3px;top: 0px;width: 7px;height: 100%;}.resize-bottom {cursor: s-resize;bottom: -3px;left: 0px;height: 7px;width: 100%;}.resize-left {cursor: w-resize;left: -3px;top: 0px;width: 7px;height: 100%;}.resize-top-right {cursor: ne-resize;width: 16px;height: 16px;right: -8px;top: -8px;}.resize-bottom-right {cursor: se-resize;width: 20px;height: 20px;right: -8px;bottom: -8px;background: url('/static/img/desktop/sgDragSize/resize_corner.png') no-repeat;}.resize-bottom-left {cursor: sw-resize;width: 16px;height: 16px;left: -8px;bottom: -8px;}.resize-top-left {cursor: nw-resize;width: 16px;height: 16px;left: -8px;top: -8px;}
}
</style>

應用

<template><div><div class="box" :style="style"><label>最小尺寸:寬度400px,高度200px</label><sgDragSize @dragging="d => style = d" :minWidth="400" :minHeight="200" /></div></div>
</template>
<script>
import sgDragSize from "@/vue/components/admin/sgDragSize";
export default {components: {sgDragSize,},data() {return {style: {height: '500px',width: '800px',left: '100px',top: '100px',},}},
};
</script>
<style lang="scss" scoped>
.box {position: absolute;display: flex;justify-content: center;align-items: center;background-color: #409EFF55;box-sizing: border-box;border: 1px solid #409EFF;label {user-select: none;color: #409EFF;}
}
</style>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/42027.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/42027.shtml
英文地址,請注明出處:http://en.pswp.cn/news/42027.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

一次Linux中的木馬病毒解決經歷(6379端口---newinit.sh)

病毒入侵解決方案 情景 最近幾天一直CPU100%,也沒有注意看到了以為正常的服務調用,直到騰訊給發了郵件警告說我的服務器正在入侵其他服務器的6379端口,我就是正常的使用不可能去入侵別人的系統的,這是違法的. 排查 既然入侵6379端口,就懷疑是通過我的Redis服務進入的我的系統…

Vue基礎-1.知識導航

知識導航&#xff08;就問全不全&#xff09; 當學習 Vue.js 時&#xff0c;除了基本的 HTML、CSS 和 JavaScript 知識外&#xff0c;還有一些其他的技術和語法需要了解&#xff0c;例如 ES6 和 TypeScript。以下是您可能需要學習的一些基礎知識和對應的學習資源&#xff0c;我…

css中變量和使用變量和運算

變量&#xff1a; 語法&#xff1a;--css變量名&#xff1a;值&#xff1b; --view-theme: #1a99fb; css使用變量&#xff1a; 語法&#xff1a;屬性名&#xff1a;var( --css變量名 )&#xff1b; color: var(--view-theme); css運算&#xff1a; 語法&#xff1a;屬性名…

vue3 rouer params傳參的問題

route.params在頁面刷新的時候數據會丟失&#xff0c;所以vue3 棄用了params方式&#xff01; 但是&#xff0c;vue3又更新了一個替代params的方式&#xff1a;history API import { useRouter } from "vue-router" const router userRouter; // 跳轉路由&#xff…

JDBC封裝與設計模式

什么是 DAO &#xff1f; Data Access Object(數據存取對象) 位于業務邏輯和持久化數據之間實現對持久化數據的訪問 DAO起著轉換器的作用&#xff0c;將數據在實體類和數據庫記錄之間進行轉換。 ----------------------------------------------------- DAO模式的組成部分 …

數據結構--拓撲排序

數據結構–拓撲排序 AOV? A O V ? \color{red}AOV? AOV?(Activity On Vertex NetWork&#xff0c;?頂點表示活動的?)&#xff1a; ? D A G 圖 \color{red}DAG圖 DAG圖&#xff08;有向?環圖&#xff09;表示?個?程。頂點表示活動&#xff0c;有向邊 < V i , V j …

計算機網絡的性能指標

計算機網絡的性能指標 1. 速率 速率是指數據在網絡中傳送的速度&#xff0c;通常用比特率或數據率來表示&#xff0c;單位是b/s&#xff0c;或bit/s&#xff0c;即比特每秒&#xff0c;或者bps(bit per second)。 速率單位&#xff1a;1 Ybps 10^24 bps(堯), 1 Zbps 10^21…

python中的lstm:介紹和基本使用方法

python中的lstm&#xff1a;介紹和基本使用方法 未使用插件 LSTM&#xff08;Long Short-Term Memory&#xff09;是一種循環神經網絡&#xff08;RNN&#xff09;的變體&#xff0c;專門用于處理序列數據。LSTM 可以記憶序列中的長期依賴關系&#xff0c;這使得它非常適合于各…

深度思考rpc框架面經之四

7 netty機制的一些理解 推薦閱讀&#xff1a; 深度思考netty網絡編程框架 7.1 Netty支持的端口號: Netty可以綁定到任何合法的端口號&#xff0c;這與大多數網絡庫類似。有效的端口范圍是從0到65535&#xff0c;但通常建議使用1024以上的端口&#xff0c;因為0-1023的端口已…

算法與數據結構(二十四)最優子結構原理和 dp 數組遍歷方向

注&#xff1a;此文只在個人總結 labuladong 動態規劃框架&#xff0c;僅限于學習交流&#xff0c;版權歸原作者所有&#xff1b; 本文是兩年前發的 動態規劃答疑篇open in new window 的修訂版&#xff0c;根據我的不斷學習總結以及讀者的評論反饋&#xff0c;我給擴展了更多…

【STM32】高效開發工具CubeMonitor快速上手

工欲善其事必先利其器。擁有一個輔助測試工具&#xff0c;能極大提高開發項目的效率。STM32CubeMonitor系列工具能夠實時讀取和呈現其變量&#xff0c;從而在運行時幫助微調和診斷STM32應用&#xff0c;類似于一個簡單的示波器。它是一款基于流程的圖形化編程工具&#xff0c;類…

面試題:線程池的底層工作原理

線程池的幾個重要的參數&#xff1a; 1、corePoolSize&#xff1a;線程池的核心線程數&#xff08;也是默認線程數&#xff09; 2、maximumPoolSize&#xff1a;最大線程數 3、keepAliveTime&#xff1a;允許的線程最大空閑時間&#xff08;單位/秒&#xff09; 線程池內部是…

鏈表之第二回

歡迎來到我的&#xff1a;世界 該文章收入欄目&#xff1a;鏈表 希望作者的文章對你有所幫助&#xff0c;有不足的地方還請指正&#xff0c;大家一起學習交流 ! 目錄 前言第一題&#xff1a;反轉一個鏈表第二題&#xff1a;鏈表內指定區間反轉第三題&#xff1a;判斷一個鏈表…

opencv+ffmpeg+QOpenGLWidget開發的音視頻播放器demo

前言 本篇文檔的demo包含了 1.使用OpenCV對圖像進行處理&#xff0c;對圖像進行置灰&#xff0c;旋轉&#xff0c;摳圖&#xff0c;高斯模糊&#xff0c;中值濾波&#xff0c;部分區域清除置黑&#xff0c;背景移除&#xff0c;邊緣檢測等操作&#xff1b;2.單純使用opencv播放…

一個案例:Vue2組件化開發組件從入門到入土

1. 環境搭建 1.1. 創建項目 npm install -g vue/clivue create vue_study_todolist1.2. 清空項目代碼 清楚HelloWorld.Vue代碼中的內容。 1.3. 啟動空項目 1.4 項目目標 項目組件實現以下效果 2. 組件拆分代碼 Vue是一個基于組件的框架&#xff0c;允許您將界面拆分成小的…

open cv學習 (五) 圖像的閾值處理

圖像的閾值處理 demo1 # 二值化處理黑白漸變圖 import cv2 img cv2.imread("./img.png", 0) # 二值化處理 t1, dst cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) cv2.imshow("img", img) cv2.imshow("dst", dst) cv2.waitKey() cv2.des…

Golang使用MinIO

最近在使用Golang做了一個網盤項目&#xff08;學習&#xff09;&#xff0c;文件存儲一直保存在本地&#xff08;各廠商提供的oss貴&#xff09;&#xff0c;所以就在思考怎么來處理這些文件&#xff0c;類似的方案很對hdfs、fastdfs&#xff0c;但這其中MinIO是最近幾年比較火…

生信豆芽菜-差異基因富集分析的圈圖

網址&#xff1a;http://www.sxdyc.com/visualsEnrichCirplot 1、數據準備 準備一個基因集的文件 2、選擇富集分析的數據庫&#xff0c;同時輸入展示top幾的條目&#xff0c;選擇顏色&#xff0c;如果是GO的話選擇三個顏色&#xff0c;如果是KEGG選擇一個&#xff0c;如果是G…

神經網絡論文研讀-多模態方向-綜述研讀(上)

翻譯以機翻為主 原文目錄 前言 圖1&#xff1a;LMU印章&#xff08;左&#xff09;風格轉移到梵高的向日葵繪畫&#xff08;中&#xff09;并與提示混合 - 梵高&#xff0c;向日葵 -通過CLIPVGAN&#xff08;右&#xff09;。在過去的幾年中&#xff0c;自然語言處理&#xff…

微信小程序實現拖拽的小球

目錄 前言 js 獲取微信小程序中獲取系統信息 觸摸移動事件的處理函數 觸摸結束事件的處理函數 用于監聽頁面滾動事件 全局參數 html CSS 前言 小程序開發提供了豐富的API和事件處理函數&#xff0c;使得開發者可以方便地實現各種交互功能。其中&#xff0c;拖拽功能…