您当前的位置: 首页 > 解决方案

微信小程序电商实战-你所困扰的自定义顶部导航栏都在这里

  • 作者: admin
  • 发布于 2018-12-18 09:46:53
  • 来源:  
  • 栏目:解决方案

导语: 本文章是一个系列文章,以一个完整的可用于生产的实际项目探索微信小程序开发中我们经常会遇到的问题,希望能提供完美的

 

11.jpg

本文章是一个系列文章,以一个完整的可用于生产的实际项目探索微信小程序开发中我们经常会遇到的问题,希望能提供完美的解决方案,这次是本系列文章的第二篇了,一下列出该系列文章链接。

微信小程序及h5,基于taro,zoro最佳实践探索 
微信小程序电商实战-解决你的登陆难问题

微信自6.6.0版本之后提供了自定义底部导航栏的功能,这使得我们的全屏页面设计成为了可能 
首先演示下最终的实现效果

 

我们实现了一个与微信之前的导航栏行为基本一致,样式可自定义的导航栏,接下来让我们一步一步实现它,这里主要需要考虑如下几点

不同的手机,状态栏高度不同,需要进行相关适配 
当开启小程序下拉刷新时,如何让顶部导航不会跟着下拉 
自定义导航栏封装成独立组件,实现仅需引入到页面,无需对页面样式做相关适配工作

该项目托管于github,有兴趣的可以直接查看源码,weapp-clover,如何运行项目源码请查看ztaro 
要想实现自定义导航,首先我们需要配置navigationStyle为custom(src/app.js)

 
  1. config = {
  2. window: {
  3. navigationStyle: 'custom'
  4. }
  5. }

再实际情况中,我们往往需要对自定义导航进行各种各样的定制化,因此我们希望,封装一个最基本的导航栏,用于解决适配问题,其他样式的导航栏仅需对其进行二次封装,无需在关心适配问题,对于这个项目,我们封装组件如下: 
ComponentBaseNavigation 导航栏基本组件,用于解决适配问题 
ComponentHomeNavigation 引入基本导航组件,定制化首页导航栏组件 
ComponentCommonNavigation 引入基本导航组件,定制化其他页面导航组件

 

ComponentBaseNavigation实现

对于适配不通手机顶部的状态栏高度,我们需要利用微信wx.getSystemInfo获取状态栏的高度,因此在user model中新增如下代码(src/models/user.js)

 
  1. // 省略其他无关代码
  2. import Taro from '@tarojs/taro'
  3.  
  4. export default {
  5. namespace: 'user',
  6.  
  7. mixins: ['common'],
  8.  
  9. state: {
  10. systemInfo: {},
  11. },
  12.  
  13. async setup({ put }) {
  14. // 新增初始化获取用户手机系统相关信息,存储到redux全局状态中
  15. Taro.getSystemInfo().then(systemInfo =>
  16. put({ type: 'update', payload: { systemInfo } }),
  17. )
  18. }
  19. }

实现组件逻辑(src/components/base/navigation/navigation.js)

 
  1. import Taro, { Component } from '@tarojs/taro'
  2. import { View } from '@tarojs/components'
  3. import { connect } from '@tarojs/redux'
  4.  
  5. import './navigation.scss'
  6.  
  7. @connect(({ user }) => ({
  8. // 链接redux中存储的状态栏高度到组件中
  9. statusBarHeight: user.systemInfo.statusBarHeight,
  10. }))
  11. class ComponentBaseNavigation extends Component {
  12. static defaultProps = {
  13. color: 'white',
  14. backgroundColor: '#2f3333',
  15. }
  16.  
  17. render() {
  18. const { statusBarHeight, backgroundColor, color } = this.props
  19.  
  20. const barStyle = {
  21. paddingTop: `${statusBarHeight}px`,
  22. backgroundColor,
  23. color,
  24. }
  25.  
  26. return (
  27. <View className="navigation">
  28. <View className="bar" style={barStyle}>
  29. {this.props.children}
  30. </View>
  31. <View className="placeholder" style={barStyle} />
  32. </View>
  33. )
  34. }
  35. }

