search
HomeWeb Front-enduni-appHow to realize sliding up and hiding navigation effect in uniapp

In mobile APP design, the navigation bar is a very important element. It allows users to quickly locate the information they need, and is also an important visual element of an APP. However, during the design process, we not only need to consider the style and layout of the navigation bar, but also pay attention to its performance and interaction effects in different scenarios.

It is relatively easy to achieve the effect of sliding down and hiding navigation in uniapp. We only need to use some simple styles and JS code to achieve it.

First, add a container to the navigation area of ​​the page, set its position attribute to fixed, and its z-index attribute to a higher value so that it is always at the top of the page. In order to hide the navigation after sliding down, we can use the transform attribute to move the navigation bar out of the visible range of the screen.

The specific implementation steps are as follows:

  1. Add a navigation container named header, and add the style and layout of the navigation in it.
  2. Set the initial position of the navigation container to the top of the screen.
  3. Use JS to listen to screen scroll events. When the user slides down, the navigation container moves out of the visible range of the screen. Conversely, when the user swipes up, the navigation container returns to the top of the screen.

The specific code implementation is as follows:

// html文件代码
<template>
  <div>
    <header>
      <!--导航内容-->
    </header>
    <!--主要内容区域-->
  </div>
</template>

<style>
  header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 50px;
    background-color: #fff;
    z-index: 999;
    transition: all .3s ease-in-out; // 添加过渡效果
  }
  
  header.hide {
    transform: translateY(-100%);
  }
</style>

<script>
  export default {
    data() {
      return {
        lastScrollTop: 0,
        isHeaderShow: true
      }
    },
    mounted() {
      window.addEventListener(&#39;scroll&#39;, this.onScroll)
    },
    destroyed() {
      window.removeEventListener(&#39;scroll&#39;, this.onScroll)
    },
    methods: {
      onScroll(e) {
        const currentScrollTop = document.documentElement.scrollTop
        if (currentScrollTop > 0 && currentScrollTop > this.lastScrollTop) {
          this.isHeaderShow = false
        } else {
          this.isHeaderShow = true
        }
        this.lastScrollTop = currentScrollTop
      }
    },
    computed: {
      headerClass() {
        return {
          hide: !this.isHeaderShow
        }
      }
    }
  }
</script>

In the above code, we use the isHeaderShow variable to mark whether the current navigation bar should be displayed, and use computed properties to bind the navigation container Style, and add a scroll event listener in the JS method to control whether the navigation bar is displayed or not.

It should be noted that in order for the navigation bar to operate correctly, we need to set the height of the navigation container and ensure that there is sufficient spacing at the head of the main content area, otherwise the navigation bar will overlap the content area. Condition.

In short, by setting the position to fixed, adding transition effects and transform attributes to the navigation container, and then controlling the movement of the navigation container through scroll event listening, we can very simply achieve the effect of sliding down to hide the navigation in uniapp.

The above is the detailed content of How to realize sliding up and hiding navigation effect in uniapp. 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
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools