Home Web Front-end uni-app How to use uniapp to develop navigation bar scrolling effect

How to use uniapp to develop navigation bar scrolling effect

Jul 04, 2023 pm 04:30 PM
uniapp Navigation Bar scrolling effect

How to use uniapp to develop navigation bar scrolling effect

In the process of mobile application development, the navigation bar is a very important component. It not only provides the switching function between pages, but also acts as the interface Signage and guidance. Adding a scrolling effect to the navigation bar can further enhance the user experience and make the application more attractive. This article will introduce how to use uniapp to develop navigation bar scrolling effects, and attach relevant code examples.

uniapp is a cross-platform application framework developed based on Vue.js. Through it, applications for small programs, H5, App and other platforms can be developed at the same time. In uniapp, the scrolling effect can be achieved by modifying the style and position of the navigation bar. The following are detailed steps:

Step 1: Create a uniapp project
First, install the uni-app development tools on your computer, and then create a new uniapp project:

$ vue create -p dcloudio/uni-preset-vue my-project
Copy after login

Step 2: Modify the style of the navigation bar
In uniapp, you can modify the style of the navigation bar by modifying the pages.json file. Open the pages.json file, find the page where you want to add a scrolling effect, and then add the following code to it:

"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#F56C6C",
"navigationBarTitleText": "我的页面",
"onReachBottomDistance": 50,
"disableScroll": true
Copy after login

In the above code, navigationBarTextStyle represents the font color of the navigation bar, navigationBarBackgroundColor represents the background color of the navigation bar, and navigationBarTitleText represents The title text of the navigation bar, onReachBottomDistance means that the event is triggered when the scroll reaches 50 pixels from the bottom, and disableScroll means that the scrolling effect of the page is disabled.

Step 3: Listen to the scrolling event of the page
In uniapp, you can achieve the scrolling effect of the navigation bar by listening to the scrolling event of the page. Find the vue file of the page where you want to add a scrolling effect, and add the following code to it:

export default {
    data() {
        return {
            scrollTop: 0
        }
    },
    methods: {
        onPageScroll(event) {
            this.scrollTop = event.scrollTop
        }
    }
}
Copy after login

In the above code, a scrollTop variable is defined in the data method to save the scrolling distance of the page. An onPageScroll method is defined in the methods method to monitor the scrolling event of the page and assign the scrolling distance to the scrollTop variable.

Step 4: Modify the position of the navigation bar
In uniapp, you can achieve the scrolling effect by modifying the position of the navigation bar. Find the vue file of the page where you want to add a scrolling effect, and add the following code to the navigation bar component of the page:

<navigation-bar
    title="我的页面"
    :fixed="scrollTop > 0"
    :style="{ top: scrollTop + 'px' }"
></navigation-bar>
Copy after login

In the above code, title represents the title text of the navigation bar, and the :fixed attribute determines whether scrollTop is greater than 0 To set the fixed position of the navigation bar, the :style attribute modifies the position of the navigation bar by assigning scrollTop to top.

So far, you have successfully developed the navigation bar scrolling effect using uniapp. By modifying the style and position of the navigation bar and listening to page scroll events, you can further optimize the user experience of your application and increase its appeal. Hope this article is helpful to you.

Code example:

pages.json file

{
  "pages": [
    {
      "path": "/pages/index/index",
      "navigationBarTitleText": "首页",
      "disableScroll": true
    },
    {
      "path": "/pages/my/my",
      "navigationBarTitleText": "我的页面",
      "disableScroll": true
    }
  ],
  "globalStyle": {
    "navigationBarTextStyle": "white",
    "navigationBarBackgroundColor": "#F56C6C",
    "onReachBottomDistance": 50
  },
  "tabBar": {
    "list": [
      {
        "text": "首页",
        "pagePath": "/pages/index/index",
        "iconPath": "static/tabbar/home.png",
        "selectedIconPath": "static/tabbar/home-active.png"
      },
      ...
    ]
  }
}
Copy after login

my.vue file

<template>
  <view class="container">
    <navigation-bar
      title="我的页面"
      :fixed="scrollTop > 0"
      :style="{ top: scrollTop + 'px' }"
    ></navigation-bar>
    <scroll-view
      :scroll-y="true"
      :style="{ height: windowHeight + 'px' }"
      @scroll="onPageScroll"
    >
      <!-- 页面内容 -->
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      scrollTop: 0,
      windowHeight: 0
    }
  },
  methods: {
    onPageScroll(e) {
      this.scrollTop = e.detail.scrollTop
    },
    getWindowHeight() {
      uni.getSystemInfo({
        success: (res) => {
          this.windowHeight = res.windowHeight
        }
      })
    }
  },
  mounted() {
    this.getWindowHeight()
  }
}
</script>