export default ComponentBaseNavigation

 
  1. 样式如下(src/components/base/navigation.scss)
  2. // 大写的PX单位是为了告诉Taro,不要转换成单位rpx
  3. // 通过测试和观察发现,微信顶部的胶囊宽高如下,并且各个屏幕下一致
  4. // 因此采用PX单位
  5. $capsule-padding: 6PX; // 胶囊的上下padding距离
  6. $capsule-height: 32PX; // 胶囊的高度
  7. $capsule-width: 88PX; // 胶囊的宽度
  8.  
  9. $navigation-height: $capsule-padding * 2 + $capsule-height;
  10. $navigation-font-size: 15PX;
  11. $navigation-icon-font-size: 25PX;
  12. $navigation-box-shadow: 0 2PX 2PX #222;
  13.  
  14. .navigation {
  15. position: relative;
  16. background: transparent;
  17.  
  18. .bar {
  19. position: fixed;
  20. top: 0;
  21. left: 0;
  22. display: flex;
  23. flex-direction: row;
  24. justify-content: center;
  25. align-items: center;
  26. width: 100%;
  27. height: $navigation-height;
  28. z-index: 1;
  29. font-size: $navigation-font-size;
  30. }
  31.  
  32. .placeholder {
  33. display: block;
  34. height: $navigation-height;
  35. background: transparent;
  36. }
  37. }

要解决我们先前提到的问题当开启小程序下拉刷新时,如何让顶部导航不会跟着下拉,仅仅只需设置.bar样式为position: fixed,这样当我们下拉刷新时导航栏就不会跟着动了,那为什么我们还需要.placeholder标签呢 
如果你尝试着去掉它,并且运行查看效果时,你会发现,页面的内容会被顶部导航栏遮挡了,我们需要对每个页面进行额外的设置以使它如预期一样显示,比如给每个页面设置顶部padding,这样的消耗太大,因此我们专门设置placeholder标签占据与导航栏相同的高度,使页面不被遮挡,且无需额外处理

 

ComponentHomeNavigation实现

有了这样一个基础组件,我们要实现首页导航栏效果就变得相当的简单了,直接上代码(src/components/home/navigation/navigation.js)

 
  1. import Taro, { Component } from '@tarojs/taro'
  2. import { View, Image, Text } from '@tarojs/components'
  3.  
  4. import { noop } from '../../../utils/tools'
  5. import ComponentBaseNavigation from '../../base/navigation/navigation'
  6.  
  7. import './navigation.scss'
  8.  
  9. class ComponentHomeNavigation extends Component {
  10. static defaultProps = {
  11. onSearch: noop,
  12. }
  13.  
  14. render() {
  15. const { onSearch } = this.props
  16.  
  17. return (
  18. <ComponentBaseNavigation>
  19. <View className="navigation">
  20. <Image className="logo" src="@oss/logo.png" />
  21. <View className="search" onClick={onSearch}>
  22. <View className="icon iconfont icon-search" />
  23. <Text className="text">搜索</Text>
  24. </View>
  25. </View>
  26. </ComponentBaseNavigation>
  27. )
  28. }
  29. }
  30.  
  31. export default ComponentHomeNavigation

