当前位置:   article > 正文

taro react 微信小程序 自定义公共头部_taro.$globalstatusbarheight

taro.$globalstatusbarheight

效果图

                                                                     默认头部样式

引入公共组件自定义样式

1.公共头部组件header.tsx

  1. import { useEffect, useState } from "react";
  2. import Taro from "@tarojs/taro";
  3. import { View, Image } from "@tarojs/components";
  4. import "./index.scss";
  5. interface CustomerHeaderProps {
  6. /**
  7. * 非必填 左边节点
  8. * */
  9. backNode?: React.ReactNode | string;
  10. /**
  11. * 标题
  12. * */
  13. title?:React.ReactNode | string
  14. /**
  15. * 图标
  16. * */
  17. Icon?:string
  18. }
  19. const CustomerHeader: React.FC<CustomerHeaderProps> = (
  20. props: CustomerHeaderProps
  21. ) => {
  22. const globalData:any = {}
  23. const { backNode,title,Icon } = props;
  24. const backIcon: any = '返回icon地址'
  25. const [navbarHeight, setnavbarHeight] = useState<number>(0); // 顶部导航栏高度
  26. const [cusnavH, setcusnavH] = useState<number>(0);
  27. const [statusBarHeight, setstatusBarHeight] = useState<number>(0);
  28. // console.log(navbarBtn);
  29. useEffect(() => {
  30. globalData.systeminfo = Taro.getSystemInfoSync();
  31. console.log('globalData', globalData);
  32. if (!globalData.headerBtnPosi)
  33. globalData.headerBtnPosi = Taro.getMenuButtonBoundingClientRect();
  34. let newstatusBarHeight = globalData.systeminfo.statusBarHeight; // 状态栏高度
  35. let headerPosi = globalData.headerBtnPosi; // 胶囊位置信息
  36. let btnPosi = {
  37. // 胶囊实际位置,坐标信息不是左上角原点
  38. height: headerPosi.height,
  39. width: headerPosi.width,
  40. top: headerPosi.top - newstatusBarHeight, // 胶囊top - 状态栏高度
  41. bottom: headerPosi.bottom - headerPosi.height - newstatusBarHeight, // 胶囊bottom - 胶囊height - 状态栏height (胶囊实际bottom 为距离导航栏底部的长度)
  42. right: globalData.systeminfo.windowWidth - headerPosi.right // 这里不能获取 屏幕宽度,PC端打开小程序会有BUG,要获取窗口高度 - 胶囊right
  43. };
  44. const newcusnavH = btnPosi.height + btnPosi.top + btnPosi.bottom; // 导航高度
  45. const newnavbarHeight = headerPosi.bottom + btnPosi.bottom; // 胶囊bottom + 胶囊实际bottom
  46. setnavbarHeight(newnavbarHeight);
  47. setcusnavH(newcusnavH);
  48. setstatusBarHeight(newstatusBarHeight);
  49. }, []);
  50. const back = ()=>{
  51. Taro.navigateBack()
  52. }
  53. return (
  54. <View className='custom_nav'>
  55. <View className='custom_nav_box' style={{ height: `${navbarHeight}px` }}>
  56. <View
  57. className='custom_nav_bar'
  58. style={{
  59. top: `${statusBarHeight}px`,
  60. height: `${cusnavH}px`,
  61. lineHeight: `${cusnavH}px`
  62. }}
  63. >
  64. <View className='nav_warp'>
  65. {backNode ? backNode : <Image className='icon' src={Icon || backIcon} onClick={back} />}
  66. <View className='title'>{title || '默认值'}</View>
  67. </View>
  68. </View>
  69. </View>
  70. </View>
  71. );
  72. };
  73. export default CustomerHeader;

2.公共头部组件css

  1. .custom_nav{
  2. width: 100%;
  3. background: #fff;
  4. position: relative;
  5. z-index: 99999;
  6. .custom_nav_box{
  7. position: fixed;
  8. width: 100%;
  9. background: #fff;
  10. z-index: 99999;
  11. border-bottom: 1px solid #ccc;
  12. }
  13. .custom_nav_bar {
  14. position: relative;
  15. z-index: 9;
  16. }
  17. .nav_warp{
  18. display: flex;
  19. align-items: center;
  20. position: relative;
  21. .icon{
  22. padding-left: 24px;
  23. width: 40px;
  24. height: 40px;
  25. }
  26. .title{
  27. flex: 0.9;
  28. font-size: 28px;
  29. text-align: center;
  30. }
  31. }
  32. }

3.引入公共组件默认样式

  1. import React, { useEffect } from "react";
  2. import { View } from "@tarojs/components"
  3. import CustomerHeader from '../../components/CustomerHeader'
  4. import './index.scss'
  5. interface Props {
  6. }
  7. const Chat: React.FC<Props> = () => {
  8. useEffect(() => {
  9. }, [])
  10. return (
  11. <>
  12. <CustomerHeader />
  13. <View className='controller'>
  14. </View>
  15. </>
  16. )
  17. }
  18. export default Chat

4.引入公共组件自定义样式

  1. import React, { useEffect } from "react";
  2. import { View, Image, Text } from "@tarojs/components"
  3. import CustomerHeader from '../../components/CustomerHeader'
  4. import './index.scss'
  5. interface Props {
  6. }
  7. const Chat: React.FC<Props> = () => {
  8. const backIcon: any = '返回icon地址'
  9. const userList = [
  10. {
  11. id: 1,
  12. avatarImg:'地址'
  13. },
  14. {
  15. id: 2,
  16. avatarImg:'地址'
  17. },
  18. {
  19. id: 3,
  20. avatarImg:'地址'
  21. },
  22. ]
  23. useEffect(() => {
  24. }, [])
  25. const back = () => {
  26. console.log('自定义事件')
  27. }
  28. return (
  29. <>
  30. <CustomerHeader
  31. backNode={
  32. <>
  33. <Image className='icon' src={backIcon} onClick={back} />
  34. <Text className='backText'>XXX</Text>
  35. </>
  36. }
  37. title={
  38. <View className='user_warp'>
  39. {
  40. userList?.map((item: any, index: number) => (
  41. <View className='avatarImg_warp'
  42. style={{
  43. zIndex: userList.length - index
  44. }} key={item.id}
  45. >
  46. <Image className='user_avatarImg' src={item.avatarImg} onClick={back} />
  47. </View>
  48. ))
  49. }
  50. </View>
  51. }
  52. />
  53. <View className='controller'>
  54. </View>
  55. </>
  56. )
  57. }
  58. export default Chat

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/751285
推荐阅读
相关标签
  

闽ICP备14008679号