<style>
.container {
  position: relative;
}
</style>
Copy after login

The above are the complete steps and steps for using uniapp to develop the navigation bar scrolling effect. Code examples. Through the following steps, you can easily implement the scrolling effect of the navigation bar to improve the user experience and application attractiveness. Hope this article is helpful to you.

The above is the detailed content of How to use uniapp to develop navigation bar scrolling effect. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to start preview of uniapp project developed by webstorm How to start preview of uniapp project developed by webstorm Apr 08, 2024 pm 06:42 PM

Steps to launch UniApp project preview in WebStorm: Install UniApp Development Tools plugin Connect to device settings WebSocket launch preview

Which one is better, uniapp or mui? Which one is better, uniapp or mui? Apr 06, 2024 am 05:18 AM

Generally speaking, uni-app is better when complex native functions are needed; MUI is better when simple or highly customized interfaces are needed. In addition, uni-app has: 1. Vue.js/JavaScript support; 2. Rich native components/API; 3. Good ecosystem. The disadvantages are: 1. Performance issues; 2. Difficulty in customizing the interface. MUI has: 1. Material Design support; 2. High flexibility; 3. Extensive component/theme library. The disadvantages are: 1. CSS dependency; 2. Does not provide native components; 3. Small ecosystem.

What development tools do uniapp use? What development tools do uniapp use? Apr 06, 2024 am 04:27 AM

UniApp uses HBuilder

What basics are needed to learn uniapp? What basics are needed to learn uniapp? Apr 06, 2024 am 04:45 AM

uniapp development requires the following foundations: front-end technology (HTML, CSS, JavaScript) mobile development knowledge (iOS and Android platforms) Node.js other foundations (version control tools, IDE, mobile development simulator or real machine debugging experience)

What are the disadvantages of uniapp What are the disadvantages of uniapp Apr 06, 2024 am 04:06 AM

UniApp has many conveniences as a cross-platform development framework, but its shortcomings are also obvious: performance is limited by the hybrid development mode, resulting in poor opening speed, page rendering, and interactive response. The ecosystem is imperfect and there are few components and libraries in specific fields, which limits creativity and the realization of complex functions. Compatibility issues on different platforms are prone to style differences and inconsistent API support. The security mechanism of WebView is different from native applications, which may reduce application security. Application releases and updates that support multiple platforms at the same time require multiple compilations and packages, increasing development and maintenance costs.

How to adjust the navigation bar at the top of Douyin? Other navigation bar adjustment options How to adjust the navigation bar at the top of Douyin? Other navigation bar adjustment options Mar 07, 2024 pm 02:50 PM

The navigation bar of the Douyin interface is located at the top and is an important channel for users to quickly access different functions and content. As Douyin continues to update, users may want to be able to customize and adjust the navigation bar according to their personal preferences and needs. 1. How to adjust the navigation bar at the top of Douyin? Usually, the top navigation bar of Douyin displays some popular channels, allowing users to quickly browse and view content of interest. If you want to adjust the settings for your top channel, just follow these steps: Open the TikTok app and log into your account. Find the navigation bar above the main interface, usually in the middle or top of the screen. Click the "+" symbol or similar button above the navigation bar to enter the channel editing interface. In the channel editing interface, you can see the default list of popular channels. You can pass

Which is better, uniapp or native development? Which is better, uniapp or native development? Apr 06, 2024 am 05:06 AM

When choosing between UniApp and native development, you should consider development cost, performance, user experience, and flexibility. The advantages of UniApp are cross-platform development, rapid iteration, easy learning and built-in plug-ins, while native development is superior in performance, stability, native experience and scalability. Weigh the pros and cons based on specific project needs. UniApp is suitable for beginners, and native development is suitable for complex applications that pursue high performance and seamless experience.

What is the difference between uniapp and flutter What is the difference between uniapp and flutter Apr 06, 2024 am 04:30 AM

UniApp is based on Vue.js, and Flutter is based on Dart. Both support cross-platform development. UniApp provides rich components and easy development, but its performance is limited by WebView; Flutter uses a native rendering engine, which has excellent performance but is more difficult to develop. UniApp has an active Chinese community, and Flutter has a large and global community. UniApp is suitable for scenarios with rapid development and low performance requirements; Flutter is suitable for complex applications with high customization and high performance.

See all articles