123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- "use strict";
- const common_vendor = require("../../../common/vendor.js");
- const sheep_helper_index = require("../../helper/index.js");
- require("../../helper/test.js");
- require("../../helper/digit.js");
- const _sfc_main = {
- name: "su-tabs",
- data() {
- return {
- addStyle: sheep_helper_index.addStyle,
- addUnit: sheep_helper_index.addUnit,
- firstTime: true,
- scrollLeft: 0,
- scrollViewWidth: 0,
- lineOffsetLeft: 0,
- tabsRect: {
- left: 0
- },
- innerCurrent: 0,
- moving: false
- };
- },
- props: {
- // 滑块的移动过渡时间,单位ms
- duration: {
- type: Number,
- default: 300
- },
- // tabs标签数组
- list: {
- type: Array,
- default: []
- },
- badge: {
- type: Boolean,
- default: false
- },
- // 滑块颜色
- lineColor: {
- type: String,
- default: ""
- },
- // 菜单选择中时的样式
- activeStyle: {
- type: [String, Object],
- default() {
- return {
- color: "#303133"
- };
- }
- },
- // 菜单非选中时的样式
- inactiveStyle: {
- type: [String, Object],
- default() {
- return {
- color: "#606266"
- };
- }
- },
- // 滑块长度
- lineWidth: {
- type: [String, Number],
- default: 20
- },
- // 滑块高度
- lineHeight: {
- type: [String, Number],
- default: 3
- },
- // 滑块背景显示大小,当滑块背景设置为图片时使用
- lineBgSize: {
- type: String,
- default: "cover"
- },
- // 菜单item的样式
- itemStyle: {
- type: [String, Object],
- default() {
- return {
- height: "44px"
- };
- }
- },
- // 菜单是否可滚动
- scrollable: {
- type: Boolean,
- default: true
- },
- // 当前选中标签的索引
- current: {
- type: [Number, String],
- default: 0
- },
- // 默认读取的键名
- keyName: {
- type: String,
- default: "name"
- }
- },
- watch: {
- current: {
- immediate: true,
- handler(newValue, oldValue) {
- if (newValue !== this.innerCurrent) {
- this.innerCurrent = newValue;
- this.$nextTick(() => {
- this.resize();
- });
- }
- }
- },
- // list变化时,重新渲染list各项信息
- list() {
- this.$nextTick(() => {
- this.resize();
- });
- }
- },
- computed: {
- textStyle() {
- return (index) => {
- const style = {};
- const customeStyle = index === this.innerCurrent ? sheep_helper_index.addStyle(this.activeStyle) : sheep_helper_index.addStyle(this.inactiveStyle);
- if (this.list[index].disabled) {
- style.color = "#c8c9cc";
- }
- return sheep_helper_index.deepMerge(customeStyle, style);
- };
- }
- },
- async mounted() {
- this.init();
- },
- methods: {
- $uGetRect(selector, all) {
- return new Promise((resolve) => {
- common_vendor.index.createSelectorQuery().in(this)[all ? "selectAll" : "select"](selector).boundingClientRect((rect) => {
- if (all && Array.isArray(rect) && rect.length) {
- resolve(rect);
- }
- if (!all && rect) {
- resolve(rect);
- }
- }).exec();
- });
- },
- setLineLeft() {
- const tabItem = this.list[this.innerCurrent];
- if (!tabItem) {
- return;
- }
- let lineOffsetLeft = this.list.slice(0, this.innerCurrent).reduce((total, curr) => total + curr.rect.width, 0);
- const lineWidth = sheep_helper_index.getPx(this.lineWidth);
- this.lineOffsetLeft = lineOffsetLeft + (tabItem.rect.width - lineWidth) / 2;
- if (this.firstTime) {
- setTimeout(() => {
- this.firstTime = false;
- }, 10);
- }
- },
- // nvue下设置滑块的位置
- animation(x, duration = 0) {
- },
- // 点击某一个标签
- clickHandler(item, index) {
- this.$emit("click", {
- ...item,
- index
- });
- if (item.disabled)
- return;
- this.innerCurrent = index;
- this.resize();
- this.$emit("change", {
- ...item,
- index
- });
- },
- init() {
- sheep_helper_index.sleep().then(() => {
- this.resize();
- });
- },
- setScrollLeft() {
- const tabRect = this.list[this.innerCurrent];
- const offsetLeft = this.list.slice(0, this.innerCurrent).reduce((total, curr) => {
- return total + curr.rect.width;
- }, 0);
- const windowWidth = sheep_helper_index.sys().windowWidth;
- let scrollLeft = offsetLeft - (this.tabsRect.width - tabRect.rect.width) / 2 - (windowWidth - this.tabsRect.right) / 2 + this.tabsRect.left / 2;
- scrollLeft = Math.min(scrollLeft, this.scrollViewWidth - this.tabsRect.width);
- this.scrollLeft = Math.max(0, scrollLeft);
- },
- // 获取所有标签的尺寸
- resize() {
- if (this.list.length === 0) {
- return;
- }
- Promise.all([this.getTabsRect(), this.getAllItemRect()]).then(
- ([tabsRect, itemRect = []]) => {
- this.tabsRect = tabsRect;
- this.scrollViewWidth = 0;
- itemRect.map((item, index) => {
- this.scrollViewWidth += item.width;
- this.list[index].rect = item;
- });
- this.setLineLeft();
- this.setScrollLeft();
- }
- );
- },
- // 获取导航菜单的尺寸
- getTabsRect() {
- return new Promise((resolve) => {
- this.queryRect("u-tabs__wrapper__scroll-view").then((size) => resolve(size));
- });
- },
- // 获取所有标签的尺寸
- getAllItemRect() {
- return new Promise((resolve) => {
- const promiseAllArr = this.list.map(
- (item, index) => this.queryRect(`u-tabs__wrapper__nav__item-${index}`, true)
- );
- Promise.all(promiseAllArr).then((sizes) => resolve(sizes));
- });
- },
- // 获取各个标签的尺寸
- queryRect(el, item) {
- return new Promise((resolve) => {
- this.$uGetRect(`.${el}`).then((size) => {
- resolve(size);
- });
- });
- }
- }
- };
- if (!Array) {
- const _easycom_uni_badge2 = common_vendor.resolveComponent("uni-badge");
- _easycom_uni_badge2();
- }
- const _easycom_uni_badge = () => "../../../uni_modules/uni-badge/components/uni-badge/uni-badge.js";
- if (!Math) {
- _easycom_uni_badge();
- }
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return {
- a: common_vendor.f($props.list, (item, index, i0) => {
- return common_vendor.e({
- a: $props.badge && item.isShow
- }, $props.badge && item.isShow ? {
- b: common_vendor.t(item[$props.keyName]),
- c: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item__text--disabled"),
- d: common_vendor.s($options.textStyle(index)),
- e: "035e7744-0-" + i0,
- f: common_vendor.p({
- text: item.num,
- absolute: "rightTop",
- size: "small"
- })
- } : {
- g: common_vendor.t(item[$props.keyName]),
- h: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item__text--disabled"),
- i: common_vendor.s($options.textStyle(index))
- }, {
- j: index,
- k: common_vendor.o(($event) => $options.clickHandler(item, index), index),
- l: `u-tabs__wrapper__nav__item-${index}`,
- m: common_vendor.n(`u-tabs__wrapper__nav__item-${index}`),
- n: common_vendor.n(item.disabled && "u-tabs__wrapper__nav__item--disabled")
- });
- }),
- b: common_vendor.s($data.addStyle($props.itemStyle)),
- c: common_vendor.s({
- flex: $props.scrollable ? "" : 1
- }),
- d: common_vendor.s({
- width: $data.addUnit($props.lineWidth),
- transform: `translate(${$data.lineOffsetLeft}px)`,
- transitionDuration: `${$data.firstTime ? 0 : $props.duration}ms`,
- height: $data.addUnit($props.lineHeight),
- background: $props.lineColor ? $props.lineColor : "var(--ui-BG-Main)",
- backgroundSize: $props.lineBgSize
- }),
- e: $props.scrollable,
- f: $data.scrollLeft
- };
- }
- const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-035e7744"], ["__file", "D:/zx/mall-front-app/sheep/ui/su-tabs/su-tabs.vue"]]);
- wx.createComponent(Component);
|