u-index-list.vue 7.9 KB

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