u-index-list.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <!-- 支付宝小程序使用$u.getRect()获取组件的根元素尺寸,所以在外面套一个"壳" -->
  3. <view>
  4. <view class="u-index-bar">
  5. <slot />
  6. <view v-if="showSidebar" class="u-index-bar__sidebar" @touchstart.stop.prevent="onTouchMove"
  7. @touchmove.stop.prevent="onTouchMove" @touchend.stop.prevent="onTouchStop"
  8. @touchcancel.stop.prevent="onTouchStop">
  9. <view v-for="(item, index) in indexList" :key="index" class="u-index-bar__index"
  10. :style="{zIndex: zIndex + 1, color: activeAnchorIndex === index ? activeColor : ''}"
  11. :data-index="index">
  12. {{ item }}
  13. </view>
  14. </view>
  15. <view class="u-indexed-list-alert" v-if="touchmove && indexList[touchmoveIndex]" :style="{
  16. zIndex: alertZIndex
  17. }">
  18. <text>{{indexList[touchmoveIndex]}}</text>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. var indexList = function() {
  25. var indexList = [];
  26. var charCodeOfA = 'A'.charCodeAt(0);
  27. for (var i = 0; i < 26; i++) {
  28. indexList.push(String.fromCharCode(charCodeOfA + i));
  29. }
  30. return indexList;
  31. };
  32. /**
  33. * indexList 索引列表
  34. * @description 通过折叠面板收纳内容区域,搭配<u-index-anchor>使用
  35. * @tutorial https://www.uviewui.com/components/indexList.html#indexanchor-props
  36. * @property {Number String} scroll-top 当前滚动高度,自定义组件无法获得滚动条事件,所以依赖接入方传入
  37. * @property {Array} index-list 索引字符列表,数组(默认A-Z)
  38. * @property {Number String} z-index 锚点吸顶时的层级(默认965)
  39. * @property {Boolean} sticky 是否开启锚点自动吸顶(默认true)
  40. * @property {Number String} offset-top 锚点自动吸顶时与顶部的距离(默认0)
  41. * @property {String} highlight-color 锚点和右边索引字符高亮颜色(默认#2979ff)
  42. * @event {Function} select 选中右边索引字符时触发
  43. * @example <u-index-list :scrollTop="scrollTop"></u-index-list>
  44. */
  45. export default {
  46. name: "u-index-list",
  47. props: {
  48. sticky: {
  49. type: Boolean,
  50. default: true
  51. },
  52. zIndex: {
  53. type: [Number, String],
  54. default: ''
  55. },
  56. scrollTop: {
  57. type: [Number, String],
  58. default: 0,
  59. },
  60. offsetTop: {
  61. type: [Number, String],
  62. default: 0
  63. },
  64. indexList: {
  65. type: Array,
  66. default () {
  67. return indexList()
  68. }
  69. },
  70. activeColor: {
  71. type: String,
  72. default: '#2979ff'
  73. }
  74. },
  75. created() {
  76. // #ifdef H5
  77. this.stickyOffsetTop = this.offsetTop ? uni.upx2px(this.offsetTop) : 44;
  78. // #endif
  79. // #ifndef H5
  80. // #ifndef APP-HARMONY
  81. this.stickyOffsetTop = this.offsetTop ? uni.upx2px(this.offsetTop) : 0;
  82. // #endif
  83. // #ifdef APP-HARMONY
  84. this.stickyOffsetTop = this.offsetTop ? this.offsetTop / 2 : 0;
  85. // #endif
  86. // #endif
  87. // 只能在created生命周期定义children,如果在data定义,会因为循环引用而报错
  88. this.children = [];
  89. },
  90. data() {
  91. return {
  92. activeAnchorIndex: 0,
  93. showSidebar: true,
  94. // children: [],
  95. touchmove: false,
  96. touchmoveIndex: 0,
  97. }
  98. },
  99. watch: {
  100. scrollTop() {
  101. this.updateData()
  102. }
  103. },
  104. computed: {
  105. // 弹出toast的z-index值
  106. alertZIndex() {
  107. return this.$u.zIndex.toast;
  108. }
  109. },
  110. methods: {
  111. updateData() {
  112. this.timer && clearTimeout(this.timer);
  113. this.timer = setTimeout(() => {
  114. this.showSidebar = !!this.children.length;
  115. this.setRect().then(() => {
  116. this.onScroll();
  117. });
  118. }, 0);
  119. },
  120. setRect() {
  121. return Promise.all([
  122. this.setAnchorsRect(),
  123. this.setListRect(),
  124. this.setSiderbarRect()
  125. ]);
  126. },
  127. setAnchorsRect() {
  128. return Promise.all(this.children.map((anchor, index) => anchor
  129. .$uGetRect('.u-index-anchor-wrapper')
  130. .then((rect) => {
  131. Object.assign(anchor, {
  132. height: rect.height,
  133. top: rect.top
  134. });
  135. })));
  136. },
  137. setListRect() {
  138. return this.$uGetRect('.u-index-bar').then((rect) => {
  139. Object.assign(this, {
  140. height: rect.height,
  141. top: rect.top + this.scrollTop
  142. });
  143. });
  144. },
  145. setSiderbarRect() {
  146. return this.$uGetRect('.u-index-bar__sidebar').then(rect => {
  147. this.sidebar = {
  148. height: rect.height,
  149. top: rect.top
  150. };
  151. });
  152. },
  153. getActiveAnchorIndex() {
  154. const {
  155. children
  156. } = this;
  157. const {
  158. sticky
  159. } = this;
  160. for (let i = this.children.length - 1; i >= 0; i--) {
  161. const preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  162. const reachTop = sticky ? preAnchorHeight : 0;
  163. if (reachTop >= children[i].top) {
  164. return i;
  165. }
  166. }
  167. return -1;
  168. },
  169. onScroll() {
  170. const {
  171. children = []
  172. } = this;
  173. if (!children.length) {
  174. return;
  175. }
  176. const {
  177. sticky,
  178. stickyOffsetTop,
  179. zIndex,
  180. scrollTop,
  181. activeColor
  182. } = this;
  183. const active = this.getActiveAnchorIndex();
  184. this.activeAnchorIndex = active;
  185. if (sticky) {
  186. let isActiveAnchorSticky = false;
  187. if (active !== -1) {
  188. isActiveAnchorSticky =
  189. children[active].top <= 0;
  190. }
  191. children.forEach((item, index) => {
  192. if (index === active) {
  193. let wrapperStyle = '';
  194. let anchorStyle = {
  195. color: `${activeColor}`
  196. };
  197. if (isActiveAnchorSticky) {
  198. wrapperStyle = {
  199. height: `${children[index].height}px`
  200. };
  201. anchorStyle = {
  202. position: 'fixed',
  203. top: `${stickyOffsetTop}px`,
  204. zIndex: `${zIndex ? zIndex : this.$u.zIndex.indexListSticky}`,
  205. color: `${activeColor}`
  206. };
  207. }
  208. item.active = active;
  209. item.wrapperStyle = wrapperStyle;
  210. item.anchorStyle = anchorStyle;
  211. } else if (index === active - 1) {
  212. const currentAnchor = children[index];
  213. const currentOffsetTop = currentAnchor.top;
  214. const targetOffsetTop = index === children.length - 1 ?
  215. this.top :
  216. children[index + 1].top;
  217. const parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  218. const translateY = parentOffsetHeight - currentAnchor.height;
  219. const anchorStyle = {
  220. position: 'relative',
  221. transform: `translate3d(0, ${translateY}px, 0)`,
  222. zIndex: `${zIndex ? zIndex : this.$u.zIndex.indexListSticky}`,
  223. color: `${activeColor}`
  224. };
  225. item.active = active;
  226. item.anchorStyle = anchorStyle;
  227. } else {
  228. item.active = false;
  229. item.anchorStyle = '';
  230. item.wrapperStyle = '';
  231. }
  232. });
  233. }
  234. },
  235. onTouchMove(event) {
  236. this.touchmove = true;
  237. const sidebarLength = this.children.length;
  238. const touch = event.touches[0];
  239. const itemHeight = this.sidebar.height / sidebarLength;
  240. let clientY = 0;
  241. clientY = touch.clientY;
  242. let index = Math.floor((clientY - this.sidebar.top) / itemHeight);
  243. if (index < 0) {
  244. index = 0;
  245. } else if (index > sidebarLength - 1) {
  246. index = sidebarLength - 1;
  247. }
  248. this.touchmoveIndex = index;
  249. this.scrollToAnchor(index);
  250. },
  251. onTouchStop() {
  252. this.touchmove = false;
  253. this.scrollToAnchorIndex = null;
  254. },
  255. scrollToAnchor(index) {
  256. if (this.scrollToAnchorIndex === index) {
  257. return;
  258. }
  259. this.scrollToAnchorIndex = index;
  260. const anchor = this.children.find((item) => item.index === this.indexList[index]);
  261. if (anchor) {
  262. this.$emit('select', anchor.index);
  263. uni.pageScrollTo({
  264. duration: 0,
  265. scrollTop: anchor.top + this.scrollTop
  266. });
  267. }
  268. }
  269. }
  270. };
  271. </script>
  272. <style lang="scss" scoped>
  273. @import "../../libs/css/style.components.scss";
  274. .u-index-bar {
  275. position: relative
  276. }
  277. .u-index-bar__sidebar {
  278. position: fixed;
  279. top: 50%;
  280. right: 0;
  281. @include vue-flex;
  282. flex-direction: column;
  283. text-align: center;
  284. transform: translateY(-50%);
  285. user-select: none;
  286. z-index: 99;
  287. }
  288. .u-index-bar__index {
  289. font-weight: 500;
  290. padding: 8rpx 18rpx;
  291. font-size: 22rpx;
  292. line-height: 1
  293. }
  294. .u-indexed-list-alert {
  295. position: fixed;
  296. width: 120rpx;
  297. height: 120rpx;
  298. right: 90rpx;
  299. top: 50%;
  300. margin-top: -60rpx;
  301. border-radius: 24rpx;
  302. font-size: 50rpx;
  303. color: #fff;
  304. background-color: rgba(0, 0, 0, 0.65);
  305. @include vue-flex;
  306. justify-content: center;
  307. align-items: center;
  308. padding: 0;
  309. z-index: 9999999;
  310. }
  311. .u-indexed-list-alert text {
  312. line-height: 50rpx;
  313. }
  314. </style>