tui-index-list.vue 15.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
<template>
	<view class="tui-index-list">
		<scroll-view class="tui-scroll__view" :style="{ height: getHeight }" scroll-y :scroll-top="scrollTop"
			@scroll="scroll">
			<slot name="header"></slot>
			<view class="tui-content__box">
				<view class="tui-item__select" v-for="(item, index) in listData" :key="index">
					<view v-if="index == listItemCur" class="tui-content__title"
						:class="{ 'tui-line__top': topLine, 'tui-line__bottom': bottomLine }">
						<view class="tui-title__item"
							:style="{ background: background_cur, color: color_cur, fontSize: size, height: height, padding: padding }">
							{{ item.letter }}
						</view>
					</view>
					<view v-else-if="index == listItemCur + 1" class="tui-content__title"
						:class="{ 'tui-line__top': topLine, 'tui-line__bottom': bottomLine }">
						<view class="tui-title__item"
							:style="{ background: background_next, color: color_next, fontSize: size, height: height, padding: padding }">
							{{ item.letter }}
						</view>
					</view>
					<view v-else class="tui-content__title"
						:class="{ 'tui-line__top': topLine, 'tui-line__bottom': bottomLine }">
						<view class="tui-title__item"
							:style="{ background: background, color: color, fontSize: size, height: height, padding: padding }">
							{{ item.letter }}
						</view>
					</view>
					<slot name="item" :entity="item.data" :index="index"></slot>
				</view>
			</view>
			<slot name="footer"></slot>
		</scroll-view>
		<view class="tui-index__indicator"
			:class="[touching && indicatorTop != -1 ? 'tui-indicator__show' : '', treeKeyTran ? 'tui-indicator__tran' : '']"
			:style="{ top: indicatorTop + 'px' }">
			{{ listData[treeItemCur] && listData[treeItemCur].letter }}
		</view>
		<view id="tui_index__letter" class="tui-index__letter" @touchstart.stop="touchStart"
			@touchmove.stop.prevent="touchMove" @touchend.stop="touchEnd" @touchcancel.stop="touchEnd">
			<view class="tui-letter__item" :class="[index === treeItemCur ? 'tui-letter__cur' : '']"
				v-for="(item, index) in listData" :key="index" @tap="letterClick(index,item.letter)">
				<view class="tui-letter__key"
					:style="{ background: index === treeItemCur ? getActiveKeyBgColor : '', color: index === treeItemCur ? activeKeyColor : keyColor }">
					{{ item.letter }}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	let ColorUtil = {
		rgbToHex(r, g, b) {
			let hex = ((r << 16) | (g << 8) | b).toString(16);
			return '#' + new Array(Math.abs(hex.length - 7)).join('0') + hex;
		},
		hexToRgb(hex) {
			let rgb = [];
			if (hex.length === 4) {
				let text = hex.substring(1, 4);
				hex = '#' + text + text;
			}
			for (let i = 1; i < 7; i += 2) {
				rgb.push(parseInt('0x' + hex.slice(i, i + 2)));
			}
			return rgb;
		},
		/**
		 * 生成渐变过渡色数组 {startColor: 开始颜色值, endColor: 结束颜色值, step: 生成色值数组长度}
		 */
		gradient(startColor, endColor, step) {
			// 将hex转换为rgb
			let sColor = this.hexToRgb(startColor),
				eColor = this.hexToRgb(endColor);

			// 计算R\G\B每一步的差值
			let rStep = (eColor[0] - sColor[0]) / step,
				gStep = (eColor[1] - sColor[1]) / step,
				bStep = (eColor[2] - sColor[2]) / step;

			let gradientColorArr = [];
			for (let i = 0; i < step; i++) {
				// 计算每一步的hex值
				gradientColorArr.push(this.rgbToHex(parseInt(rStep * i + sColor[0]), parseInt(gStep * i + sColor[1]),
					parseInt(bStep * i + sColor[2])));
			}
			return gradientColorArr;
		},
		/**
		 * 生成随机颜色值
		 */
		generateColor() {
			let color = '#';
			for (let i = 0; i < 6; i++) {
				color += ((Math.random() * 16) | 0).toString(16);
			}
			return color;
		}
	};
	export default {
		name: 'tuiIndexList',
		emits: ['letterClick'],
		props: {
			// 数据源
			listData: {
				type: Array,
				default () {
					return [];
				}
			},
			// 顶部高度
			top: {
				type: Number,
				default: 0
			},
			// 底部高度
			bottom: {
				type: Number,
				default: 0
			},
			//top和bottom单位,可传rpx 或 px
			unit: {
				type: String,
				default: 'px'
			},
			//sticky letter 是否显示上边线条
			topLine: {
				type: Boolean,
				default: true
			},
			//sticky letter 是否显示下边线条
			bottomLine: {
				type: Boolean,
				default: true
			},
			height: {
				type: String,
				default: '60rpx'
			},
			color: {
				type: String,
				default: '#666'
			},
			activeColor: {
				type: String,
				default: ''
			},
			size: {
				type: String,
				default: '26rpx'
			},
			background: {
				type: String,
				default: '#ededed'
			},
			activeBackground: {
				type: String,
				default: '#FFFFFF'
			},
			padding: {
				type: String,
				default: '0 20rpx'
			},
			keyColor: {
				type: String,
				default: '#666'
			},
			activeKeyColor: {
				type: String,
				default: '#FFFFFF'
			},
			activeKeyBackground: {
				type: String,
				default: ''
			},
			//重新初始化[可异步加载时使用,设置大于0的数]
			reinit: {
				type: Number,
				default: 0
			}
		},
		computed: {
			getHeight() {
				return `calc(100vh - ${this.top + this.bottom + this.unit})`;
			},
			getChange() {
				return `${this.top}-${this.bottom}-${this.reinit}`;
			},
			getActiveColor(){
				return this.activeColor || (uni && uni.$tui && uni.$tui.color.primary) || '#5677fc'
			},
			getActiveKeyBgColor(){
				return this.activeKeyBackground || (uni && uni.$tui && uni.$tui.color.primary) || '#5677fc'
			}
		},
		watch: {
			listData(val) {
				this.init();
			},
			getChange(val) {
				this.init();
			}
		},
		data() {
			return {
				remScale: 1, // 缩放比例
				realTop: 0, // 计算后顶部高度实际值
				realBottom: 0, // 计算后底部高度实际值
				treeInfo: {
					// 索引树节点信息
					treeTop: 0,
					treeBottom: 0,
					itemHeight: 0,
					itemMount: 0
				},
				indicatorTopList: [], // 指示器节点信息列表
				maxScrollTop: 0, // 最大滚动高度
				blocks: [], // 节点组信息

				/* 渲染数据 */
				treeItemCur: -1, // 索引树的聚焦项
				listItemCur: -1, // 节点树的聚焦项
				touching: false, // 是否在触摸索引树中
				scrollTop: 0, // 节点树滚动高度
				indicatorTop: -1, // 指示器顶部距离
				treeKeyTran: false,
				background_cur: '',
				color_cur: '',
				background_next: '',
				color_next: '',
				colors: [],
				backgroundColors: []
			};
		},
		methods: {
			scroll(e) {
				if (this.touching) return;

				let scrollTop = e.detail.scrollTop;
				if (scrollTop > this.maxScrollTop) return;

				let blocks = this.blocks,
					stickyTitleHeight = this.remScale * 30;
				let len = blocks.length - 1;
				this.background_cur = this.background;
				this.color_cur = this.color;
				for (let i = len; i >= 0; i--) {
					let block = blocks[i];
					// 判断当前滚动值 scrollTop 所在区间, 以得到当前聚焦项
					if (scrollTop >= block.itemTop && scrollTop < block.itemBottom) {
						// 判断当前滚动值 scrollTop 是否在当前聚焦项底一个 .block__title 高度范围内, 如果是则开启过度色值计算
						if (scrollTop > block.itemBottom - stickyTitleHeight) {
							let percent = Math.floor(((scrollTop - (block.itemBottom - stickyTitleHeight)) /
								stickyTitleHeight) * 100);
							this.background_cur = this.backgroundColors[percent];
							this.color_cur = this.colors[percent];
							this.background_next = this.backgroundColors[100 - percent];
							this.color_next = this.colors[100 - percent];
							this.treeItemCur = i;
							this.listItemCur = i;
						} else if (scrollTop <= block.itemBottom - stickyTitleHeight) {
							this.background_cur = this.activeBackground;
							this.color_cur = this.getActiveColor;
							this.background_next = this.background;
							this.color_next = this.color;
							this.treeItemCur = i;
							this.listItemCur = i;
						}
						break;
					}
				}
			},
			/**
			 * tree 触摸开始
			 */
			touchStart(e) {
				// 获取触摸点信息
				let startTouch = e.changedTouches[0];
				if (!startTouch) return;
				this.touching = true;
				let treeItemCur = this.getCurrentTreeItem(startTouch.pageY);
				this.setValue(treeItemCur);
			},
			/**
			 * tree 触摸移动
			 */
			touchMove(e) {
				// 获取触摸点信息
				let currentTouch = e.changedTouches[0];
				if (!currentTouch) return;

				// 滑动结束后迅速开始第二次滑动时候 touching 为 false 造成不显示 indicator 问题
				if (!this.touching) {
					this.touching = true;
				}

				let treeItemCur = this.getCurrentTreeItem(currentTouch.pageY);
				this.setValue(treeItemCur);
			},
			/**
			 * tree 触摸结束
			 */
			touchEnd(e) {
				let treeItemCur = this.treeItemCur;
				let listItemCur = this.listItemCur;
				if (treeItemCur !== listItemCur) {
					this.treeItemCur = listItemCur;
					this.indicatorTop = this.indicatorTopList[treeItemCur];
				}
				this.treeKeyTran = true;
				setTimeout(() => {
					this.touching = false;
					this.treeKeyTran = false;
				}, 300);
			},
			letterClick(index, letter) {
				// #ifdef H5
				this.setValue(index);
				this.touchEnd()
				// #endif
				this.$emit('letterClick', {
					index: index,
					letter: letter
				})
			},
			/**
			 * 获取当前触摸的 tree-item
			 * @param pageY: 当前触摸点pageY
			 */
			getCurrentTreeItem(pageY) {
				let {
					treeTop,
					treeBottom,
					itemHeight,
					itemMount
				} = this.treeInfo;

				if (pageY < treeTop) {
					return 0;
				} else if (pageY >= treeBottom) {
					return itemMount - 1;
				} else {
					return Math.floor((pageY - treeTop) / itemHeight);
				}
			},
			/**
			 * 触摸之后后设置对应value
			 */
			setValue(treeItemCur) {
				if (treeItemCur === this.treeItemCur) return;

				let block = this.blocks[treeItemCur];
				if (!block) return;

				let {
					scrollTop,
					scrollIndex
				} = block,
				indicatorTop = this.indicatorTopList[treeItemCur];

				this.background_cur = this.activeBackground;
				this.color_cur = this.getActiveColor;
				this.background_next = this.background;
				this.color_next = this.color;
				this.treeItemCur = treeItemCur;
				this.scrollTop = scrollTop;
				this.listItemCur = scrollIndex;
				this.indicatorTop = indicatorTop;
			},
			/**
			 * 清除参数
			 */
			clearData() {
				this.treeItemCur = 0; // 索引树的聚焦项
				this.listItemCur = 0; // 节点树的聚焦项
				this.touching = false; // 是否在触摸索引树中
				this.scrollTop = 0; // 节点树滚动高度
				this.indicatorTop = -1; // 指示器顶部距离
				this.treeKeyTran = false;
				this.background_cur = this.background;
				this.color_cur = this.color;
				this.background_next = this.background;
				this.color_next = this.color;
			},
			/**
			 *  初始化获取 dom 信息
			 */
			initDom() {
				let {
					windowHeight,
					windowWidth
				} = uni.getSystemInfoSync();
				let remScale = (windowWidth || 375) / 375,
					realTop = (this.top * remScale) / 2,
					realBottom = (this.bottom * remScale) / 2,
					colors = ColorUtil.gradient(this.getActiveColor, this.color, 100),
					backgroundColors = ColorUtil.gradient(this.activeBackground, this.background, 100);

				this.remScale = remScale;
				this.realTop = realTop;
				this.realBottom = realBottom;
				this.colors = colors;
				this.backgroundColors = backgroundColors;

				uni.createSelectorQuery()
					.in(this)
					.select('#tui_index__letter')
					.boundingClientRect(res => {
						let treeTop = res.top,
							treeBottom = res.top + res.height,
							itemHeight = res.height / this.listData.length,
							itemMount = this.listData.length;

						let indicatorTopList = this.listData.map((item, index) => {
							return itemHeight / 2 + index * itemHeight + treeTop - remScale * 25;
						});
						this.treeInfo = {
							treeTop: treeTop,
							treeBottom: treeBottom,
							itemHeight: itemHeight,
							itemMount: itemMount
						};
						this.indicatorTopList = indicatorTopList;
					})
					.exec();

				uni.createSelectorQuery()
					.in(this)
					.select('.tui-content__box')
					.boundingClientRect(res => {
						let maxScrollTop = res.height - (windowHeight - realTop - realBottom);

						uni.createSelectorQuery()
							.in(this)
							.selectAll('.tui-item__select')
							.boundingClientRect(res => {
								let maxScrollIndex = -1;

								let blocks = res.map((item, index) => {
									// Math.ceil 向上取整, 防止索引树切换列表时候造成真机固定头部上边线显示过粗问题
									let itemTop = Math.ceil(item.top - realTop),
										itemBottom = Math.ceil(itemTop + item.height);

									if (maxScrollTop >= itemTop && maxScrollTop < itemBottom)
										maxScrollIndex = index;

									return {
										itemTop: itemTop,
										itemBottom: itemBottom,
										scrollTop: itemTop >= maxScrollTop ? maxScrollTop : itemTop,
										scrollIndex: maxScrollIndex === -1 ? index : maxScrollIndex
									};
								});

								this.maxScrollTop = maxScrollTop;
								this.blocks = blocks;
							})
							.exec();
					})
					.exec();
			},
			/**
			 *  初始化
			 */
			init() {
				this.clearData();
				// 避免获取不到节点信息报错问题
				if (this.listData.length === 0) {
					return;
				}
				// 异步加载数据时候, 延迟执行 initDom 方法
				setTimeout(() => this.initDom(), 1200);
			}
		},
		mounted() {
			this.$nextTick(()=>{
				this.init();
			})
		}
	};
