提交 1bc8c446 编写于 作者: L liuxiaohang

update(h5): tabbar支持更多配置,borderStyle、blurEffect、fontSize、iconWidth、spacing、height、midButton

上级 a05b11ad
...@@ -57,7 +57,7 @@ const getPageComponents = function (inputDir, pagesJson) { ...@@ -57,7 +57,7 @@ const getPageComponents = function (inputDir, pagesJson) {
if (tabBarList.length) { // 添加全部属性,方便 Vue 响应式 if (tabBarList.length) { // 添加全部属性,方便 Vue 响应式
pagesJson.tabBar.color = pagesJson.tabBar.color || '#999' pagesJson.tabBar.color = pagesJson.tabBar.color || '#999'
pagesJson.tabBar.selectedColor = pagesJson.tabBar.selectedColor || '#007aff' pagesJson.tabBar.selectedColor = pagesJson.tabBar.selectedColor || '#007aff'
pagesJson.tabBar.backgroundColor = pagesJson.tabBar.backgroundColor || '#f7f7fa' pagesJson.tabBar.backgroundColor = pagesJson.tabBar.backgroundColor || ''
pagesJson.tabBar.borderStyle = pagesJson.tabBar.borderStyle || 'black' pagesJson.tabBar.borderStyle = pagesJson.tabBar.borderStyle || 'black'
} }
...@@ -458,4 +458,4 @@ ${genRegisterPageVueComponentsCode(pageComponents)} ...@@ -458,4 +458,4 @@ ${genRegisterPageVueComponentsCode(pageComponents)}
global.__uniRoutes=[${genPageRoutes(pageComponents).concat(genSystemRoutes()).join(',')}] global.__uniRoutes=[${genPageRoutes(pageComponents).concat(genSystemRoutes()).join(',')}]
global.UniApp && new global.UniApp(); global.UniApp && new global.UniApp();
` `
} }
<template> <template>
<uni-tabbar :class="['uni-tabbar-'+position]"> <uni-tabbar :class="['uni-tabbar-'+position]">
<div <div
:style="{backgroundColor:backgroundColor}" :style="{
class="uni-tabbar" backgroundColor:tabbarBackgroundColor,
'backdrop-filter':blurEffect !== 'none' ? 'blur(10px)' : blurEffect
}"
class="uni-tabbar"
> >
<div <div
:style="{backgroundColor:borderColor}" :style="{backgroundColor:borderColor}"
class="uni-tabbar-border" class="uni-tabbar-border"
/> />
<div <div
v-for="(item,index) in list" v-for="(item,index) in list"
:key="item.pagePath" :key="item.isMidButton ? index : item.pagePath"
class="uni-tabbar__item" :style="item.isMidButton ? {flex:'0 0 ' + item.width} : {}"
@click="_switchTab(item,index)" class="uni-tabbar__item"
@click="_switchTab(item,index)"
> >
<div class="uni-tabbar__bd"> <!-- midButton iconPath -->
<div <div
v-if="item.iconPath" v-if="item.isMidButton"
:class="{'uni-tabbar__icon__diff':!item.text}" class="uni-tabbar__mid"
class="uni-tabbar__icon" :style="_uniTabbarBdStyle(item)"
>
<img
:style="{width: item.iconWidth,height:item.iconWidth}"
:src="_getRealPath(item.iconPath)"
> >
<img :src="_getRealPath(selectedIndex===index?item.selectedIconPath:item.iconPath)"> </div>
<div <!-- tabbar button -->
v-if="item.redDot" <div
:class="{'uni-tabbar__badge':!!item.badge}" class="uni-tabbar__bd"
class="uni-tabbar__reddot" :style="{height:height}"
>
<div
v-if="item.iconPath || item.isMidButton"
:class="{'uni-tabbar__icon__diff':!item.text}"
class="uni-tabbar__icon"
:style="{width: iconWidth,height:iconWidth}"
>
<img
v-if="!item.isMidButton"
:src="_getRealPath(selectedIndex===index?item.selectedIconPath:item.iconPath)"
>
<div
v-if="item.redDot"
:class="{'uni-tabbar__badge':!!item.badge}"
class="uni-tabbar__reddot"
> >
{{ item.badge }} {{ item.badge }}
</div> </div>
</div> </div>
<div <div
v-if="item.text" v-if="item.text"
:style="{color:selectedIndex===index?selectedColor:color,fontSize:item.iconPath?'10px':'14px'}" :style="{
class="uni-tabbar__label" color:selectedIndex === index ? selectedColor : color,
fontSize: fontSize,
lineHeight: !item.iconPath ? 1.8 : 'normal',
marginTop: !item.iconPath ? 'inherit' : spacing
}"
class="uni-tabbar__label"
> >
{{ item.text }} {{ item.text }}
<div <div
v-if="item.redDot&&!item.iconPath" v-if="item.redDot&&!item.iconPath"
:class="{'uni-tabbar__badge':!!item.badge}" :class="{'uni-tabbar__badge':!!item.badge}"
class="uni-tabbar__reddot" class="uni-tabbar__reddot"
> >
{{ item.badge }} {{ item.badge }}
</div> </div>
...@@ -46,7 +74,10 @@ ...@@ -46,7 +74,10 @@
</div> </div>
</div> </div>
</div> </div>
<div class="uni-placeholder" /> <div
class="uni-placeholder"
:style="{height:height}"
/>
</uni-tabbar> </uni-tabbar>
</template> </template>
...@@ -90,7 +121,6 @@ ...@@ -90,7 +121,6 @@
uni-tabbar .uni-tabbar~.uni-placeholder { uni-tabbar .uni-tabbar~.uni-placeholder {
width: 100%; width: 100%;
height: 50px;
margin-bottom: 0; margin-bottom: 0;
margin-bottom: constant(safe-area-inset-bottom); margin-bottom: constant(safe-area-inset-bottom);
margin-bottom: env(safe-area-inset-bottom); margin-bottom: env(safe-area-inset-bottom);
...@@ -113,7 +143,6 @@ ...@@ -113,7 +143,6 @@
uni-tabbar .uni-tabbar__bd { uni-tabbar .uni-tabbar__bd {
position: relative; position: relative;
height: 50px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
...@@ -125,8 +154,6 @@ ...@@ -125,8 +154,6 @@
position: relative; position: relative;
display: inline-block; display: inline-block;
margin-top: 5px; margin-top: 5px;
width: 24px;
height: 24px;
} }
uni-tabbar .uni-tabbar__icon.uni-tabbar__icon__diff { uni-tabbar .uni-tabbar__icon.uni-tabbar__icon__diff {
...@@ -144,7 +171,6 @@ ...@@ -144,7 +171,6 @@
position: relative; position: relative;
text-align: center; text-align: center;
font-size: 10px; font-size: 10px;
line-height: 1.8;
} }
uni-tabbar .uni-tabbar-border { uni-tabbar .uni-tabbar-border {
...@@ -179,10 +205,23 @@ ...@@ -179,10 +205,23 @@
text-align: center; text-align: center;
white-space: nowrap; white-space: nowrap;
} }
uni-tabbar .uni-tabbar__mid {
display: flex;
justify-content: center;
position: absolute;
bottom: 0;
background-size: 100% 100%;
}
</style> </style>
<script> <script>
import getRealPath from 'uni-platform/helpers/get-real-path' import getRealPath from 'uni-platform/helpers/get-real-path'
import { isPlainObject } from 'uni-shared'
import { publish } from 'uni-platform/service/bridge'
function cssSupports (css) {
return window.CSS && CSS.supports && (CSS.supports(css) || CSS.supports.apply(undefined, css.split(':')))
}
export default { export default {
name: 'TabBar', name: 'TabBar',
props: { props: {
...@@ -202,13 +241,11 @@ export default { ...@@ -202,13 +241,11 @@ export default {
}, },
backgroundColor: { backgroundColor: {
type: String, type: String,
default: '#f7f7fa' default: ''
}, },
borderStyle: { borderStyle: {
default: 'black', type: String,
validator (value) { default: 'black'
return ['black', 'white'].indexOf(value) !== -1
}
}, },
list: { list: {
type: Array, type: Array,
...@@ -221,6 +258,30 @@ export default { ...@@ -221,6 +258,30 @@ export default {
default: function () { default: function () {
return {} return {}
} }
},
blurEffect: {
type: String,
default: 'none'
},
fontSize: {
type: String,
default: '10px'
},
iconWidth: {
type: String,
default: '24px'
},
spacing: {
type: String,
default: '3px'
},
height: {
type: String,
default: '50px'
},
midButton: {
type: Object,
default: null
} }
}, },
data () { data () {
...@@ -229,10 +290,29 @@ export default { ...@@ -229,10 +290,29 @@ export default {
} }
}, },
computed: { computed: {
tabbarBackgroundColor () {
// 背景色 区分 高斯模糊 分别返回
const DefaultBgColor = '#f7f7fa'
if (this.backgroundColor) return this.backgroundColor
if (cssSupports('backdrop-filter:blur(10px)') && this.blurEffect !== 'none') {
if (this.blurEffect === 'dark') {
return 'rgb(0, 0, 0, 0.8)'
}
if (['light', 'extralight'].includes(this.blurEffect)) {
return 'rgb(250, 250, 250, 0.8)'
}
}
return DefaultBgColor
},
borderColor () { borderColor () {
return this.borderStyle === 'white' // 不再限制可配置颜色值
? 'rgba(255, 255, 255, 0.33)' if (this.borderStyle === 'white') return 'rgba(255, 255, 255, 0.33)'
: 'rgba(0, 0, 0, 0.33)' if (this.borderStyle === 'black') return 'rgba(0, 0, 0, 0.33)'
return this.borderStyle
} }
}, },
watch: { watch: {
...@@ -249,6 +329,9 @@ export default { ...@@ -249,6 +329,9 @@ export default {
} }
} }
}, },
created () {
this._initMidButton()
},
beforeCreate () { beforeCreate () {
this.__path__ = this.$route.path this.__path__ = this.$route.path
}, },
...@@ -263,8 +346,13 @@ export default { ...@@ -263,8 +346,13 @@ export default {
}, },
_switchTab ({ _switchTab ({
text, text,
pagePath pagePath,
isMidButton = false
}, index) { }, index) {
if (isMidButton) {
publish('onTabBarMidButtonTap')
return
}
this.selectedIndex = index this.selectedIndex = index
let url = '/' + pagePath let url = '/' + pagePath
if (url === __uniRoutes[0].alias) { if (url === __uniRoutes[0].alias) {
...@@ -285,7 +373,30 @@ export default { ...@@ -285,7 +373,30 @@ export default {
} else { } else {
UniServiceJSBridge.emit('onTabItemTap', detail) UniServiceJSBridge.emit('onTabItemTap', detail)
} }
},
_initMidButton () {
const listLength = this.list.length
// 偶数则添加midButton
if (listLength % 2 === 0 && isPlainObject(this.midButton)) {
// 给midButton赋值默认值
const DefaultMidButton = {
width: '50px',
height: '50px',
iconWidth: '24px'
}
for (const key in DefaultMidButton) {
this.midButton[key] = this.midButton[key] || DefaultMidButton[key]
}
this.list.splice(~~(listLength / 2), 0, Object.assign({}, this.midButton, { isMidButton: true }))
}
},
_uniTabbarBdStyle (item) {
return Object.assign({}, {
width: item.width,
height: item.height,
backgroundImage: item.backgroundImage ? 'url(\'' + this._getRealPath(item.backgroundImage) + '\')' : ''
})
} }
} }
} }
</script> </script>
...@@ -18,28 +18,6 @@ uni-page-head[uni-page-head-type="default"] ~ uni-page-wrapper { ...@@ -18,28 +18,6 @@ uni-page-head[uni-page-head-type="default"] ~ uni-page-wrapper {
height: calc(100% - 44px - env(safe-area-inset-top)); height: calc(100% - 44px - env(safe-area-inset-top));
} }
.uni-app--showtabbar uni-page-wrapper {
display: block;
height: calc(100% - 50px);
height: calc(100% - 50px - constant(safe-area-inset-bottom));
height: calc(100% - 50px - env(safe-area-inset-bottom));
}
.uni-app--showtabbar uni-page-wrapper::after {
content: "";
display: block;
width: 100%;
height: 50px;
height: calc(50px + constant(safe-area-inset-bottom));
height: calc(50px + env(safe-area-inset-bottom));
}
.uni-app--showtabbar uni-page-head[uni-page-head-type="default"] ~ uni-page-wrapper {
height: calc(100% - 44px - 50px);
height: calc(100% - 44px - constant(safe-area-inset-top) - 50px - constant(safe-area-inset-bottom));
height: calc(100% - 44px - env(safe-area-inset-top) - 50px - env(safe-area-inset-bottom));
}
uni-page-body { uni-page-body {
display: block; display: block;
box-sizing: border-box; box-sizing: border-box;
...@@ -47,7 +25,34 @@ uni-page-body { ...@@ -47,7 +25,34 @@ uni-page-body {
} }
</style> </style>
<script> <script>
import appendCss from 'uni-platform/helpers/append-css'
import { tabBar } from '../app/observable'
export default { export default {
name: 'PageBody' name: 'PageBody',
mounted () {
const HEIGTH = tabBar.height || '50px'
let cssText = `.uni-app--showtabbar uni-page-wrapper {
display: block;
height: calc(100% - ${HEIGTH});
height: calc(100% - ${HEIGTH} - constant(safe-area-inset-bottom));
height: calc(100% - ${HEIGTH} - env(safe-area-inset-bottom));
}`
cssText += '\n'
cssText += `.uni-app--showtabbar uni-page-wrapper::after {
content: "";
display: block;
width: 100%;
height: ${HEIGTH};
height: calc(${HEIGTH} + constant(safe-area-inset-bottom));
height: calc(${HEIGTH} + env(safe-area-inset-bottom));
}`
cssText += '\n'
cssText += `.uni-app--showtabbar uni-page-head[uni-page-head-type="default"] ~ uni-page-wrapper {
height: calc(100% - 44px - ${HEIGTH});
height: calc(100% - 44px - constant(safe-area-inset-top) - ${HEIGTH} - constant(safe-area-inset-bottom));
height: calc(100% - 44px - env(safe-area-inset-top) - ${HEIGTH} - env(safe-area-inset-bottom));
}`
appendCss(cssText)
}
} }
</script> </script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册