微信小程序電商實戰-自定義頂部導航欄

夢想.jpg

感謝讀者@Followyour的好心提醒,對于安卓機,下拉刷新確實無法做到導航欄不跟著下拉,特此做一下修正,并且自定義導航欄存在它的不足,比如部分安卓機型無法獲取statusBarHeight,因此給予一個默認值20PX,但這并不能完全覆蓋所有機型,還有一點是使用自定義導航欄對于web-view的兼容性未做測試

本文章是一個系列文章,以一個完整的可用于生產的實際項目探索微信小程序開發中我們經常會遇到的問題,希望能提供完美的解決方案,這次是本系列文章的第二篇了,以下列出該系列文章鏈接。

  1. 微信小程序及h5,基于taro,zoro最佳實踐探索
  2. 微信小程序電商實戰-登錄模塊設計

微信自6.6.0版本之后提供了自定義頂部導航欄的功能,這使得我們的全屏頁面設計成為了可能

首先演示下最終的實現效果


微信小程序自定義導航欄演示.gif

我們實現了一個與微信之前的導航欄行為基本一致,樣式可自定義的導航欄,接下來讓我們一步一步實現它,這里主要需要考慮如下幾點

  • 不同的手機,狀態欄高度不同,需要進行相關適配
  • 當開啟小程序下拉刷新時,如何讓頂部導航不會跟著下拉(僅IOS有效)
  • 自定義導航欄封裝成獨立組件,實現僅需引入到頁面,無需對頁面樣式做相關適配工作

該項目托管于github,有興趣的可以直接查看源碼,weapp-clover,如何運行項目源碼請查看ztaro

要想實現自定義導航,首先我們需要配置navigationStyle為custom(src/app.js)

config = {
  window: {
    navigationStyle: 'custom'
  }
}

再實際情況中,我們往往需要對自定義導航進行各種各樣的定制化,因此我們希望,封裝一個最基本的導航欄,用于解決適配問題,其他樣式的導航欄僅需對其進行二次封裝,無需在關心適配問題,對于這個項目,我們封裝組件如下:

ComponentBaseNavigation 導航欄基本組件,用于解決適配問題
ComponentHomeNavigation 引入基本導航組件,定制化首頁導航欄組件
ComponentCommonNavigation 引入基本導航組件,定制化其他頁面導航組件

ComponentBaseNavigation實現

對于適配不通手機頂部的狀態欄高度,我們需要利用微信wx.getSystemInfo獲取狀態欄的高度,因此在user model中新增如下代碼(src/models/user.js)

// 省略其他無關代碼
import Taro from '@tarojs/taro'

export default {
  namespace: 'user',

  mixins: ['common'],

  state: {
    systemInfo: {},
  },

  async setup({ put }) {
    // 新增初始化獲取用戶手機系統相關信息,存儲到redux全局狀態中
    Taro.getSystemInfo().then(systemInfo =>
      put({ type: 'update', payload: { systemInfo } }),
    )
  }
}

實現組件邏輯(src/components/base/navigation/navigation.js)

import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { connect } from '@tarojs/redux'

import './navigation.scss'

@connect(({ user }) => ({
  // 鏈接redux中存儲的狀態欄高度到組件中
  statusBarHeight: user.systemInfo.statusBarHeight,
}))
class ComponentBaseNavigation extends Component {
  static defaultProps = {
    color: 'white',
    backgroundColor: '#2f3333',
  }

  render() {
    const { statusBarHeight, backgroundColor, color } = this.props

    const barStyle = {
      paddingTop: `${statusBarHeight || 20}px`,
      backgroundColor,
      color,
    }

    return (
      <View className="navigation">
        <View className="bar" style={barStyle}>
          {this.props.children}
        </View>
        <View className="placeholder" style={barStyle} />
      </View>
    )
  }
}

export default ComponentBaseNavigation

樣式如下(src/components/base/navigation.scss)

// 大寫的PX單位是為了告訴Taro,不要轉換成單位rpx
// 通過測試和觀察發現,微信頂部的膠囊寬高如下,并且各個屏幕下一致
// 因此采用PX單位
$capsule-padding: 6PX; // 膠囊的上下padding距離
$capsule-height: 32PX; // 膠囊的高度
$capsule-width: 88PX; // 膠囊的寬度

$navigation-height: $capsule-padding * 2 + $capsule-height;
$navigation-font-size: 15PX;
$navigation-icon-font-size: 25PX;
$navigation-box-shadow: 0 2PX 2PX #222;

.navigation {
  position: relative;
  background: transparent;
  
  .bar {
    position: fixed;
    top: 0;
    left: 0;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: $navigation-height;
    z-index: 1;
    font-size: $navigation-font-size;
  }

  .placeholder {
    display: block;
    height: $navigation-height;
    background: transparent;
  }
}