引入导航组件到首页中, 省略样式代码(src/pages/home/home.js)

 
  1. import Taro, { Component } from '@tarojs/taro'
  2. import { View } from '@tarojs/components'
  3. import { dispatcher } from '@opcjs/zoro'
  4.  
  5. import ComponentCommonLogin from '../../components/common/login/login'
  6. import ComponentCommonSlogan from '../../components/common/slogan/slogan'
  7. // 引入导航组件
  8. import ComponentHomeNavigation from '../../components/home/navigation/navigation'
  9. import ComponentHomeCarousel from '../../components/home/carousel/carousel'
  10. import ComponentHomeBrand from '../../components/home/brand/brand'
  11.  
  12. import './home.scss'
  13.  
  14. class PageHome extends Component {
  15. config = {
  16. enablePullDownRefresh: true,
  17. }
  18.  
  19. state = {
  20. // 请到README.md中查看此参数说明
  21. __TAB_PAGE__: true, // eslint-disable-line
  22. }
  23.  
  24. componentDidMount() {
  25. dispatcher.banner.getBannerInfo()
  26. dispatcher.brand.getHotBrandList()
  27. }
  28.  
  29. onPullDownRefresh() {
  30. Promise.all([
  31. dispatcher.banner.getBannerInfo(),
  32. dispatcher.brand.getHotBrandList(),
  33. ])
  34. .then(Taro.stopPullDownRefresh)
  35. .catch(Taro.stopPullDownRefresh)
  36. }
  37.  
  38. handleGoSearch = () => Taro.navigateTo({ url: '/pages/search/search' })
  39.  
  40. render() {
  41. return (
  42. <View className="home">
  43. <ComponentCommonLogin />
  44. <ComponentHomeNavigation onSearch={this.handleGoSearch} />
  45. <ComponentHomeCarousel />
  46. <View class="content">
  47. <ComponentCommonSlogan />
  48. <ComponentHomeBrand />
  49. </View>
  50. </View>
  51. )
  52. }
  53. }
  54.  
  55. export default PageHome
 

ComponentCommonNavigation实现

该组件的实现方式与首页基本一致,需要提的一点就是返回键的实现,我们该如何统一的判断该页面是否需要返回键呢,这里需要利用微信接口wx.getCurrentPages(),实现代码如下(src/components/common/navigation/navigation.js)

 
  1. import Taro, { Component } from '@tarojs/taro'
  2. import { View } from '@tarojs/components'
  3. import classNames from 'classnames'
  4.  
  5. import ComponentBaseNavigation from '../../base/navigation/navigation'
  6.  
  7. import './navigation.scss'
  8.  
  9. class ComponentCommonNavigation extends Component {
  10. static defaultProps = {
  11. title: '',
  12. }
  13.  
  14. state = {
  15. canBack: false,
  16. }
  17.  
  18. componentDidMount() {
  19. // 获取当前页面是否需要返回键
  20. const canBack = Taro.getCurrentPages().length > 1
  21. this.setState({ canBack })
  22. }
  23.  
  24. handleGoHome = () => Taro.switchTab({ url: '/pages/home/home' })
  25.  
  26. handleGoBack = () => Taro.navigateBack()
  27.  
  28. render() {
  29. const { title } = this.props
  30. const { canBack } = this.state
  31.  
  32. return (
  33. <ComponentBaseNavigation>
  34. <View className={classNames('navigation', { padding: !canBack })}>
  35. <View className="tools">
  36. {canBack && (
  37. <View
  38. className="iconfont icon-arrow-left back"
  39. onClick={this.handleGoBack}
  40. />
  41. )}
  42. <View
  43. className="iconfont icon-home home"
  44. onClick={this.handleGoHome}
  45. />
  46. </View>
  47. <View className="title">{title}</View>
  48. </View>
  49. </ComponentBaseNavigation>
  50. )
  51. }
  52. }
  53.  
  54. export default ComponentCommonNavigation

感谢观看,文笔不佳,不能完全表达出设计思路,代码是最好的表达,移步weapp-clover 
本项目会持续完善,如有兴趣,请关注一波

作者:FaureWu 
链接:https://www.jianshu.com/p/5877a3dc0b1e 
來源:简书 
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。



温馨提示:这篇文章没有解决您的问题?欢迎添加微信:18948083295,有微信小程序专业人员,保证有问必答。转载本站文章请注明转自http://www.okeydown.com/(微信小程序网)。

  • 微信扫描二维码关注官方微信
  • ▲长按图片识别二维码
关注我们

微信小程序官方微信

栏目最新
栏目推荐
返回顶部