</script>

<style scoped>
	.tui-index-list {
		width: 100vw;
		overflow: hidden;
		position: relative;
	}

	.tui-scroll__view {
		width: 100vw;
	}

	.tui-content__box {
		position: relative;
		width: 100%;
	}

	.tui-content__title {
		position: sticky;
		top: 0;
		z-index: 10;
		font-weight: bold;
	}

	.tui-content__title .tui-title__item {
		width: 100%;
		position: relative;
		display: flex;
		align-items: center;
	}

	.tui-line__top::before {
		content: ' ';
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		border-top: 1px solid #ebedf0;
		-webkit-transform: scaleY(0.5) translateZ(0);
		transform: scaleY(0.5) translateZ(0);
		transform-origin: 0 0;
		z-index: 2;
		pointer-events: none;
	}

	.tui-line__bottom::after {
		content: ' ';
		position: absolute;
		border-bottom: 1px solid #ebedf0;
		-webkit-transform: scaleY(0.5) translateZ(0);
		transform: scaleY(0.5) translateZ(0);
		transform-origin: 0 100%;
		bottom: 0;
		right: 0;
		left: 0;
	}

	.tui-index__indicator {
		position: fixed;
		right: 100rpx;
		width: 100rpx;
		height: 100rpx;
		line-height: 100rpx;
		border-radius: 10rpx;
		text-align: center;
		color: #ffffff;
		font-size: 60rpx;
		font-weight: bold;
		display: none;
		z-index: 10;
	}

	.tui-index__indicator:after {
		content: '';
		position: absolute;
		top: 0;
		right: 0;
		width: 100%;
		height: 100%;
		z-index: -1;
		border-radius: 100% 0% 100% 100%;
		background: #c9c9c9;
		transform: rotate(45deg);
	}

	.tui-indicator__show {
		display: block;
		z-index: 10;
	}

	.tui-indicator__tran {
		display: block;
		opacity: 0;
		transition: opacity 0.3s linear;
	}

	.tui-index__letter {
		position: fixed;
		right: 0;
		top: 50%;
		transform: translateY(-50%);
		text-align: center;
		z-index: 10;
	}

	.tui-letter__item {
		padding: 0 8rpx;
		font-weight: bold;
	}

	.tui-letter__key {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		font-size: 26rpx;
		transform: scale(0.8);
		transform-origin: center center;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.tui-list__item {
		width: 100%;
		display: flex;
		align-items: center;
	}

	.tui-list__item .tui-avatar {
		width: 68rpx;
		height: 68rpx;
		border-radius: 8rpx;
		flex-shrink: 0;
		background-color: #ccc;
	}

	.tui-list__item view {
		width: 90%;
		font-size: 32rpx;
		padding-left: 20rpx;
		padding-right: 40rpx;
		box-sizing: border-box;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
</style>