vr-shopxo-uniapp/components/diy/tabs.vue

179 lines
6.4 KiB
Vue

<template>
<!-- 选项卡 -->
<view class="tabs pr">
<view :class="top_up == '1' ? 'tabs-top' : ''" :style="tabs_top_style">
<view class="tabs-content wh-auto bs-bb" :style="style_container">
<componentDiyModulesTabsView :propValue="tabs_data" propIsTabsIcon :propStyle="propStyle" @tabs-click="tabs_click_event"></componentDiyModulesTabsView>
</view>
</view>
<view v-if="top_up == '1'" class="tabs-seat" :style="'height:' + tabs_seat_height + 'px;'"></view>
</view>
</template>
<script>
const app = getApp();
import { common_styles_computer } from '@/common/js/common/common.js';
import componentDiyModulesTabsView from '@/components/diy/modules/tabs-view';
// 状态栏高度
var bar_height = parseInt(app.globalData.get_system_info('statusBarHeight', 0));
// #ifdef MP-TOUTIAO
bar_height = 0;
// #endif
export default {
props: {
propValue: {
type: Object,
default: () => ({}),
},
// 置顶距离顶部高度
propTop: {
type: Number,
default: 0,
},
// 是否导航栏置顶
propNavIsTop: {
type: Boolean,
default: true,
},
// 是否选项卡置顶
propTabsIsTop: {
type: Boolean,
default: false,
},
propStyle: {
type: String,
default: '',
},
propkey: {
type: String,
default: '',
}
},
components: {
componentDiyModulesTabsView,
},
data() {
return {
style_container: '',
content: '',
tabs_data: {},
// 是否滑动置顶
top_up: '0',
// 置顶时,选项卡高度
tabs_seat_height: 0,
// 置顶时,选项卡距离顶部高度
// tabs_padding_top: 0,
// 置顶时,选项卡样式
tabs_top_style: '',
};
},
created() {
this.init();
},
mounted() {
setTimeout(() => {
this.get_tabs_height();
});
},
// 属性值改变监听
watch: {
// 数据
propTabsIsTop(value, old_value) {
this.init();
this.$nextTick(() => {
this.get_tabs_height();
});
},
propkey(val) {
// 初始化
this.init();
}
},
methods: {
init() {
const new_content = this.propValue.content || {};
const new_style = this.propValue.style || {};
let new_tabs_data = JSON.parse(JSON.stringify(this.propValue));
new_tabs_data.content.tabs_list.unshift(new_tabs_data.content.home_data);
// 判断选项卡是否置顶
let other_style = 'calc(' + this.propTop + 'px' + ' + 66rpx)';
let new_tabs_top_style = this.propNavIsTop || this.propTabsIsTop || new_content.tabs_top_up == '1' ? (new_content.tabs_top_up == '1' ? 'top:' + other_style + ';z-index:3;' : '') : '';
let new_top_up = new_content.tabs_top_up;
// #ifdef H5 || MP-TOUTIAO
if (this.propTabsIsTop) {
other_style = '0';
}
new_tabs_top_style = 'top:' + other_style + ';z-index:3;';
new_top_up = this.propNavIsTop || this.propTabsIsTop ? new_content.tabs_top_up : '0';
// #endif
let tabs_bg = new_style.common_style.color_list;
let new_tabs_background = '';
if (!Array.isArray(tabs_bg) || tabs_bg.length === 0 || !tabs_bg[0] || !tabs_bg[0].color) {
new_tabs_background = 'background:#fff;';
}
this.setData({
tabs_data: new_tabs_data,
style_container: common_styles_computer(new_style.common_style) + new_tabs_background,
tabs_top_style: new_tabs_top_style,
// 判断是否置顶
top_up: new_top_up,
// tabs_padding_top: this.propNavIsTop || this.propTabsIsTop ? other_style : '0',
});
},
// 获取选项卡高度
get_tabs_height() {
if (this.top_up == '1') {
const query = uni.createSelectorQuery();
// 选择我们想要的元素
query
.in(this)
.select('.tabs-content')
.boundingClientRect((res) => {
if ((res || null) != null) {
// data包含元素的宽度、高度等信息
this.setData({
tabs_seat_height: res.height,
});
this.$emit('computer-height', this.tabs_seat_height);
}
})
.exec(); // 执行查询
} else {
this.$emit('computer-height', 0);
}
},
// 选项卡回调
tabs_click_event(index, item) {
let tabs_id = '';
// 抽象出获取 tabs_id 的逻辑
tabs_id = this.get_tabs_id(item, index);
// 是否是商品分类页面
const is_micro_page = item.data_type == '0';
this.$emit('tabs-click', tabs_id, is_micro_page);
},
get_tabs_id(item, index) {
if (item.data_type === '0') {
return index !== 0 ? item.micro_page_list?.id : '';
} else {
return index !== 0 ? item.classify?.id : '';
}
},
},
};
</script>
<style lang="scss" scoped>
.tabs {
z-index: 3;
.tabs-top {
position: fixed;
left: 0;
right: 0;
max-width: 100%;
}
}
</style>