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:
2. Code implementation
Add the navigation bar component in the wxml file:
<view class="navbar">导航栏内容</view>
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; }
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', }) } } })
3. Use example
Configure the page path and window style in app.json:
{ "pages": [ "pages/index/index" ], "window": { "navigationBarTitleText": "导航栏固定效果示例" } }
Add the navigation bar component in pages/index/index.wxml:
<view class="navbar">导航栏内容</view>
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; }
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', }) } } })
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!