u-index-list.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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"
  8. @touchend.stop.prevent="onTouchStop" @touchcancel.stop.prevent="onTouchStop">
  9. <view v-for="(item, index) in indexList" :key="index" :data-index="index"
  10. :style="{zIndex: zIndex + 1, color: activeAnchorIndex === index ? activeColor : ''}"
  11. class="u-index-bar__index">
  12. {{ item }}
  13. </view>
  14. </view>
  15. <view v-if="touchmove && indexList[touchmoveIndex]" :style="{
  16. zIndex: alertZIndex
  17. }" class="u-indexed-list-alert">
  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. this.stickyOffsetTop = this.offsetTop ? uni.upx2px(this.offsetTop) : 0;
  81. // #endif
  82. // 只能在created生命周期定义children,如果在data定义,会因为循环引用而报错
  83. this.children = [];
  84. },
  85. data() {
  86. return {
  87. activeAnchorIndex: 0,
  88. showSidebar: true,
  89. // children: [],
  90. touchmove: false,
  91. touchmoveIndex: 0,
  92. }
  93. },
  94. watch: {
  95. scrollTop() {
  96. this.updateData()
  97. }
  98. },
  99. computed: {
  100. // 弹出toast的z-index值
  101. alertZIndex() {
  102. return this.$u.zIndex.toast;
  103. }
  104. },
  105. methods: {
  106. updateData() {
  107. this.timer && clearTimeout(this.timer);
  108. this.timer = setTimeout(() => {
  109. this.showSidebar = !!this.children.length;
  110. this.setRect().then(() => {
  111. this.onScroll();
  112. });
  113. }, 0);
  114. },
  115. setRect() {
  116. return Promise.all([
  117. this.setAnchorsRect(),
  118. this.setListRect(),
  119. this.setSiderbarRect()
  120. ]);
  121. },
  122. setAnchorsRect() {
  123. return Promise.all(this.children.map((anchor, index) => anchor
  124. .$uGetRect('.u-index-anchor-wrapper')
  125. .then((rect) => {
  126. Object.assign(anchor, {
  127. height: rect.height,
  128. top: rect.top
  129. });
  130. })));
  131. },
  132. setListRect() {
  133. return this.$uGetRect('.u-index-bar').then((rect) => {
  134. Object.assign(this, {
  135. height: rect.height,
  136. top: rect.top + this.scrollTop
  137. });
  138. });
  139. },
  140. setSiderbarRect() {
  141. return this.$uGetRect('.u-index-bar__sidebar').then(rect => {
  142. this.sidebar = {
  143. height: rect.height,
  144. top: rect.top
  145. };
  146. });
  147. },
  148. getActiveAnchorIndex() {
  149. const {
  150. children
  151. } = this;
  152. const {
  153. sticky
  154. } = this;
  155. for (let i = this.children.length - 1; i >= 0; i--) {
  156. const preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  157. const reachTop = sticky ? preAnchorHeight : 0;
  158. if (reachTop >= children[i].top) {
  159. return i;
  160. }
  161. }
  162. return -1;
  163. },
  164. onScroll() {
  165. const {
  166. children = []
  167. } = this;
  168. if (!children.length) {
  169. return;
  170. }
  171. const {
  172. sticky,
  173. stickyOffsetTop,
  174. zIndex,
  175. scrollTop,
  176. activeColor
  177. } = this;
  178. const active = this.getActiveAnchorIndex();
  179. this.activeAnchorIndex = active;
  180. if (sticky) {
  181. let isActiveAnchorSticky = false;
  182. if (active !== -1) {
  183. isActiveAnchorSticky =
  184. children[active].top <= 0;
  185. }
  186. children.forEach((item, index) => {
  187. if (index === active) {
  188. let wrapperStyle = '';
  189. let anchorStyle = {
  190. color: `${activeColor}`
  191. };
  192. if (isActiveAnchorSticky) {
  193. wrapperStyle = {
  194. height: `${children[index].height}px`
  195. };
  196. anchorStyle = {
  197. position: 'fixed',
  198. top: `${stickyOffsetTop}px`,
  199. zIndex: `${zIndex ? zIndex : this.$u.zIndex.indexListSticky}`,
  200. color: `${activeColor}`
  201. };
  202. }
  203. item.active = active;
  204. item.wrapperStyle = wrapperStyle;
  205. item.anchorStyle = anchorStyle;
  206. } else if (index === active - 1) {
  207. const currentAnchor = children[index];
  208. const currentOffsetTop = currentAnchor.top;
  209. const targetOffsetTop = index === children.length - 1 ?
  210. this.top :
  211. children[index + 1].top;
  212. const parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  213. const translateY = parentOffsetHeight - currentAnchor.height;
  214. const anchorStyle = {
  215. position: 'relative',
  216. transform: `translate3d(0, ${translateY}px, 0)`,
  217. zIndex: `${zIndex ? zIndex : this.$u.zIndex.indexListSticky}`,
  218. color: `${activeColor}`
  219. };
  220. item.active = active;
  221. item.anchorStyle = anchorStyle;
  222. } else {
  223. item.active = false;
  224. item.anchorStyle = '';
  225. item.wrapperStyle = '';
  226. }
  227. });
  228. }
  229. },
  230. onTouchMove(event) {
  231. this.touchmove = true;
  232. const sidebarLength = this.children.length;
  233. const touch = event.touches[0];
  234. const itemHeight = this.sidebar.height / sidebarLength;
  235. let clientY = 0;
  236. clientY = touch.clientY;
  237. let index = Math.floor((clientY - this.sidebar.top) / itemHeight);
  238. if (index < 0) {
  239. index = 0;
  240. } else if (index > sidebarLength - 1) {
  241. index = sidebarLength - 1;
  242. }
  243. this.touchmoveIndex = index;
  244. this.scrollToAnchor(index);
  245. },
  246. onTouchStop() {
  247. this.touchmove = false;
  248. this.scrollToAnchorIndex = null;
  249. },
  250. scrollToAnchor(index) {
  251. if (this.scrollToAnchorIndex === index) {
  252. return;
  253. }
  254. this.scrollToAnchorIndex = index;
  255. const anchor = this.children.find((item) => item.index === this.indexList[index]);
  256. if (anchor) {
  257. this.$emit('select', anchor.index);
  258. uni.pageScrollTo({
  259. duration: 0,
  260. scrollTop: anchor.top + this.scrollTop
  261. });
  262. }
  263. }
  264. }
  265. };
  266. </script>
  267. <style lang="scss" scoped>
  268. @import "../../libs/css/style.components.scss";
  269. .u-index-bar {
  270. position: relative
  271. }
  272. .u-index-bar__sidebar {
  273. position: fixed;
  274. top: 50%;
  275. right: 0;
  276. @include vue-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: 8 rpx 18 rpx;
  286. font-size: 22 rpx;
  287. line-height: 1
  288. }
  289. .u-indexed-list-alert {
  290. position: fixed;
  291. width: 120 rpx;
  292. height: 120 rpx;
  293. right: 90 rpx;
  294. top: 50%;
  295. margin-top: -60rpx;
  296. border-radius: 24 rpx;
  297. font-size: 50 rpx;
  298. color: #fff;
  299. background-color: rgba(0, 0, 0, 0.65);
  300. @include vue-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: 50 rpx;
  308. }
  309. </style>