


The UI framework of vue mobile terminal realizes the side menu plug-in effect
This time I will bring you the UI framework of vue mobile terminal to implement the side menu plug-in effect. What are the precautions of the UI framework of vue mobile terminal to implement the side menu plug-in. The following is a practical case. , let’s take a look.
In recent interviews, I found that many front-end programmers have never had experience in writing plug-ins, and they basically work on Baidu. So I plan to write a series of articles to teach brothers who have never written components how to write plug-ins step by step. This series of articles are all based on VUE, and the core content is the same. After understanding it, you can quickly rewrite it into components such as react, angular, or small programs. This article is the first one, and it is about a side menu component similar to QQ.
Start making
DOM structure
There should be two containers in the overall structure: 1. Menu container 2. Main page container; therefore the current DOM structure is as follows:
<template> <p> </p> <p></p> <p></p> </template>
In order to make the menu content and theme content customizable, we add two slots to the two containers: the main content is placed in the default slot, and the menu is placed in the menu slot:
<template> <p> </p> <p> <slot></slot> </p> <p> <slot></slot> </p> </template>
css style
I used scss in my project, the code is as follows:
<style> @mixin one-screen { position: absolute; left:0; top:0; width:100%; height:100%; overflow: hidden; } .r-slide-menu{ @include one-screen; &-wrap, &-content{ @include one-screen; } &-transition{ -webkit-transition: transform .3s; transition: transform .3s; } } </style>
At this point we have two absolutely positioned containers
Now let’s start the formal code writing. First, let’s clarify the interaction logic:
When your finger slides left and right, both the main container and the menu container move with the finger movement
When the finger movement distance exceeds the width of the menu container, the page cannot continue to slide to the right
When the finger moves to the left so that the moving distance between the menu and the page returns to zero, the page cannot continue to move to the left
When the finger is released from the screen, if the page slides beyond a certain distance (proportion of the entire menu width), the entire menu will be opened. If it is less than a certain distance, the menu will be closed.
So now we need to be able to enter parameters when using the component to customize the menu width and the ratio of the critical value that triggers the menu to close and the menu width. At the same time, we need to add touch events to the main container. Finally, we add separate additions to the menu container and the main container. A style that controls their movement. Control the movement of the container by controlling this style
<template> <p> </p> <p> <slot></slot> </p> <p> <slot></slot> </p> </template> <script> export default { props: { width: { type: String, default: '250' }, ratio: { type: Number, default: 2 } }, data () { return { isMoving: false, transitionClass: '', startPoint: { X: 0, y: 0 }, oldPoint: { x: 0, y: 0 }, move: { x: 0, y: 0 } } }, computed: { wrapStyle () { let style = { width: `${this.width}px`, left: `-${this.width / this.ratio}px`, transform: `translate3d(${this.move.x / this.ratio}px, 0px, 0px)` } return style }, contentStyle () { let style = { transform: `translate3d(${this.move.x}px, 0px, 0px)` } return style } }, methods: { touchstart (e) {}, touchmove (e) {}, touchend (e) {} } }</script>
Next, let’s implement our core touch event processing function. The logic of the event is as follows:
The moment the finger is pressed, the point currently touched by the finger and the position of the current main container are recorded
When the finger moves, the position of the moving point is obtained
Calculate the X and Y axis distance of the current finger point movement. If the X movement distance is greater than the Y movement distance, it is determined as a horizontal movement, otherwise it is a vertical movement
If it moves horizontally, it is judged that the current movement distance is within a reasonable movement range (0 to menu width). If so, the positions of the two containers are changed (preventing other events in the page from being triggered during the movement)
Finger leaves the screen: If the cumulative movement distance exceeds the critical value, use animation to open the menu, otherwise close the menu
touchstart (e) { this.oldPoint.x = e.touches[0].pageX this.oldPoint.y = e.touches[0].pageY this.startPoint.x = this.move.x this.startPoint.y = this.move.y this.setTransition() }, touchmove (e) { let newPoint = { x: e.touches[0].pageX, y: e.touches[0].pageY } let moveX = newPoint.x - this.oldPoint.x let moveY = newPoint.y - this.oldPoint.y if (Math.abs(moveX) = this.width) { this.move.x = this.width } else if (moveX this.width / this.ratio) ? this.width : 0 }, setTransition (isTransition = false) { this.transitionClass = isTransition ? 'r-slide-menu-transition' : '' }
Above, there is a setTransition in this core code Function, the function of this function is to add a transition attribute to the container element when the finger leaves, so that the container has a transition animation to complete the closing or opening animation; therefore, the transition attribute on the container needs to be removed at the moment the finger is pressed down, to avoid There is a bad experience of delayed container and finger sliding during sliding. As a final reminder, the reason why translate3d is used instead of translate in the code is to activate the 3D acceleration of animation on mobile phones and improve the smoothness of animation. The final code is as follows:
<template> <p> </p> <p> <slot></slot> </p> <p> <slot></slot> </p> </template> <script> export default { props: { width: { type: String, default: '250' }, ratio: { type: Number, default: 2 } }, data () { return { isMoving: false, transitionClass: '', startPoint: { X: 0, y: 0 }, oldPoint: { x: 0, y: 0 }, move: { x: 0, y: 0 } } }, computed: { wrapStyle () { let style = { width: `${this.width}px`, left: `-${this.width / this.ratio}px`, transform: `translate3d(${this.move.x / this.ratio}px, 0px, 0px)` } return style }, contentStyle () { let style = { transform: `translate3d(${this.move.x}px, 0px, 0px)` } return style } }, methods: { touchstart (e) { this.oldPoint.x = e.touches[0].pageX this.oldPoint.y = e.touches[0].pageY this.startPoint.x = this.move.x this.startPoint.y = this.move.y this.setTransition() }, touchmove (e) { let newPoint = { x: e.touches[0].pageX, y: e.touches[0].pageY } let moveX = newPoint.x - this.oldPoint.x let moveY = newPoint.y - this.oldPoint.y if (Math.abs(moveX) < Math.abs(moveY)) return false e.preventDefault() this.isMoving = true moveX = this.startPoint.x * 1 + moveX * 1 moveY = this.startPoint.y * 1 + moveY * 1 if (moveX >= this.width) { this.move.x = this.width } else if (moveX <= 0) { this.move.x = 0 } else { this.move.x = moveX } }, touchend (e) { this.setTransition(true) this.isMoving = false this.move.x = (this.move.x > this.width / this.ratio) ? this.width : 0 }, // 点击切换 switch () { this.setTransition(true) this.move.x = (this.move.x === 0) ? this.width : 0 }, setTransition (isTransition = false) { this.transitionClass = isTransition ? 'r-slide-menu-transition' : '' } } } </script> <style> @mixin one-screen { position: absolute; left:0; top:0; width:100%; height:100%; overflow: hidden; } .r-slide-menu{ @include one-screen; &-wrap, &-content{ @include one-screen; } &-transition{ -webkit-transition: transform .3s; transition: transform .3s; } } </style>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
How to reload the routing page in AngularJS
Why is element-ui binding @keyup event invalid?
The above is the detailed content of The UI framework of vue mobile terminal realizes the side menu plug-in effect. For more information, please follow other related articles on the PHP Chinese website!

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
