main
fanfan 8 months ago
parent 3a4c52f674
commit 0f76dc0da4
  1. 4
      pages/index/index.vue
  2. 788
      uni_modules/vk-uview-ui/components/u-subsection/u-subsection.vue

@ -2,7 +2,7 @@
<view class="index">
<view class="navbarContent">
<u-navbar :is-back="false" title="" :background="background" :border-bottom="false">
<image :src="logoImage" mode="heightFix" class="logo"></image>
<image v-if="logoImage" :src="logoImage" mode="heightFix" class="logo"></image>
<view class="slot-wrap">
<u-subsection active-color="#FB213D" inactive-color="#000000" height="60" @change="changeHome"
:bold="false" button-color="#fff" :list="list" :current="current"></u-subsection>
@ -1097,7 +1097,7 @@
onLoad() {
this.getTypeList()
this.getSeckill()
this.getSuggest()
this.getSuggest()//
this.getCityInfo()
this.getCoupons();
this.getServeList();

@ -1,402 +1,416 @@
<template>
<view class="u-subsection" :style="[subsectionStyle]">
<view class="u-item u-line-1" :style="[itemStyle(index)]" @tap="click(index)" :class="[noBorderRight(index), 'u-item-' + index]"
v-for="(item, index) in listInfo" :key="index">
<view :style="[textStyle(index)]" class="u-item-text u-line-1">{{ item.name }}</view>
<u-badge v-if="item.num > 0" :count="item.num" :offset="offset" size="mini"></u-badge>
</view>
<view class="u-item-bg" :style="[itemBarStyle]"></view>
</view>
<view class="u-subsection" :style="[subsectionStyle]">
<view
class="u-item u-line-1"
:style="[itemStyle(index)]"
@tap="click(index)"
:class="[noBorderRight(index), 'u-item-' + index]"
v-for="(item, index) in listInfo"
:key="index"
>
<view :style="[textStyle(index)]" class="u-item-text u-line-1">{{
item.name
}}</view>
<u-badge
v-if="item.num > 0"
:count="item.num"
:offset="offset"
size="mini"
></u-badge>
</view>
<view class="u-item-bg" :style="[itemBarStyle]"></view>
</view>
</template>
<script>
/**
* subsection 分段器
* @description 该分段器一般用于用户从几个选项中选择某一个的场景
* @tutorial https://www.uviewui.com/components/subsection.html
* @property {Array} list 选项的数组形式见上方"基本使用"
* @property {String Number} current 初始化时默认选中的选项索引值默认0
* @property {String} active-color 激活时的颜色mode为subsection时固定为白色默认#303133
* @property {String} inactive-color 未激活时字体的颜色mode为subsection时无效默认#606266
* @property {String} mode 模式选择见官网"模式选择"说明默认button
* @property {String Number} font-size 字体大小单位rpx默认28
* @property {String Number} height 组件高度单位rpx默认70
* @property {Boolean} animation 是否开启动画效果见上方说明默认true
* @property {Boolean} bold 激活选项的字体是否加粗默认true
* @property {String} bg-color 组件背景颜色mode为button时有效默认#eeeeef
* @property {String} button-color 按钮背景颜色mode为button时有效默认#ffffff
* @event {Function} change 分段器选项发生改变时触发
* @example <u-subsection active-color="#ff9900"></u-subsection>
*/
export default {
name: "u-subsection",
emits: ["change","update:modelValue", "input"],
props: {
// tab
list: {
type: Array,
default () {
return [];
}
},
value: {
type: [String, Number],
default: 0
},
modelValue: {
type: [String, Number],
default: 0
},
// tabindex
current: {
type: [Number, String],
default: 0
},
//
activeColor: {
type: String,
default: '#303133'
},
//
inactiveColor: {
type: String,
default: '#606266'
},
// mode=buttonmode=subsection
mode: {
type: String,
default: 'button'
},
// rpx
fontSize: {
type: [Number, String],
default: 28
},
//
animation: {
type: Boolean,
default: true
},
// rpx
height: {
type: [Number, String],
default: 70
},
// tab
bold: {
type: Boolean,
default: true
},
// mode=button
bgColor: {
type: String,
default: '#eeeeef'
},
// mode = button
buttonColor: {
type: String,
default: '#ffffff'
},
//
vibrateShort: {
type: Boolean,
default: false
},
offset: {
type: Array,
default: function(){
return [0,0]
}
},
},
data() {
return {
itemBgStyle: {
width: 0,
left: 0,
backgroundColor: '#ffffff',
height: '100%',
transition: ''
},
currentIndex: this.current,
buttonPadding: 3, // mode = button
borderRadius: 5, //
firstTimeVibrateShort: true ,// current
listInfo:[]
};
},
watch: {
valueCom: {
immediate: true,
handler(nVal) {
if (!nVal) nVal = 0;
this.currentIndex = nVal;
this.changeSectionStatus(nVal);
}
},
current: {
immediate: true,
handler(nVal) {
if (!nVal) nVal = this.valueCom || 0;
this.currentIndex = nVal;
this.changeSectionStatus(nVal);
}
},
list: {
deep:true,
handler(nVal=[]) {
this.listInfoFn();
setTimeout(() => {
this.getTabsInfo();
}, 10);
}
},
},
computed: {
valueCom() {
// #ifndef VUE3
return this.value;
// #endif
// #ifdef VUE3
return this.modelValue;
// #endif
},
// mode=subsection
noBorderRight() {
return index => {
if (this.mode != 'subsection') return;
let classs = '';
//
if (index < this.list.length - 1) classs += ' u-none-border-right';
//
if (index == 0) classs += ' u-item-first';
if (index == this.list.length - 1) classs += ' u-item-last';
return classs;
};
},
//
textStyle() {
return index => {
let style = {};
//
if (this.mode == 'subsection') {
if (index == this.currentIndex) {
style.color = '#ffffff';
} else {
style.color = this.activeColor;
}
} else {
if (index == this.currentIndex) {
style.color = this.activeColor;
} else {
style.color = this.inactiveColor;
}
}
//
if (index == this.currentIndex && this.bold) style.fontWeight = 'bold';
//
style.fontSize = this.fontSize + 'rpx';
return style;
};
},
// item
itemStyle() {
return index => {
let style = {};
if (this.mode == 'subsection') {
// border
style.borderColor = this.activeColor;
style.borderWidth = '1px';
style.borderStyle = 'solid';
}
return style;
};
},
// mode=buttonview
subsectionStyle() {
let style = {};
style.height = uni.upx2px(this.height) + 'px';
if (this.mode == 'button') {
style.backgroundColor = this.bgColor;
style.padding = `${this.buttonPadding}px`;
style.borderRadius = `${this.borderRadius}px`;
}
return style;
},
//
itemBarStyle() {
let style = {};
style.backgroundColor = this.activeColor;
style.zIndex = 1;
if (this.mode == 'button') {
style.backgroundColor = this.buttonColor;
style.borderRadius = `${this.borderRadius}px`;
style.bottom = `${this.buttonPadding}px`;
style.height = uni.upx2px(this.height) - this.buttonPadding * 2 + 'px';
style.zIndex = 0;
}
return Object.assign(this.itemBgStyle, style);
}
},
mounted() {
this.listInfoFn();
setTimeout(() => {
this.getTabsInfo();
}, 10);
},
methods: {
listInfoFn(){
let { list =[] } = this;
this.listInfo = this.list.map((val, index) => {
if (typeof val != 'object') {
let obj = {
width: 0,
name: val,
};
return obj;
} else {
return val;
}
});
return this.listInfo;
},
//
changeSectionStatus(nVal) {
if (this.mode == 'subsection') {
//
if (nVal == this.list.length - 1) {
this.itemBgStyle.borderRadius = `0 ${this.buttonPadding}px ${this.buttonPadding}px 0`;
}
if (nVal == 0) {
this.itemBgStyle.borderRadius = `${this.buttonPadding}px 0 0 ${this.buttonPadding}px`;
}
if (nVal > 0 && nVal < this.list.length - 1) {
this.itemBgStyle.borderRadius = '0';
}
}
//
setTimeout(() => {
this.itemBgLeft();
}, 10);
if (this.vibrateShort && !this.firstTimeVibrateShort) {
// 使APP(HX 2.6.8)H5
// #ifndef H5
uni.vibrateShort();
// #endif
}
// firstTimeVibrateShortfalse()
this.firstTimeVibrateShort = false;
},
click(index) {
//
if (index == this.currentIndex) return;
this.currentIndex = index;
this.changeSectionStatus(index);
this.$emit('change', Number(index));
this.$emit("input", Number(index));
this.$emit("update:modelValue", Number(index));
},
// tab
getTabsInfo() {
let view = uni.createSelectorQuery().in(this);
for (let i = 0; i < this.list.length; i++) {
view.select('.u-item-' + i).boundingClientRect();
}
view.exec(res => {
if (!res.length) {
setTimeout(() => {
this.getTabsInfo();
return;
}, 10);
}
// itemlistInfo
res.map((val, index) => {
this.listInfo[index].width = val.width;
});
//
if (this.mode == 'subsection') {
this.itemBgStyle.width = this.listInfo[0].width + 'px';
} else if (this.mode == 'button') {
this.itemBgStyle.width = this.listInfo[0].width + 'px';
}
//
this.itemBgLeft();
});
},
itemBgLeft() {
//
if (this.animation) {
this.itemBgStyle.transition = 'all 0.35s';
} else {
this.itemBgStyle.transition = 'all 0s';
}
let left = 0;
// item
this.listInfo.map((val, index) => {
if (index < this.currentIndex) left += val.width;
});
// mode
if (this.mode == 'subsection') {
this.itemBgStyle.left = left + 'px';
} else if (this.mode == 'button') {
this.itemBgStyle.left = left + this.buttonPadding + 'px';
}
}
}
};
/**
* subsection 分段器
* @description 该分段器一般用于用户从几个选项中选择某一个的场景
* @tutorial https://www.uviewui.com/components/subsection.html
* @property {Array} list 选项的数组形式见上方"基本使用"
* @property {String Number} current 初始化时默认选中的选项索引值默认0
* @property {String} active-color 激活时的颜色mode为subsection时固定为白色默认#303133
* @property {String} inactive-color 未激活时字体的颜色mode为subsection时无效默认#606266
* @property {String} mode 模式选择见官网"模式选择"说明默认button
* @property {String Number} font-size 字体大小单位rpx默认28
* @property {String Number} height 组件高度单位rpx默认70
* @property {Boolean} animation 是否开启动画效果见上方说明默认true
* @property {Boolean} bold 激活选项的字体是否加粗默认true
* @property {String} bg-color 组件背景颜色mode为button时有效默认#eeeeef
* @property {String} button-color 按钮背景颜色mode为button时有效默认#ffffff
* @event {Function} change 分段器选项发生改变时触发
* @example <u-subsection active-color="#ff9900"></u-subsection>
*/
export default {
name: "u-subsection",
emits: ["change", "update:modelValue", "input"],
props: {
// tab
list: {
type: Array,
default() {
return [];
},
},
value: {
type: [String, Number],
default: 0,
},
modelValue: {
type: [String, Number],
default: 0,
},
// tabindex
current: {
type: [Number, String],
default: 0,
},
//
activeColor: {
type: String,
default: "#303133",
},
//
inactiveColor: {
type: String,
default: "#606266",
},
// mode=buttonmode=subsection
mode: {
type: String,
default: "button",
},
// rpx
fontSize: {
type: [Number, String],
default: 28,
},
//
animation: {
type: Boolean,
default: true,
},
// rpx
height: {
type: [Number, String],
default: 70,
},
// tab
bold: {
type: Boolean,
default: true,
},
// mode=button
bgColor: {
type: String,
default: "#eeeeef",
},
// mode = button
buttonColor: {
type: String,
default: "#ffffff",
},
//
vibrateShort: {
type: Boolean,
default: false,
},
offset: {
type: Array,
default: function () {
return [0, 0];
},
},
},
data() {
return {
itemBgStyle: {
width: 0,
left: 0,
backgroundColor: "#ffffff",
height: "100%",
transition: "",
},
currentIndex: this.current,
buttonPadding: 3, // mode = button
borderRadius: 5, //
firstTimeVibrateShort: true, // current
listInfo: [],
};
},
watch: {
valueCom: {
immediate: true,
handler(nVal) {
if (!nVal) nVal = 0;
this.currentIndex = nVal;
this.changeSectionStatus(nVal);
},
},
current: {
immediate: true,
handler(nVal) {
if (!nVal) nVal = this.valueCom || 0;
this.currentIndex = nVal;
this.changeSectionStatus(nVal);
},
},
list: {
deep: true,
handler(nVal = []) {
this.listInfoFn();
setTimeout(() => {
this.getTabsInfo();
}, 10);
},
},
},
computed: {
valueCom() {
// #ifndef VUE3
return this.value;
// #endif
// #ifdef VUE3
return this.modelValue;
// #endif
},
// mode=subsection
noBorderRight() {
return (index) => {
if (this.mode != "subsection") return;
let classs = "";
//
if (index < this.list.length - 1) classs += " u-none-border-right";
//
if (index == 0) classs += " u-item-first";
if (index == this.list.length - 1) classs += " u-item-last";
return classs;
};
},
//
textStyle() {
return (index) => {
let style = {};
//
if (this.mode == "subsection") {
if (index == this.currentIndex) {
style.color = "#ffffff";
} else {
style.color = this.activeColor;
}
} else {
if (index == this.currentIndex) {
style.color = this.activeColor;
} else {
style.color = this.inactiveColor;
}
}
//
if (index == this.currentIndex && this.bold) style.fontWeight = "bold";
//
style.fontSize = this.fontSize + "rpx";
return style;
};
},
// item
itemStyle() {
return (index) => {
let style = {};
if (this.mode == "subsection") {
// border
style.borderColor = this.activeColor;
style.borderWidth = "1px";
style.borderStyle = "solid";
}
return style;
};
},
// mode=buttonview
subsectionStyle() {
let style = {};
style.height = uni.upx2px(this.height) + "px";
if (this.mode == "button") {
style.backgroundColor = this.bgColor;
style.padding = `${this.buttonPadding}px`;
style.borderRadius = `${this.borderRadius}px`;
}
return style;
},
//
itemBarStyle() {
let style = {};
style.backgroundColor = this.activeColor;
style.zIndex = 1;
if (this.mode == "button") {
style.backgroundColor = this.buttonColor;
style.borderRadius = `${this.borderRadius}px`;
style.bottom = `${this.buttonPadding}px`;
style.height = uni.upx2px(this.height) - this.buttonPadding * 2 + "px";
style.zIndex = 0;
}
return Object.assign(this.itemBgStyle, style);
},
},
mounted() {
this.listInfoFn();
setTimeout(() => {
this.getTabsInfo();
}, 10);
},
methods: {
listInfoFn() {
let { list = [] } = this;
this.listInfo = this.list.map((val, index) => {
if (typeof val != "object") {
let obj = {
width: 0,
name: val,
};
return obj;
} else {
return val;
}
});
return this.listInfo;
},
//
changeSectionStatus(nVal) {
if (this.mode == "subsection") {
//
if (nVal == this.list.length - 1) {
this.itemBgStyle.borderRadius = `0 ${this.buttonPadding}px ${this.buttonPadding}px 0`;
}
if (nVal == 0) {
this.itemBgStyle.borderRadius = `${this.buttonPadding}px 0 0 ${this.buttonPadding}px`;
}
if (nVal > 0 && nVal < this.list.length - 1) {
this.itemBgStyle.borderRadius = "0";
}
}
//
setTimeout(() => {
this.itemBgLeft();
}, 10);
if (this.vibrateShort && !this.firstTimeVibrateShort) {
// 使APP(HX 2.6.8)H5
// #ifndef H5
uni.vibrateShort();
// #endif
}
// firstTimeVibrateShortfalse()
this.firstTimeVibrateShort = false;
},
click(index) {
//
if (index == this.currentIndex) return;
this.currentIndex = index;
this.changeSectionStatus(index);
this.$emit("change", Number(index));
this.$emit("input", Number(index));
this.$emit("update:modelValue", Number(index));
},
// tab
getTabsInfo() {
let view = uni.createSelectorQuery().in(this);
for (let i = 0; i < this.list.length; i++) {
view.select(".u-item-" + i).boundingClientRect();
}
view.exec((res) => {
if (!res.length) {
setTimeout(() => {
this.getTabsInfo();
return;
}, 10);
}
// itemlistInfo
res.map((val, index) => {
this.listInfo[index].width = val.width;
});
//
if (this.listInfo) {
if (this.mode == "subsection") {
this.itemBgStyle.width = this.listInfo[0].width + "px";
} else if (this.mode == "button") {
this.itemBgStyle.width = this.listInfo[0].width + "px";
}
}
//
this.itemBgLeft();
});
},
itemBgLeft() {
//
if (this.animation) {
this.itemBgStyle.transition = "all 0.35s";
} else {
this.itemBgStyle.transition = "all 0s";
}
let left = 0;
// item
this.listInfo.map((val, index) => {
if (index < this.currentIndex) left += val.width;
});
// mode
if (this.mode == "subsection") {
this.itemBgStyle.left = left + "px";
} else if (this.mode == "button") {
this.itemBgStyle.left = left + this.buttonPadding + "px";
}
},
},
};
</script>
<style lang="scss" scoped>
@import "../../libs/css/style.components.scss";
@import "../../libs/css/style.components.scss";
.u-subsection {
@include vue-flex;
align-items: center;
overflow: hidden;
position: relative;
}
.u-subsection {
@include vue-flex;
align-items: center;
overflow: hidden;
position: relative;
}
.u-item {
flex: 1;
text-align: center;
font-size: 26rpx;
height: 100%;
@include vue-flex;
align-items: center;
justify-content: center;
color: $u-main-color;
padding: 0 6rpx;
position: relative;
}
.u-item {
flex: 1;
text-align: center;
font-size: 26rpx;
height: 100%;
@include vue-flex;
align-items: center;
justify-content: center;
color: $u-main-color;
padding: 0 6rpx;
position: relative;
}
.u-item-bg {
background-color: $u-type-primary;
position: absolute;
z-index: -1;
}
.u-item-bg {
background-color: $u-type-primary;
position: absolute;
z-index: -1;
}
.u-none-border-right {
border-right: none !important;
}
.u-none-border-right {
border-right: none !important;
}
.u-item-first {
border-top-left-radius: 8rpx;
border-bottom-left-radius: 8rpx;
}
.u-item-first {
border-top-left-radius: 8rpx;
border-bottom-left-radius: 8rpx;
}
.u-item-last {
border-top-right-radius: 8rpx;
border-bottom-right-radius: 8rpx;
}
.u-item-last {
border-top-right-radius: 8rpx;
border-bottom-right-radius: 8rpx;
}
.u-item-text {
transition: all 0.35s;
color: $u-main-color;
@include vue-flex;
align-items: center;
position: relative;
z-index: 3;
}
.u-item-text {
transition: all 0.35s;
color: $u-main-color;
@include vue-flex;
align-items: center;
position: relative;
z-index: 3;
}
</style>

Loading…
Cancel
Save