Home > Web Front-end > HTML Tutorial > Use WeChat applet to achieve navigation bar fixed effect

Use WeChat applet to achieve navigation bar fixed effect

王林
Release: 2023-11-21 08:18:40
Original
1678 people have browsed it

Use WeChat applet to achieve navigation bar fixed effect

WeChat Mini Program is a platform for rapid application development, which provides rich development capabilities on the mobile side. Among them, realizing the fixed effect of the navigation bar is a common requirement. This article will introduce how to use the WeChat applet to realize the fixed effect of the navigation bar and provide specific code examples.

1. Requirements Analysis
The fixed effect of the navigation bar is that when the page is scrolled, the navigation bar always remains at the top of the page. Implementing the navigation bar fixed effect requires the following steps:

  1. Add a navigation bar component at the top of the page.
  2. Listen to page scroll events and dynamically change the style of the navigation bar during scrolling so that it is fixed at the top of the page.

2. Code implementation

  1. Add the navigation bar component in the wxml file:

    <view class="navbar">导航栏内容</view>
    Copy after login
    Copy after login
  2. In the wxss file Set the initial style and fixed style of the navigation bar in:

    .navbar {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 50px;
      background-color: #FFFFFF;
      z-index: 99;
    }
    Copy after login
    Copy after login
  3. Add the code for scrolling event monitoring and dynamically modifying the navigation bar style in the js file:

    Page({
      onPageScroll: function (e) {
     if (e.scrollTop > 0) {
       wx.setNavigationBarColor({
         frontColor: '#000000',
         backgroundColor: '#FFFFFF',
       })
     } else {
       wx.setNavigationBarColor({
         frontColor: '#FFFFFF',
         backgroundColor: '#FFFFFF',
       })
     }
      }
    })
    Copy after login
    Copy after login

3. Use example

  1. Create a new WeChat applet project.
  2. Configure the page path and window style in app.json:

    {
      "pages": [
     "pages/index/index"
      ],
      "window": {
     "navigationBarTitleText": "导航栏固定效果示例"
      }
    }
    Copy after login
  3. Add the navigation bar component in pages/index/index.wxml:

    <view class="navbar">导航栏内容</view>
    Copy after login
    Copy after login
  4. Set the style of the navigation bar in pages/index/index.wxss:

    .navbar {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 50px;
      background-color: #FFFFFF;
      z-index: 99;
    }
    Copy after login
    Copy after login
  5. Add scrolling in pages/index/index.js Code for event listening and dynamically modifying the navigation bar style:

    Page({
      onPageScroll: function (e) {
     if (e.scrollTop > 0) {
       wx.setNavigationBarColor({
         frontColor: '#000000',
         backgroundColor: '#FFFFFF',
       })
     } else {
       wx.setNavigationBarColor({
         frontColor: '#FFFFFF',
         backgroundColor: '#FFFFFF',
       })
     }
      }
    })
    Copy after login
    Copy after login
  6. Run the applet. When the page scrolls, the navigation bar will be fixed at the top of the page, and the text color of the navigation bar will change when scrolling.

The above is the detailed content of Use WeChat applet to achieve navigation bar fixed effect. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template