Blame view

uni_modules/uview-plus/components/u-index-anchor/u-index-anchor.vue 2.89 KB
a2702f6d   刘淇   巡查计划
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
  <template>
  	<!-- #ifdef APP-NVUE -->
  	<header>
  	<!-- #endif -->
  	<view
  	    class="u-index-anchor u-border-bottom"
  		:class="{ 'u-index-anchor--sticky': parentSticky }"
  		:ref="`u-index-anchor-${text}`"
  	    :style="{
  			height: addUnit(height),
  			backgroundColor: bgColor
  		}"
  	>
  		<text
  		    class="u-index-anchor__text"
  		    :style="{
  				fontSize: addUnit(size),
  				color: color
  			}"
  		>{{ text.name || text }}</text>
  	</view>
  	<!-- #ifdef APP-NVUE -->
  	</header>
  	<!-- #endif -->
  </template>
  
  <script>
  	import { props } from './props';
  	import { mpMixin } from '../../libs/mixin/mpMixin';
  	import { mixin } from '../../libs/mixin/mixin';
  	import { addUnit, $parent, error } from '../../libs/function/index';
  	// #ifdef APP-NVUE
  	const dom = uni.requireNativePlugin('dom')
  	// #endif
  	/**
  	 * IndexAnchor 列表锚点
  	 * @description 
  	 * @tutorial https://uview-plus.jiangruyi.com/components/indexList.html
  	 * @property {String | Number}	text	列表锚点文本内容
  	 * @property {String}			color	列表锚点文字颜色 ( 默认 '#606266' )
  	 * @property {String | Number}	size	列表锚点文字大小,单位默认px ( 默认 14 )
  	 * @property {String}			bgColor	列表锚点背景颜色 ( 默认 '#dedede' )
  	 * @property {String | Number}	height	列表锚点高度,单位默认px ( 默认 32 )
  	 * @example <u-index-anchor :text="indexList[index]"></u-index-anchor>
  	 */
  	export default {
  		name: 'u-index-anchor',
  		mixins: [mpMixin, mixin, props],
  		data() {
  			return {
  			}
  		},
  		mounted() {
  			this.init()
  		},
  		methods: {
  			addUnit,
  			init() {
  				// 此处会活动父组件实例,并赋值给实例的parent属性
  				const indexList = $parent.call(this, 'u-index-list')
  				if (!indexList) { 
  					return error('u-index-anchor必须要搭配u-index-list组件使用')
  				}
  				// 将当前实例放入到u-index-list中
  				indexList.anchors.push(this)
  				const indexListItem = $parent.call(this, 'u-index-item')
  				// #ifndef APP-NVUE
  				// 只有在非nvue下,u-index-anchor才是嵌套在u-index-item中的
  				if (!indexListItem) {
  					return error('u-index-anchor必须要搭配u-index-item组件使用')
  				}
  				// 设置u-index-item的id为anchor的text标识符,因为非nvue下滚动列表需要依赖scroll-view滚动到元素的特性
  				if (typeof this.text == 'string') {
  					indexListItem.id = this.text.charCodeAt(0)
  				} else {
  					indexListItem.id = this.text.name.charCodeAt(0)
  				}
  				// #endif
  			}
  		},
  		computed: {
          parentSticky() {
              const indexList = $parent.call(this, "u-index-list");
              return indexList ? indexList.sticky : true;
          	},
  		},
  	}
  </script>
  
  <style lang="scss" scoped>
  
  	.u-index-anchor {
  		position: sticky;
  		top: 0;
  		@include flex;
  		align-items: center;
  		padding-left: 15px;
  		z-index: 1;
  
  		&--sticky {
          position: sticky;
          top: 0;
      	}
  
  		&__text {
  			@include flex;
  			align-items: center;
  		}
  	}
  </style>