search
HomeWeb Front-endJS TutorialHow to implement inertial sliding & rebound Vue navigation bar on mobile terminal

This time I will show you how to implement the inertial sliding & rebounding Vue navigation bar on the mobile terminal. What are the precautions for realizing the inertial sliding & rebounding Vue navigation bar on the mobile terminal. The following is a practical case. Let’s take a look.

I wrote an adaptive sliding Vue navigation bar component for mobile some time ago. I think it has certain practicality and everyone may use it (of course there is no need if some big guys write better themselves), so I sorted it out two days ago and it has been published to npm and GitHub. Click me to go to npm and click me to the GitHub project. Students in need can npm install ly-tab -S or yarn add ly-tab Use, 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>
  <ly-tab-item></ly-tab-item>
</ly-tab>

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

<ly-tab>
  <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>
  <mt-tab-item>
    <img  src="/static/imghwm/default1.png" data-src="http://placehold.it/100x100" class="lazy" alt="How to implement inertial sliding & rebound Vue navigation bar on mobile terminal" >
    <span>订单</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>
  <p><slot></slot></p>
  <p><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>
.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 me 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 'vue';
import LyTab from 'ly-tab';
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>
 <!-- selected是你自己定义的一个在data中用于存放当前tab-item的索引值的变量 -->
 <ly-tab-item>
  {{item.itemName}}
 </ly-tab-item>
</ly-tab>

上面的栗子其实只是tabbar的实现,大家项目中肯定还需要做视图区的切换,在这里简单说一下我目前的做法:

  • 使用Vue-router做router-view的切换

  • 使用动态组件(可以配合异步组件使用)

我暂时的话好像只用过这两种,不知道大家还有其他什么更好的方法,欢迎分享~

配置项

可以给 <ly-tab></ly-tab> 组件传入一些配置项以自定义你想要的效果

配置项 类型 描述 默认值
lineWidth Number fixBottom为false时tabbar底部border-width 1px
activeColor String 激活状态下字体color以及border-bottom-color red
fixBottom Boolean 是否固定在视图底部(为false时不可滑动) false
additionalX Number 近似等于超出边界时最大可拖动距离 50px
reBoundExponent Number 惯性回弹指数(值越大,幅度越大,惯性回弹距离越长) 10
sensitivity Number 惯性滑动时的灵敏度(值越小,阻力越大),可近似认为手松开后速度减为零所需时间 1000ms
reBoundingDuration Number 回弹动画duration 360ms

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读

解决vue2.0路由不显示router-view的问题

vue.js怎么在标签属性中插入变量参数

The above is the detailed content of How to implement inertial sliding & rebound Vue navigation bar on mobile terminal. 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
Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

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.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

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's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

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: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

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.

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.

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor