vue.js實作可拖曳選單的方法:【import "@/assets/second.css";export default {name: "HelloWorld",directives: {move(el, bindings) {.. .】。
本文操作環境:windows10系統、vue.js 2.9、thinkpad t480電腦。
在給出正式的實作程式碼之前,我們要先來了解一點相關知識點。
知識點一:
vue中的自訂指令directive
// 注册一个全局自定义指令 `v-focus` Vue.directive('focus', { // 当被绑定的元素插入到 DOM 中时…… inserted: function (el) { // 聚焦元素 el.focus() } }) // 注册局部自定义指令 directives: { focus: { // 指令的定义 inserted: function (el) { el.focus() } } } // 在此我们用的是局部
知識點二:js
onmousedown :鼠标按下事件 clientX :时鼠标指针相对于浏览器页面(或客户区)的水平坐标 document.getElementById :通过id获取节点 offsetWidth :获取的是盒子最终的宽 onmousemove :鼠标移动事件 onmouseup :鼠标释放事件
效果圖:
#頁面程式碼:
<template> <el-container> <el-main> <div class="myBox"> <div id="silderLeft"> <div class="menuList">侧栏菜单区</div> <div class="moveBtn" v-move></div> </div> <div class="silderRight">右边自适应大小,黄色的为拖拽的按钮</div> </div> </el-main> </el-container> </template> <script> import "@/assets/second.css"; export default { name: "HelloWorld", directives: { move(el, bindings) { el.onmousedown = function(e) { var init = e.clientX; var parent = document.getElementById("silderLeft"); var initWidth = parent.offsetWidth; document.onmousemove = function(e) { var end = e.clientX; var newWidth = end - init + initWidth; parent.style.width = newWidth + "px"; }; document.onmouseup = function() { document.onmousemove = document.onmouseup = null; }; }; } }, data() { return { msg: "我是第二页" }; }, methods: {}, mounted() {}, created() {}, updated() {} }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> </style>
樣式程式碼:
.myBox{ width: 100%;; height: 700px; border: 1px solid red; display: flex; } #silderLeft{ width: 250px; height: 100%; background-color: #999; position: relative; /* overflow-y: auto; */ } /* 拖动条 */ .moveBtn{ height: 100%; width: 10px; /* opacity: 0; */ position: absolute; right: 0px; top: 0; cursor: col-resize; background-color: yellow; } .menuList{ background-color: yellowgreen; /* height: 120%; */ } .silderRight{ height: 100%; background-color: sandybrown; flex: 1; }
可以修改自訂指令,設定一個最小拖曳寬度
directives: { move(el, bindings) { el.onmousedown = function(e) { var init = e.clientX; console.log('init',init); var parent = document.getElementById("sidebar"); var initWidth = parent.offsetWidth; document.onmousemove = function(e) { var end = e.clientX; // end - init表示鼠标移动的距离 // end为鼠标移动的宽度,可用于设置最小宽度 if(end > 250){ var newWidth = end - init + initWidth; parent.style.width = newWidth + "px"; }else{ end = 250; // 最小宽度242 parent.style.width = 242 + "px"; } }; document.onmouseup = function() { document.onmousemove = document.onmouseup = null; }; }; } }
推薦學習:php訓練
#以上是vue.js如何實作可拖曳選單的詳細內容。更多資訊請關注PHP中文網其他相關文章!

VueUse 是 Anthony Fu 的一个开源项目,它为 Vue 开发人员提供了大量适用于 Vue 2 和 Vue 3 的基本 Composition API 实用程序函数。本篇文章就来给大家分享几个我常用的几个 VueUse 最佳组合,希望对大家有所帮助!

Vue3如何更好地使用qrcodejs生成二维码并添加文字描述?下面本篇文章给大家介绍一下Vue3+qrcodejs生成二维码并添加文字描述,希望对大家有所帮助。

如何使用VueRouter4.x?下面本篇文章就来给大家分享快速上手教程,介绍一下10分钟快速上手VueRouter4.x的方法,希望对大家有所帮助!

本篇文章给大家整理分享8个GitHub上很棒的的 Vue 项目,都是非常棒的项目,希望当中有您想要收藏的那一个。

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)