ie-navbar.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view class="relative z-100" :style="{ opacity: bgOpacity }">
  3. <uv-navbar :title="title" :bgColor="fixedBgColor" :titleStyle="titleStyle" :placeholder="bgFixed" :fixed="fixed"
  4. leftIcon="" :autoBack="false" @leftClick="leftClick" :clickHover="clickHover">
  5. <template #left>
  6. <slot name="headerLeft">
  7. <uv-icon name="arrow-left" size="20px" :color="titleStyle.color"></uv-icon>
  8. </slot>
  9. </template>
  10. <template #right>
  11. <slot name="headerRight" :isTransparent="isTransparent"></slot>
  12. </template>
  13. </uv-navbar>
  14. </view>
  15. </template>
  16. <script lang="ts" setup>
  17. import { useTransferPage } from "@/hooks/useTransferPage";
  18. import { useScroll } from '@/hooks/useScroll';
  19. const { transferBack } = useTransferPage();
  20. const { scrollTop } = useScroll();
  21. const isTransparent = ref(false);
  22. /**
  23. * ie-navbar 导航栏组件
  24. * @description 导航栏组件
  25. * @property {String} title 标题
  26. * @property {String} bgColor 背景颜色
  27. * @property {String} titleColor 标题颜色
  28. * @property {Boolean} fixed 是否固定
  29. * @property {Boolean} placeholder 是否占位
  30. * @property {Boolean} opacity 是否透明
  31. * @property {Boolean} transparent 是否透明
  32. * @property {Boolean} keepTitleColor 是否保持标题颜色
  33. * @property {Boolean} customBack 是否自定义返回
  34. * @property {Object} titleStyle 标题样式
  35. */
  36. type NavbarOptions = {
  37. title: string;
  38. bgColor: string;
  39. titleColor: string;
  40. fixed: boolean;
  41. placeholder: boolean;
  42. opacity: boolean;
  43. transparent: boolean;
  44. keepTitleColor: boolean;
  45. customBack: boolean;
  46. clickHover: boolean;
  47. titleStyle: any;
  48. }
  49. const props = withDefaults(defineProps<NavbarOptions>(), {
  50. fixed: true,
  51. placeholder: true,
  52. clickHover: true,
  53. titleStyle: () => ({})
  54. });
  55. const navHeight = 60;
  56. const fixedBgColor = computed(() => {
  57. if (props.opacity) {
  58. return props.bgColor;
  59. }
  60. if (props.transparent) {
  61. return uni.$uv.colorToRgba(props.bgColor, scrollTop.value / navHeight);
  62. }
  63. return props.bgColor;
  64. })
  65. const titleStyle = computed(() => {
  66. const style = { color: '', fontWeight: 600 };
  67. if (props.titleColor) {
  68. style.color = props.titleColor;
  69. }
  70. if (props.opacity) {
  71. return style;
  72. }
  73. if (props.keepTitleColor) {
  74. return style;
  75. }
  76. if (props.transparent) {
  77. if (scrollTop.value / navHeight > 0.5) {
  78. style.color = '#333';
  79. isTransparent.value = false;
  80. } else {
  81. style.color = '#fff';
  82. isTransparent.value = true;
  83. }
  84. }
  85. return {
  86. ...style,
  87. ...props.titleStyle
  88. };
  89. })
  90. const bgOpacity = computed(() => {
  91. if (props.opacity) {
  92. return scrollTop.value / navHeight;
  93. }
  94. return 1
  95. })
  96. const bgFixed = computed(() => {
  97. if (!props.placeholder) {
  98. return false;
  99. }
  100. if (props.opacity || props.transparent) {
  101. return false;
  102. }
  103. return true;
  104. })
  105. const emit = defineEmits(["leftClick"]);
  106. const leftClick = () => {
  107. emit("leftClick");
  108. if (!props.customBack) {
  109. handleBack();
  110. }
  111. }
  112. const handleBack = () => {
  113. transferBack();
  114. }
  115. </script>
  116. <style></style>