How to implement draggable menu in vue.js
Vue.js method to implement draggable menu: [import "@/assets/second.css";export default {name: "HelloWorld",directives: {move(el, bindings) {.. .].
The operating environment of this article: windows10 system, vue.js 2.9, thinkpad t480 computer.
Before giving the formal implementation code, we need to first understand some relevant knowledge points.
Knowledge point one:
Custom instruction directive in vue
// 注册一个全局自定义指令 `v-focus` Vue.directive('focus', { // 当被绑定的元素插入到 DOM 中时…… inserted: function (el) { // 聚焦元素 el.focus() } }) // 注册局部自定义指令 directives: { focus: { // 指令的定义 inserted: function (el) { el.focus() } } } // 在此我们用的是局部
Knowledge point two: js
onmousedown :鼠标按下事件 clientX :时鼠标指针相对于浏览器页面(或客户区)的水平坐标 document.getElementById :通过id获取节点 offsetWidth :获取的是盒子最终的宽 onmousemove :鼠标移动事件 onmouseup :鼠标释放事件
Rendering:
Page code:
<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>
Style code:
.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; }
Custom commands can be modified ,Set a minimum drag width
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; }; }; } }
Recommended learning:php training
The above is the detailed content of How to implement draggable menu in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In Windows 11, the Start menu has been redesigned and features a simplified set of apps arranged in a grid of pages, unlike its predecessor, which had folders, apps, and apps on the Start menu. Group. You can customize the Start menu layout and import and export it to other Windows devices to personalize it to your liking. In this guide, we’ll discuss step-by-step instructions for importing Start Layout to customize the default layout on Windows 11. What is Import-StartLayout in Windows 11? Import Start Layout is a cmdlet used in Windows 10 and earlier versions to import customizations for the Start menu into

When using the Vue framework to develop front-end projects, we will deploy multiple environments when deploying. Often the interface domain names called by development, testing and online environments are different. How can we make the distinction? That is using environment variables and patterns.

One of the most annoying changes that we users never want is the inclusion of "Show more options" in the right-click context menu. However, you can remove it and get back the classic context menu in Windows 11. No more multiple clicks and looking for these ZIP shortcuts in context menus. Follow this guide to return to a full-blown right-click context menu on Windows 11. Fix 1 – Manually adjust the CLSID This is the only manual method on our list. You will adjust specific keys or values in Registry Editor to resolve this issue. NOTE – Registry edits like this are very safe and will work without any issues. Therefore, you should create a registry backup before trying this on your system. Step 1 – Try it

Ace is an embeddable code editor written in JavaScript. It matches the functionality and performance of native editors like Sublime, Vim, and TextMate. It can be easily embedded into any web page and JavaScript application. Ace is maintained as the main editor for the Cloud9 IDE and is the successor to the Mozilla Skywriter (Bespin) project.

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

Foreword: In the development of vue3, reactive provides a method to implement responsive data. This is a frequently used API in daily development. In this article, the author will explore its internal operating mechanism.

In Vue.js, developers can use two different syntaxes to create user interfaces: JSX syntax and template syntax. Both syntaxes have their own advantages and disadvantages. Let’s discuss their differences, advantages and disadvantages.

In the actual development project process, sometimes it is necessary to upload relatively large files, and then the upload will be relatively slow, so the background may require the front-end to upload file slices. It is very simple. For example, 1 A gigabyte file stream is cut into several small file streams, and then the interface is requested to deliver the small file streams respectively.