要解決我們先前提到的問題當開啟小程序下拉刷新時,如何讓頂部導航不會跟著下拉,僅僅只需設置.bar樣式為position: fixed,這樣當我們下拉刷新時導航欄就不會跟著動了,那為什么我們還需要.placeholder標簽呢

如果你嘗試著去掉它,并且運行查看效果時,你會發現,頁面的內容會被頂部導航欄遮擋了,我們需要對每個頁面進行額外的設置以使它如預期一樣顯示,比如給每個頁面設置頂部padding,這樣的消耗太大,因此我們專門設置placeholder標簽占據與導航欄相同的高度,使頁面不被遮擋,且無需額外處理

ComponentHomeNavigation實現

有了這樣一個基礎組件,我們要實現首頁導航欄效果就變得相當的簡單了,直接上代碼(src/components/home/navigation/navigation.js)

import Taro, { Component } from '@tarojs/taro'
import { View, Image, Text } from '@tarojs/components'

import { noop } from '../../../utils/tools'
import ComponentBaseNavigation from '../../base/navigation/navigation'

import './navigation.scss'

class ComponentHomeNavigation extends Component {
  static defaultProps = {
    onSearch: noop,
  }

  render() {
    const { onSearch } = this.props

    return (
      <ComponentBaseNavigation>
        <View className="navigation">
          <Image className="logo" src="@oss/logo.png" />
          <View className="search" onClick={onSearch}>
            <View className="icon iconfont icon-search" />
            <Text className="text">搜索</Text>
          </View>
        </View>
      </ComponentBaseNavigation>
    )
  }
}

export default ComponentHomeNavigation

引入導航組件到首頁中, 省略樣式代碼(src/pages/home/home.js)

import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { dispatcher } from '@opcjs/zoro'

import ComponentCommonLogin from '../../components/common/login/login'
import ComponentCommonSlogan from '../../components/common/slogan/slogan'
// 引入導航組件
import ComponentHomeNavigation from '../../components/home/navigation/navigation'
import ComponentHomeCarousel from '../../components/home/carousel/carousel'
import ComponentHomeBrand from '../../components/home/brand/brand'

import './home.scss'

class PageHome extends Component {
  config = {
    enablePullDownRefresh: true,
  }

  state = {
    // 請到README.md中查看此參數說明
    __TAB_PAGE__: true, // eslint-disable-line
  }

  componentDidMount() {
    dispatcher.banner.getBannerInfo()
    dispatcher.brand.getHotBrandList()
  }

  onPullDownRefresh() {
    Promise.all([
      dispatcher.banner.getBannerInfo(),
      dispatcher.brand.getHotBrandList(),
    ])
      .then(Taro.stopPullDownRefresh)
      .catch(Taro.stopPullDownRefresh)
  }

  handleGoSearch = () => Taro.navigateTo({ url: '/pages/search/search' })

  render() {
    return (
      <View className="home">
        <ComponentCommonLogin />
        <ComponentHomeNavigation onSearch={this.handleGoSearch} />
        <ComponentHomeCarousel />
        <View class="content">
          <ComponentCommonSlogan />
          <ComponentHomeBrand />
        </View>
      </View>
    )
  }
}

export default PageHome

ComponentCommonNavigation實現

該組件的實現方式與首頁基本一致,需要提的一點就是返回鍵的實現,我們該如何統一的判斷該頁面是否需要返回鍵呢,這里需要利用微信接口wx.getCurrentPages(),實現代碼如下(src/components/common/navigation/navigation.js)

import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import classNames from 'classnames'

import ComponentBaseNavigation from '../../base/navigation/navigation'

import './navigation.scss'

class ComponentCommonNavigation extends Component {
  static defaultProps = {
    title: '',
  }

  state = {
    canBack: false,
  }

  componentDidMount() {
    // 獲取當前頁面是否需要返回鍵
    const canBack = Taro.getCurrentPages().length > 1
    this.setState({ canBack })
  }

  handleGoHome = () => Taro.switchTab({ url: '/pages/home/home' })
  
  handleGoBack = () => Taro.navigateBack()

  render() {
    const { title } = this.props
    const { canBack } = this.state

    return (
      <ComponentBaseNavigation>
        <View className={classNames('navigation', { padding: !canBack })}>
          <View className="tools">
            {canBack && (
              <View
                className="iconfont icon-arrow-left back"
                onClick={this.handleGoBack}
              />
            )}
            <View
              className="iconfont icon-home home"
              onClick={this.handleGoHome}
            />
          </View>
          <View className="title">{title}</View>
        </View>
      </ComponentBaseNavigation>
    )
  }
}

export default ComponentCommonNavigation

感謝觀看,文筆不佳,不能完全表達出設計思路,代碼是最好的表達,移步weapp-clover

本項目會持續完善,如有興趣,請關注一波

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容