search
HomeWeb Front-endJS TutorialHandwritten mobile terminal inertial sliding & rebound Vue navigation bar component problem (detailed tutorial)

Some time ago I wrote a mobile terminal inertial sliding & rebound Vue navigation bar component ly-tab. I think it is very practical. You may use it when doing projects. Friends who are interested can learn together

Some time ago, I wrote an adaptive sliding Vue navigation bar component for the mobile terminal. I think it has certain practicality and everyone may use it (of course, if some big guys write better themselves, there is no need), so I sorted it out the past two days. After a while, it has been published to npm and GitHub. Click me to go to npm, and click me to go to the GitHub project. Students in need can npm install ly-tab -S or yarn in the project Add ly-tab is used. The specific usage will be discussed below.

Okay, let’s take a look at the results first

Okay, let’s start talking nonsense. The internship has been almost 3 months. During this time, I have been following the mentor. I have also been exposed to some projects and learned a lot. The projects I have come into contact with are basically mobile projects, and the framework mainly uses Vue. Students who have worked on mobile devices or used mobile apps (bah, bullshit) will definitely find that many times there is a tab navigation bar with a sliding effect similar to the one above. I believe you must have seen it on the homepage of the Nuggets.

Implementation ideas

The project at that time happened to have this demand, so I wanted to be lazy and use the Mint-ui component library directly There are ready-made tabbar and tab-item components. I looked at its implementation source code on github and found that it only implements the switching function but cannot slide. So, if I am too lazy, I have to write it myself.

In fact, it is not difficult to implement the tab switching function. Mint-ui actually uses v-model syntax sugar, just like the following

<ly-tab v-model="selected">
  <ly-tab-item></ly-tab-item>
</ly-tab>

The following is the disassembly of v-model syntax The implementation of sugar

<ly-tab :value="selected" @input="selected = arguments[0]">
  <ly-tab-item></ly-tab-item>
</ly-tab>

Then you only need to implement it in the tab-item component. When it is clicked, let its parent component, which is the ly-tab component, $emit an input event, and pass in an identification event for each tab. The unique value of -item is used as the first parameter. Regarding this unique value, mint-ui requires the user to manually pass in a unique id value to each tab-item through props. The following is the demo implementation of Mint UI:

<mt-tabbar v-model="selected">
  <mt-tab-item id="订单">
    <img  src="/static/imghwm/default1.png"  data-src="http://placehold.it/100x100"  class="lazy"  slot="icon"  alt="Handwritten mobile terminal inertial sliding & rebound Vue navigation bar component problem (detailed tutorial)" >
    <span slot="label">订单</span>
  </mt-tab-item>
 </mt-tabbar>

However, after reading the boss’s thoughts on designing the Tabbar plug-in in vue, I feel that the approach in the article is better, because for the parent component , As long as you know which <ly-tab-item></ly-tab-item> is clicked, then I put the index of each <ly-tab-item></ly-tab-item> component Wouldn't it be enough to use the index value as its unique identification value?

Then the question is: How to get its own index value inside the tab-item component?

First of all, the $children of the ly-tab component is an array. Since each <ly-tab-item></ly-tab-item> component is created sequentially and inserted into the array by push, so each <ly-tab-item></ly-tab-item> component is created and pushed to $children , for the <ly-tab-item></ly-tab-item> component (this.$parent.$children.length || 1) - 1 Isn’t it just every The only index value of the component. In fact, the click-to-switch function can already be implemented here. Paste the code in tab-item.vue below:

tab-item.vue

<template>
 <a class="ly-tab-item"
    :style="$parent.value === id ? activeStyle : {}"
    @click="$parent.$emit(&#39;input&#39;, id)">
  <p class="ly-tab-item-icon"><slot name="icon"></slot></p>
  <p class="ly-tab-item-label"><slot></slot></p>
 </a>
</template>
<script>
export default {
 name: &#39;LyTabItem&#39;,
 computed: {
  activeStyle () {
   return {
    color: this.$parent.activeColor,
    borderColor: this.$parent.activeColor,
    borderWidth: this.$parent.lineWidth,
    borderBottomStyle: &#39;solid&#39;
   }
  }
 },
 data () {
  return {
   id: (this.$parent.$children.length || 1) - 1
  }
 }
}
</script>
<style lang="scss">
.ly-tab-item {
 text-decoration: none;
 text-align: center;
 .ly-tab-item-icon {
  margin: 0 auto 5px;
 }
 .ly-tab-item-label {
  margin: 0 auto 10px;
  line-height: 18px;
 }
}
</style>

Regarding the implementation of touch sliding, inertial sliding and rebound effects in tab.vue, there is no discussion here. The method is explained in detail. Interested friends can check it out on github. Click here to check out the project on github. If you want to see a sample demo, you can clone the project locally and run it. You are welcome to correct me if it is not well written. If you think it is useful It would be best if you can get it or be able to help everyone, so you might as well give it a star, haha...

Hey, hey, that’s not right, why did you start asking for a star? The most important thing is I haven’t talked about it yet—how to use ly-tab?

How to use ly-tab

If you want to use ly-tab, you need to download it through npm or yarn in your project Installation:

npm install ly-tab -S
or
yarn add ly-tab

Then import it globally in main.js:

import Vue from &#39;vue&#39;;
import LyTab from &#39;ly-tab&#39;;
Vue.use(LyTab);

Then you can use it in your project <ly-tab></ly-tab>&gt ; and <ly-tab-item></ly-tab-item> component without the need to introduce it again

chestnut

<ly-tab v-model="selected" fixBottom>
 <!-- selected是你自己定义的一个在data中用于存放当前tab-item的索引值的变量 -->
 <ly-tab-item v-for="(item, index) in tabList" :key="index">
  {{item.itemName}}
 </ly-tab-item>
</ly-tab>

The chestnut above is actually just the implementation of tabbar. You definitely need to switch the view area in your project. Here I will briefly talk about my current approach:

  • Using Vue -router switches router-view

  • Use dynamic components (can be used with asynchronous components)

I seem to have only used this for the time being Two kinds. I don’t know if you have any other better methods. Welcome to share~

Configuration items

can be given to<ly-tab>&lt ;/ly-tab> </ly-tab>The component passes in some configuration items to customize the effect you want

##reBoundExponentNumberInertial rebound index (the larger the value , the greater the amplitude, the longer the inertia rebound distance)10sensitivityNumberSensitivity during inertial sliding (The smaller the value, the greater the resistance). It can be approximated that the time required for the speed to reduce to zero after the hand is released is 1000msreBoundingDuration NumberRebound animation duration360ms
Configuration item Type Description Default value
lineWidth Number fixBottom is false when the tabbar bottom border-width 1px
activeColor String Activated font color and border-bottom-color red
fixBottom Boolean Whether it is fixed at the bottom of the view (cannot slide when false) false
additionalX Number Approximately Equal to the maximum draggable distance when exceeding the boundary 50px
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

In the vue project, use axios cross-domain processing

Vue-cli project to obtain local json file data Example

Using import and require in JavaScript to package and implement principle analysis

The above is the detailed content of Handwritten mobile terminal inertial sliding & rebound Vue navigation bar component problem (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

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.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor