uview-pro 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +6 -0
- package/components/u-action-sheet/u-action-sheet.vue +205 -0
- package/components/u-alert-tips/u-alert-tips.vue +241 -0
- package/components/u-avatar/u-avatar.vue +220 -0
- package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
- package/components/u-avatar-cropper/weCropper.d.ts +54 -0
- package/components/u-avatar-cropper/weCropper.js +1267 -0
- package/components/u-avatar-cropper/weCropper.ts +1254 -0
- package/components/u-back-top/u-back-top.vue +156 -0
- package/components/u-badge/u-badge.vue +189 -0
- package/components/u-button/u-button.vue +562 -0
- package/components/u-calendar/u-calendar.vue +725 -0
- package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
- package/components/u-card/u-card.vue +240 -0
- package/components/u-cell-group/u-cell-group.vue +56 -0
- package/components/u-cell-item/u-cell-item.vue +245 -0
- package/components/u-checkbox/u-checkbox.vue +310 -0
- package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
- package/components/u-circle-progress/u-circle-progress.vue +210 -0
- package/components/u-col/u-col.vue +135 -0
- package/components/u-collapse/u-collapse.vue +82 -0
- package/components/u-collapse-item/u-collapse-item.vue +190 -0
- package/components/u-column-notice/u-column-notice.vue +264 -0
- package/components/u-count-down/u-count-down.vue +333 -0
- package/components/u-count-to/u-count-to.vue +297 -0
- package/components/u-divider/u-divider.vue +141 -0
- package/components/u-dropdown/u-dropdown.vue +311 -0
- package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
- package/components/u-empty/u-empty.vue +111 -0
- package/components/u-field/u-field.vue +469 -0
- package/components/u-form/u-form.vue +162 -0
- package/components/u-form-item/u-form-item.vue +476 -0
- package/components/u-full-screen/u-full-screen.vue +80 -0
- package/components/u-gap/u-gap.vue +48 -0
- package/components/u-grid/u-grid.vue +101 -0
- package/components/u-grid-item/u-grid-item.vue +136 -0
- package/components/u-icon/u-icon.vue +389 -0
- package/components/u-image/types.ts +48 -0
- package/components/u-image/u-image.vue +218 -0
- package/components/u-index-anchor/u-index-anchor.vue +101 -0
- package/components/u-index-list/u-index-list.vue +376 -0
- package/components/u-input/u-input.vue +462 -0
- package/components/u-keyboard/u-keyboard.vue +188 -0
- package/components/u-lazy-load/u-lazy-load.vue +288 -0
- package/components/u-line/u-line.vue +71 -0
- package/components/u-line-progress/u-line-progress.vue +128 -0
- package/components/u-link/u-link.vue +87 -0
- package/components/u-loading/u-loading.vue +111 -0
- package/components/u-loadmore/u-loadmore.vue +205 -0
- package/components/u-mask/u-mask.vue +137 -0
- package/components/u-message-input/u-message-input.vue +315 -0
- package/components/u-modal/u-modal.vue +284 -0
- package/components/u-navbar/u-navbar.vue +314 -0
- package/components/u-no-network/image.ts +2 -0
- package/components/u-no-network/u-no-network.vue +311 -0
- package/components/u-notice-bar/u-notice-bar.vue +274 -0
- package/components/u-number-box/u-number-box.vue +344 -0
- package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
- package/components/u-parse/libs/CssHandler.js +100 -0
- package/components/u-parse/libs/MpHtmlParser.js +580 -0
- package/components/u-parse/libs/config.js +80 -0
- package/components/u-parse/libs/handler.wxs +22 -0
- package/components/u-parse/libs/trees.vue +505 -0
- package/components/u-parse/u-parse.vue +645 -0
- package/components/u-picker/u-picker.vue +808 -0
- package/components/u-popup/u-popup.vue +404 -0
- package/components/u-radio/u-radio.vue +272 -0
- package/components/u-radio-group/u-radio-group.vue +116 -0
- package/components/u-rate/u-rate.vue +349 -0
- package/components/u-read-more/u-read-more.vue +199 -0
- package/components/u-row/u-row.vue +95 -0
- package/components/u-row-notice/u-row-notice.vue +273 -0
- package/components/u-search/u-search.vue +298 -0
- package/components/u-section/u-section.vue +175 -0
- package/components/u-select/u-select.vue +387 -0
- package/components/u-skeleton/u-skeleton.vue +230 -0
- package/components/u-slider/u-slider.vue +293 -0
- package/components/u-steps/u-steps.vue +200 -0
- package/components/u-sticky/u-sticky.vue +189 -0
- package/components/u-subsection/u-subsection.vue +388 -0
- package/components/u-swipe-action/u-swipe-action.vue +289 -0
- package/components/u-swiper/u-swiper.vue +305 -0
- package/components/u-switch/u-switch.vue +146 -0
- package/components/u-tabbar/u-tabbar.vue +347 -0
- package/components/u-table/u-table.vue +104 -0
- package/components/u-tabs/u-tabs.vue +322 -0
- package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
- package/components/u-tag/u-tag.vue +270 -0
- package/components/u-td/u-td.vue +76 -0
- package/components/u-th/u-th.vue +70 -0
- package/components/u-time-line/u-time-line.vue +39 -0
- package/components/u-time-line-item/u-time-line-item.vue +88 -0
- package/components/u-toast/types.ts +4 -0
- package/components/u-toast/u-toast.vue +238 -0
- package/components/u-top-tips/u-top-tips.vue +118 -0
- package/components/u-tr/u-tr.vue +24 -0
- package/components/u-upload/u-upload.vue +600 -0
- package/components/u-verification-code/u-verification-code.vue +194 -0
- package/components/u-waterfall/u-waterfall.vue +186 -0
- package/iconfont.css +910 -0
- package/index.scss +23 -0
- package/index.ts +166 -0
- package/libs/config/config.ts +26 -0
- package/libs/config/zIndex.ts +37 -0
- package/libs/css/color.scss +155 -0
- package/libs/css/common.scss +176 -0
- package/libs/css/style.components.scss +7 -0
- package/libs/css/style.h5.scss +8 -0
- package/libs/css/style.mp.scss +72 -0
- package/libs/css/style.nvue.scss +3 -0
- package/libs/css/style.vue.scss +175 -0
- package/libs/function/$parent.ts +22 -0
- package/libs/function/addUnit.ts +13 -0
- package/libs/function/color.ts +37 -0
- package/libs/function/colorGradient.ts +123 -0
- package/libs/function/debounce.ts +28 -0
- package/libs/function/deepClone.ts +39 -0
- package/libs/function/deepMerge.ts +34 -0
- package/libs/function/getParent.ts +59 -0
- package/libs/function/getRect.ts +26 -0
- package/libs/function/guid.ts +42 -0
- package/libs/function/md5.ts +397 -0
- package/libs/function/parent.ts +21 -0
- package/libs/function/queryParams.ts +60 -0
- package/libs/function/random.ts +16 -0
- package/libs/function/randomArray.ts +11 -0
- package/libs/function/route.ts +118 -0
- package/libs/function/sys.ts +15 -0
- package/libs/function/test.ts +229 -0
- package/libs/function/throttle.ts +31 -0
- package/libs/function/timeFormat.ts +54 -0
- package/libs/function/timeFrom.ts +48 -0
- package/libs/function/toast.ts +14 -0
- package/libs/function/trim.ts +21 -0
- package/libs/function/type2icon.ts +36 -0
- package/libs/hooks/useEmitter.ts +77 -0
- package/libs/hooks/useParent.ts +29 -0
- package/libs/request/index.ts +237 -0
- package/libs/store/index.ts +88 -0
- package/libs/util/area.ts +1 -0
- package/libs/util/async-validator.js +1356 -0
- package/libs/util/city.ts +1 -0
- package/libs/util/emitter.ts +112 -0
- package/libs/util/mitt.ts +118 -0
- package/libs/util/parent.ts +20 -0
- package/libs/util/province.ts +1 -0
- package/package.json +98 -0
- package/readme.md +165 -0
- package/theme.scss +38 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<view v-if="loading" :style="rootStyle" @touchmove.stop.prevent>
|
|
3
|
+
<view v-for="item in RectNodes" :key="$u.guid()" :class="[animation ? 'skeleton-fade' : '']" :style="rectStyle(item)"></view>
|
|
4
|
+
<view v-for="item in circleNodes" :key="$u.guid()" :class="[animation ? 'skeleton-fade' : '']" :style="circleStyle(item)"></view>
|
|
5
|
+
<view v-for="item in filletNodes" :key="$u.guid()" :class="[animation ? 'skeleton-fade' : '']" :style="filletStyle(item)"></view>
|
|
6
|
+
</view>
|
|
7
|
+
</template>
|
|
8
|
+
|
|
9
|
+
<script setup lang="ts">
|
|
10
|
+
import { ref, computed, onMounted, getCurrentInstance } from 'vue';
|
|
11
|
+
import { $u } from '../..';
|
|
12
|
+
|
|
13
|
+
defineOptions({ name: 'u-skeleton' });
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* skeleton 骨架屏
|
|
17
|
+
* @description 骨架屏一般用于页面在请求远程数据尚未完成时,页面用灰色块预显示本来的页面结构,给用户更好的体验。
|
|
18
|
+
* @tutorial https://www.uviewui.com/components/skeleton.html
|
|
19
|
+
* @property {String} elColor 骨架块状元素的背景颜色(默认#e5e5e5)
|
|
20
|
+
* @property {String} bgColor 骨架组件背景颜色(默认#ffffff)
|
|
21
|
+
* @property {Boolean} animation 骨架块是否显示动画效果(默认false)
|
|
22
|
+
* @property {String|Number} borderRadius u-skeleton-fillet类名元素,对应的骨架块的圆角大小,单位rpx(默认10)
|
|
23
|
+
* @property {Boolean} loading 是否显示骨架组件,请求完成后,将此值设置为false(默认true)
|
|
24
|
+
* @example <u-skeleton :loading="true" :animation="true"></u-skeleton>
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const props = defineProps({
|
|
28
|
+
/** 骨架块状元素的背景颜色 */
|
|
29
|
+
elColor: {
|
|
30
|
+
type: String,
|
|
31
|
+
default: '#e5e5e5'
|
|
32
|
+
},
|
|
33
|
+
/** 整个骨架屏页面的背景颜色 */
|
|
34
|
+
bgColor: {
|
|
35
|
+
type: String,
|
|
36
|
+
default: '#ffffff'
|
|
37
|
+
},
|
|
38
|
+
/** 是否显示加载动画 */
|
|
39
|
+
animation: {
|
|
40
|
+
type: Boolean,
|
|
41
|
+
default: false
|
|
42
|
+
},
|
|
43
|
+
/** 圆角值,只对类名为u-skeleton-fillet的元素生效,为数值,不带单位 */
|
|
44
|
+
borderRadius: {
|
|
45
|
+
type: [String, Number],
|
|
46
|
+
default: 10
|
|
47
|
+
},
|
|
48
|
+
/** 是否显示骨架,true-显示,false-隐藏 */
|
|
49
|
+
loading: {
|
|
50
|
+
type: Boolean,
|
|
51
|
+
default: true
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const instance = getCurrentInstance();
|
|
56
|
+
const windowWinth = ref(750); // 骨架屏宽度
|
|
57
|
+
const windowHeight = ref(1500); // 骨架屏高度
|
|
58
|
+
const filletNodes = ref<any[]>([]); // 圆角骨架元素
|
|
59
|
+
const circleNodes = ref<any[]>([]); // 圆形骨架元素
|
|
60
|
+
const RectNodes = ref<any[]>([]); // 矩形骨架元素
|
|
61
|
+
const top = ref(0);
|
|
62
|
+
const left = ref(0);
|
|
63
|
+
|
|
64
|
+
// 计算根元素的样式
|
|
65
|
+
const rootStyle = computed(() => ({
|
|
66
|
+
width: windowWinth.value + 'px',
|
|
67
|
+
height: windowHeight.value + 'px',
|
|
68
|
+
backgroundColor: props.bgColor,
|
|
69
|
+
position: 'absolute' as const,
|
|
70
|
+
left: left.value + 'px',
|
|
71
|
+
top: top.value + 'px',
|
|
72
|
+
zIndex: 9998,
|
|
73
|
+
overflow: 'hidden'
|
|
74
|
+
}));
|
|
75
|
+
|
|
76
|
+
// 矩形骨架元素样式
|
|
77
|
+
const rectStyle = (item: any) => ({
|
|
78
|
+
width: item.width + 'px',
|
|
79
|
+
height: item.height + 'px',
|
|
80
|
+
backgroundColor: props.elColor,
|
|
81
|
+
position: 'absolute' as const,
|
|
82
|
+
left: item.left - left.value + 'px',
|
|
83
|
+
top: item.top - top.value + 'px'
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// 圆形骨架元素样式
|
|
87
|
+
const circleStyle = (item: any) => ({
|
|
88
|
+
width: item.width + 'px',
|
|
89
|
+
height: item.height + 'px',
|
|
90
|
+
backgroundColor: props.elColor,
|
|
91
|
+
borderRadius: item.width / 2 + 'px',
|
|
92
|
+
position: 'absolute' as const,
|
|
93
|
+
left: item.left - left.value + 'px',
|
|
94
|
+
top: item.top - top.value + 'px'
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// 圆角骨架元素样式
|
|
98
|
+
const filletStyle = (item: any) => ({
|
|
99
|
+
width: item.width + 'px',
|
|
100
|
+
height: item.height + 'px',
|
|
101
|
+
backgroundColor: props.elColor,
|
|
102
|
+
borderRadius: props.borderRadius + 'rpx',
|
|
103
|
+
position: 'absolute' as const,
|
|
104
|
+
left: item.left - left.value + 'px',
|
|
105
|
+
top: item.top - top.value + 'px'
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 查询各节点的信息
|
|
110
|
+
* @description 获取骨架屏各类型元素的位置信息
|
|
111
|
+
*/
|
|
112
|
+
function selecterQueryInfo() {
|
|
113
|
+
// 获取整个父组件容器的高度,当做骨架屏的高度
|
|
114
|
+
// 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
|
|
115
|
+
let query: any = '';
|
|
116
|
+
// #ifdef MP-WEIXIN
|
|
117
|
+
query = uni.createSelectorQuery().in(instance?.proxy?.$parent);
|
|
118
|
+
// #endif
|
|
119
|
+
// #ifndef MP-WEIXIN
|
|
120
|
+
query = uni.createSelectorQuery();
|
|
121
|
+
// #endif
|
|
122
|
+
query
|
|
123
|
+
.selectAll('.u-skeleton')
|
|
124
|
+
.boundingClientRect()
|
|
125
|
+
.exec((res: any) => {
|
|
126
|
+
windowHeight.value = res[0][0].height;
|
|
127
|
+
windowWinth.value = res[0][0].width;
|
|
128
|
+
top.value = res[0][0].bottom - res[0][0].height;
|
|
129
|
+
left.value = res[0][0].left;
|
|
130
|
+
});
|
|
131
|
+
// 矩形骨架元素
|
|
132
|
+
getRectEls();
|
|
133
|
+
// 圆形骨架元素
|
|
134
|
+
getCircleEls();
|
|
135
|
+
// 圆角骨架元素
|
|
136
|
+
getFilletEls();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 获取矩形骨架元素
|
|
141
|
+
*/
|
|
142
|
+
function getRectEls() {
|
|
143
|
+
let query: any = '';
|
|
144
|
+
// 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
|
|
145
|
+
// #ifdef MP-WEIXIN
|
|
146
|
+
query = uni.createSelectorQuery().in(instance?.proxy?.$parent);
|
|
147
|
+
// #endif
|
|
148
|
+
// #ifndef MP-WEIXIN
|
|
149
|
+
query = uni.createSelectorQuery();
|
|
150
|
+
// #endif
|
|
151
|
+
query
|
|
152
|
+
.selectAll('.u-skeleton-rect')
|
|
153
|
+
.boundingClientRect()
|
|
154
|
+
.exec((res: any) => {
|
|
155
|
+
RectNodes.value = res[0];
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 获取圆角骨架元素
|
|
161
|
+
*/
|
|
162
|
+
function getFilletEls() {
|
|
163
|
+
let query: any = '';
|
|
164
|
+
// 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
|
|
165
|
+
// #ifdef MP-WEIXIN
|
|
166
|
+
query = uni.createSelectorQuery().in(instance?.proxy?.$parent);
|
|
167
|
+
// #endif
|
|
168
|
+
// #ifndef MP-WEIXIN
|
|
169
|
+
query = uni.createSelectorQuery();
|
|
170
|
+
// #endif
|
|
171
|
+
query
|
|
172
|
+
.selectAll('.u-skeleton-fillet')
|
|
173
|
+
.boundingClientRect()
|
|
174
|
+
.exec((res: any) => {
|
|
175
|
+
filletNodes.value = res[0];
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 获取圆形骨架元素
|
|
181
|
+
*/
|
|
182
|
+
function getCircleEls() {
|
|
183
|
+
let query: any = '';
|
|
184
|
+
// 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
|
|
185
|
+
// #ifdef MP-WEIXIN
|
|
186
|
+
query = uni.createSelectorQuery().in(instance?.proxy?.$parent);
|
|
187
|
+
// #endif
|
|
188
|
+
// #ifndef MP-WEIXIN
|
|
189
|
+
query = uni.createSelectorQuery();
|
|
190
|
+
// #endif
|
|
191
|
+
query
|
|
192
|
+
.selectAll('.u-skeleton-circle')
|
|
193
|
+
.boundingClientRect()
|
|
194
|
+
.exec((res: any) => {
|
|
195
|
+
circleNodes.value = res[0];
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
onMounted(() => {
|
|
200
|
+
// 获取系统信息
|
|
201
|
+
const systemInfo = uni.getSystemInfoSync();
|
|
202
|
+
windowHeight.value = systemInfo.windowHeight;
|
|
203
|
+
windowWinth.value = systemInfo.windowWidth;
|
|
204
|
+
selecterQueryInfo();
|
|
205
|
+
});
|
|
206
|
+
</script>
|
|
207
|
+
|
|
208
|
+
<style lang="scss" scoped>
|
|
209
|
+
@import '../../libs/css/style.components.scss';
|
|
210
|
+
.skeleton-fade {
|
|
211
|
+
width: 100%;
|
|
212
|
+
height: 100%;
|
|
213
|
+
background: rgb(194, 207, 214);
|
|
214
|
+
animation-duration: 1.5s;
|
|
215
|
+
animation-name: blink;
|
|
216
|
+
animation-timing-function: ease-in-out;
|
|
217
|
+
animation-iteration-count: infinite;
|
|
218
|
+
}
|
|
219
|
+
@keyframes blink {
|
|
220
|
+
0% {
|
|
221
|
+
opacity: 1;
|
|
222
|
+
}
|
|
223
|
+
50% {
|
|
224
|
+
opacity: 0.4;
|
|
225
|
+
}
|
|
226
|
+
100% {
|
|
227
|
+
opacity: 1;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
</style>
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<view
|
|
3
|
+
class="u-slider"
|
|
4
|
+
@tap="onClick"
|
|
5
|
+
:class="[disabled ? 'u-slider--disabled' : '']"
|
|
6
|
+
:style="{
|
|
7
|
+
backgroundColor: inactiveColor
|
|
8
|
+
}"
|
|
9
|
+
>
|
|
10
|
+
<view
|
|
11
|
+
class="u-slider__gap"
|
|
12
|
+
:style="[
|
|
13
|
+
barStyle,
|
|
14
|
+
{
|
|
15
|
+
height: height + 'rpx',
|
|
16
|
+
backgroundColor: activeColor
|
|
17
|
+
}
|
|
18
|
+
]"
|
|
19
|
+
>
|
|
20
|
+
<view class="u-slider__button-wrap" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd" @touchcancel="onTouchEnd">
|
|
21
|
+
<slot v-if="slots.default" />
|
|
22
|
+
<view
|
|
23
|
+
v-else
|
|
24
|
+
class="u-slider__button"
|
|
25
|
+
:style="[
|
|
26
|
+
blockStyle,
|
|
27
|
+
{
|
|
28
|
+
height: blockWidth + 'rpx',
|
|
29
|
+
width: blockWidth + 'rpx',
|
|
30
|
+
backgroundColor: blockColor
|
|
31
|
+
}
|
|
32
|
+
]"
|
|
33
|
+
/>
|
|
34
|
+
</view>
|
|
35
|
+
</view>
|
|
36
|
+
</view>
|
|
37
|
+
</template>
|
|
38
|
+
|
|
39
|
+
<script setup lang="ts">
|
|
40
|
+
/**
|
|
41
|
+
* slider 滑块选择器
|
|
42
|
+
* @tutorial https://uviewui.com/components/slider.html
|
|
43
|
+
* @property {Number | String} value 滑块默认值(默认0)
|
|
44
|
+
* @property {Number | String} min 最小值(默认0)
|
|
45
|
+
* @property {Number | String} max 最大值(默认100)
|
|
46
|
+
* @property {Number | String} step 步长(默认1)
|
|
47
|
+
* @property {Number | String} blockWidth 滑块宽度,高等于宽(30)
|
|
48
|
+
* @property {Number | String} height 滑块条高度,单位rpx(默认6)
|
|
49
|
+
* @property {String} inactiveColor 底部条背景颜色(默认#c0c4cc)
|
|
50
|
+
* @property {String} activeColor 底部选择部分的背景颜色(默认#2979ff)
|
|
51
|
+
* @property {String} blockColor 滑块颜色(默认#ffffff)
|
|
52
|
+
* @property {Object} blockStyle 给滑块自定义样式,对象形式
|
|
53
|
+
* @property {Boolean} disabled 是否禁用滑块(默认为false)
|
|
54
|
+
* @event start 滑动触发
|
|
55
|
+
* @event moving 正在滑动中
|
|
56
|
+
* @event end 滑动结束
|
|
57
|
+
* @example <u-slider v-model="value" />
|
|
58
|
+
*/
|
|
59
|
+
import { ref, watch, onMounted, useSlots, getCurrentInstance } from 'vue';
|
|
60
|
+
import { $u } from '../..';
|
|
61
|
+
|
|
62
|
+
defineOptions({ name: 'u-slider' });
|
|
63
|
+
|
|
64
|
+
const emit = defineEmits(['update:modelValue', 'start', 'moving', 'end']);
|
|
65
|
+
|
|
66
|
+
const props = defineProps({
|
|
67
|
+
/** 当前进度百分比值,范围0-100 */
|
|
68
|
+
modelValue: {
|
|
69
|
+
type: [Number, String],
|
|
70
|
+
default: 0
|
|
71
|
+
},
|
|
72
|
+
/** 是否禁用滑块 */
|
|
73
|
+
disabled: {
|
|
74
|
+
type: Boolean,
|
|
75
|
+
default: false
|
|
76
|
+
},
|
|
77
|
+
/** 滑块宽度,高等于宽,单位rpx */
|
|
78
|
+
blockWidth: {
|
|
79
|
+
type: [Number, String],
|
|
80
|
+
default: 30
|
|
81
|
+
},
|
|
82
|
+
/** 最小值 */
|
|
83
|
+
min: {
|
|
84
|
+
type: [Number, String],
|
|
85
|
+
default: 0
|
|
86
|
+
},
|
|
87
|
+
/** 最大值 */
|
|
88
|
+
max: {
|
|
89
|
+
type: [Number, String],
|
|
90
|
+
default: 100
|
|
91
|
+
},
|
|
92
|
+
/** 步进值 */
|
|
93
|
+
step: {
|
|
94
|
+
type: [Number, String],
|
|
95
|
+
default: 1
|
|
96
|
+
},
|
|
97
|
+
/** 滑块条高度,单位rpx */
|
|
98
|
+
height: {
|
|
99
|
+
type: [Number, String],
|
|
100
|
+
default: 6
|
|
101
|
+
},
|
|
102
|
+
/** 进度条的激活部分颜色 */
|
|
103
|
+
activeColor: {
|
|
104
|
+
type: String,
|
|
105
|
+
default: '#2979ff'
|
|
106
|
+
},
|
|
107
|
+
/** 进度条的背景颜色 */
|
|
108
|
+
inactiveColor: {
|
|
109
|
+
type: String,
|
|
110
|
+
default: '#c0c4cc'
|
|
111
|
+
},
|
|
112
|
+
/** 滑块的背景颜色 */
|
|
113
|
+
blockColor: {
|
|
114
|
+
type: String,
|
|
115
|
+
default: '#ffffff'
|
|
116
|
+
},
|
|
117
|
+
/** 用户对滑块的自定义颜色 */
|
|
118
|
+
blockStyle: {
|
|
119
|
+
type: Object as () => Record<string, any>,
|
|
120
|
+
default: () => ({})
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const slots = useSlots();
|
|
125
|
+
const instance = getCurrentInstance();
|
|
126
|
+
|
|
127
|
+
// 滑块条的尺寸信息
|
|
128
|
+
const sliderRect = ref<{ left: number; width: number }>({ left: 0, width: 0 });
|
|
129
|
+
const startX = ref(0);
|
|
130
|
+
const status = ref<'start' | 'moving' | 'end'>('end');
|
|
131
|
+
const newValue = ref(0);
|
|
132
|
+
const distanceX = ref(0);
|
|
133
|
+
const startValue = ref(0);
|
|
134
|
+
const barStyle = ref<Record<string, any>>({});
|
|
135
|
+
|
|
136
|
+
// 监听 value 变化,非滑动状态时才更新滑块值
|
|
137
|
+
watch(
|
|
138
|
+
() => props.modelValue,
|
|
139
|
+
n => {
|
|
140
|
+
// 只有在非滑动状态时,才可以通过modelValue更新滑块值,这里监听,是为了让用户触发
|
|
141
|
+
if (status.value === 'end') updateValue(props.modelValue, false);
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
onMounted(() => {
|
|
146
|
+
// 获取滑块条的尺寸信息
|
|
147
|
+
$u.getRect('.u-slider', instance).then((rect: { left: number; width: number }) => {
|
|
148
|
+
sliderRect.value = rect;
|
|
149
|
+
});
|
|
150
|
+
updateValue(props.modelValue, false);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 触摸开始
|
|
155
|
+
*/
|
|
156
|
+
function onTouchStart(event: TouchEvent) {
|
|
157
|
+
if (props.disabled) return;
|
|
158
|
+
startX.value = 0;
|
|
159
|
+
// 触摸点集
|
|
160
|
+
const touches = event.touches[0];
|
|
161
|
+
// 触摸点到屏幕左边的距离
|
|
162
|
+
startX.value = touches.clientX;
|
|
163
|
+
// 此处的props.modelValue虽为props值,但是通过emit('update:modelValue')进行了修改
|
|
164
|
+
startValue.value = format(props.modelValue);
|
|
165
|
+
// 标示当前的状态为开始触摸滑动
|
|
166
|
+
status.value = 'start';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 触摸移动
|
|
171
|
+
*/
|
|
172
|
+
function onTouchMove(event: TouchEvent) {
|
|
173
|
+
if (props.disabled) return;
|
|
174
|
+
// 连续触摸的过程会一直触发本方法,但只有手指触发且移动了才被认为是拖动了,才发出事件
|
|
175
|
+
// 触摸后第一次移动已经将status设置为moving状态,故触摸第二次移动不会触发本事件
|
|
176
|
+
if (status.value === 'start') emit('start');
|
|
177
|
+
const touches = event.touches[0];
|
|
178
|
+
// 滑块的左边不一定跟屏幕左边接壤,所以需要减去最外层父元素的左边值
|
|
179
|
+
distanceX.value = touches.clientX - sliderRect.value.left;
|
|
180
|
+
// 获得移动距离对整个滑块的百分比值,此为带有多位小数的值,不能用此更新视图
|
|
181
|
+
// 否则造成通信阻塞,需要每改变一个step值时修改一次视图
|
|
182
|
+
newValue.value = (distanceX.value / sliderRect.value.width) * 100;
|
|
183
|
+
status.value = 'moving';
|
|
184
|
+
// 发出moving事件
|
|
185
|
+
emit('moving');
|
|
186
|
+
updateValue(newValue.value, true);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 触摸结束
|
|
191
|
+
*/
|
|
192
|
+
function onTouchEnd() {
|
|
193
|
+
if (props.disabled) return;
|
|
194
|
+
if (status.value === 'moving') {
|
|
195
|
+
updateValue(newValue.value, false);
|
|
196
|
+
emit('end');
|
|
197
|
+
}
|
|
198
|
+
status.value = 'end';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 更新滑块值
|
|
203
|
+
* @param value 新值
|
|
204
|
+
* @param drag 是否为拖动
|
|
205
|
+
*/
|
|
206
|
+
function updateValue(value: number | string, drag: boolean) {
|
|
207
|
+
// 去掉小数部分,同时也是对step步进的处理
|
|
208
|
+
const width = format(value);
|
|
209
|
+
// 不允许滑动的值超过max最大值,百分比也不能超过100
|
|
210
|
+
if (width > Number(props.max) || width > 100) return;
|
|
211
|
+
// 设置移动的百分比值
|
|
212
|
+
const style: Record<string, any> = {
|
|
213
|
+
width: width + '%'
|
|
214
|
+
};
|
|
215
|
+
// 移动期间无需过渡动画
|
|
216
|
+
if (drag === true) {
|
|
217
|
+
style.transition = 'none';
|
|
218
|
+
} else {
|
|
219
|
+
// 非移动期间,删掉对过渡为空的声明,让css中的声明起效
|
|
220
|
+
delete style.transition;
|
|
221
|
+
}
|
|
222
|
+
// 修改value值
|
|
223
|
+
emit('update:modelValue', width);
|
|
224
|
+
barStyle.value = style;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 格式化滑块值
|
|
229
|
+
* @param value 输入值
|
|
230
|
+
* @returns 处理后的值
|
|
231
|
+
*/
|
|
232
|
+
function format(value: number | string): number {
|
|
233
|
+
// 将小数变成整数,为了减少对视图的更新,造成视图层与逻辑层的阻塞
|
|
234
|
+
return Math.round(Math.max(Number(props.min), Math.min(Number(value), Number(props.max))) / Number(props.step)) * Number(props.step);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* 点击滑块条
|
|
239
|
+
*/
|
|
240
|
+
function onClick(event: any) {
|
|
241
|
+
if (props.disabled) return;
|
|
242
|
+
// 直接点击滑块的情况,计算方式与onTouchMove方法相同
|
|
243
|
+
const value = ((event.detail.x - sliderRect.value.left) / sliderRect.value.width) * 100;
|
|
244
|
+
updateValue(value, false);
|
|
245
|
+
}
|
|
246
|
+
</script>
|
|
247
|
+
|
|
248
|
+
<style lang="scss" scoped>
|
|
249
|
+
@import '../../libs/css/style.components.scss';
|
|
250
|
+
|
|
251
|
+
.u-slider {
|
|
252
|
+
position: relative;
|
|
253
|
+
border-radius: 999px;
|
|
254
|
+
background-color: #ebedf0;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
.u-slider:before {
|
|
258
|
+
position: absolute;
|
|
259
|
+
right: 0;
|
|
260
|
+
left: 0;
|
|
261
|
+
content: '';
|
|
262
|
+
top: -8px;
|
|
263
|
+
bottom: -8px;
|
|
264
|
+
z-index: -1;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
.u-slider__gap {
|
|
268
|
+
position: relative;
|
|
269
|
+
border-radius: inherit;
|
|
270
|
+
transition: width 0.2s;
|
|
271
|
+
background-color: #1989fa;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
.u-slider__button {
|
|
275
|
+
width: 24px;
|
|
276
|
+
height: 24px;
|
|
277
|
+
border-radius: 50%;
|
|
278
|
+
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
|
|
279
|
+
background-color: #fff;
|
|
280
|
+
cursor: pointer;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
.u-slider__button-wrap {
|
|
284
|
+
position: absolute;
|
|
285
|
+
top: 50%;
|
|
286
|
+
right: 0;
|
|
287
|
+
transform: translate3d(50%, -50%, 0);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
.u-slider--disabled {
|
|
291
|
+
opacity: 0.5;
|
|
292
|
+
}
|
|
293
|
+
</style>
|