tn-index-list.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <!-- 支付宝小程序使用_tGetRect()获取组件的根元素尺寸,所以在外面套一个"壳" -->
  3. <view>
  4. <view class="tn-index-list-class tn-index-list">
  5. <slot></slot>
  6. <!-- 侧边栏 -->
  7. <view
  8. v-if="showSidebar"
  9. class="tn-index-list__sidebar"
  10. @touchstart.stop.prevent="onTouchMove"
  11. @touchmove.stop.prevent="onTouchMove"
  12. @touchend.stop.prevent="onTouchStop"
  13. @touchcancel.stop.prevent="onTouchStop"
  14. >
  15. <view
  16. v-for="(item, index) in indexList"
  17. :key="index"
  18. class="tn-index-list__sidebar__item"
  19. :style="{
  20. zIndex: zIndex + 1,
  21. color: activeAnchorIndex === index ? activeColor : ''
  22. }"
  23. >
  24. {{ item }}
  25. </view>
  26. </view>
  27. <!-- 选中弹出框 -->
  28. <view
  29. v-if="touchMove && indexList[touchMoveIndex]"
  30. class="tn-index-list__alert"
  31. :style="{
  32. zIndex: selectAlertZIndex
  33. }"
  34. >
  35. <text>{{ indexList[touchMoveIndex] }}</text>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. // 生成 A-Z的字母列表
  42. let indexList = function() {
  43. let indexList = []
  44. let charCodeOfA = 'A'.charCodeAt(0)
  45. for (var i = 0; i < 26; i++) {
  46. indexList.push(String.fromCharCode(charCodeOfA + i))
  47. }
  48. return indexList
  49. }
  50. export default {
  51. name: 'tn-index-list',
  52. props: {
  53. // 索引列表
  54. indexList: {
  55. type: Array,
  56. default() {
  57. return indexList()
  58. }
  59. },
  60. // 是否自动吸顶
  61. sticky: {
  62. type: Boolean,
  63. default: true
  64. },
  65. // 自动吸顶时距离顶部的距离,单位px
  66. stickyTop: {
  67. type: Number,
  68. default: 0
  69. },
  70. // 自定义顶栏的高度,单位px
  71. customBarHeight: {
  72. type: Number,
  73. default: 0
  74. },
  75. // 当前滚动的高度
  76. // 由于自定义组件无法获取滚动高度,所以依赖传入
  77. scrollTop: {
  78. type: Number,
  79. default: 0
  80. },
  81. // 选中索引时的颜色
  82. activeColor: {
  83. type: String,
  84. default: '#01BEFF'
  85. },
  86. // 吸顶时的z-index
  87. zIndex: {
  88. type: Number,
  89. default: 0
  90. }
  91. },
  92. computed: {
  93. // 选中索引列表弹出提示框的z-index
  94. selectAlertZIndex() {
  95. return this.$t.zIndex.toast
  96. },
  97. // 吸顶的偏移高度
  98. stickyOffsetTop() {
  99. // #ifdef H5
  100. return this.stickyTop !== '' ? this.stickyTop : 44
  101. // #endif
  102. // #ifndef H5
  103. return this.stickyTop !== '' ? this.stickyTop : 0
  104. // #endif
  105. }
  106. },
  107. data() {
  108. return {
  109. // 当前激活的列表锚点的序号
  110. activeAnchorIndex: 0,
  111. // 显示侧边索引栏
  112. showSidebar: true,
  113. // 标记是否开始触摸移动
  114. touchMove: false,
  115. // 当前触摸移动到对应索引的序号
  116. touchMoveIndex: 0,
  117. // 滚动到对应锚点的序号
  118. scrollToAnchorIndex: 0,
  119. // 侧边栏的信息
  120. sidebar: {
  121. height: 0,
  122. top: 0
  123. },
  124. // 内容区域高度
  125. height: 0,
  126. // 内容区域top
  127. top: 0
  128. }
  129. },
  130. watch: {
  131. scrollTop() {
  132. this.updateData()
  133. }
  134. },
  135. created() {
  136. // 只能在created生命周期定义childrens,如果在data定义,会因为循环引用而报错
  137. this.childrens = []
  138. },
  139. methods: {
  140. // 更新数据
  141. updateData() {
  142. this.timer && clearTimeout(this.timer)
  143. this.timer = setTimeout(() => {
  144. this.showSidebar = !!this.childrens.length
  145. this.getRect().then(() => {
  146. this.onScroll()
  147. })
  148. }, 0)
  149. },
  150. // 获取对应的信息
  151. getRect() {
  152. return Promise.all([
  153. this.getAnchorRect(),
  154. this.getListRect(),
  155. this.getSidebarRect()
  156. ])
  157. },
  158. // 获取列表内容子元素信息
  159. getAnchorRect() {
  160. return Promise.all(this.childrens.map((child, index) => {
  161. child._tGetRect('.tn-index-anchor__wrap').then((rect) => {
  162. Object.assign(child, {
  163. height: rect.height,
  164. top: rect.top - this.customBarHeight
  165. })
  166. })
  167. }))
  168. },
  169. // 获取列表信息
  170. getListRect() {
  171. return this._tGetRect('.tn-index-list').then(rect => {
  172. Object.assign(this, {
  173. height: rect.height,
  174. top: rect.top + this.scrollTop
  175. })
  176. })
  177. },
  178. // 获取侧边滚动栏信息
  179. getSidebarRect() {
  180. return this._tGetRect('.tn-index-list__sidebar').then(rect => {
  181. this.sidebar = {
  182. height: rect.height,
  183. top: rect.top
  184. }
  185. })
  186. },
  187. // 滚动事件
  188. onScroll() {
  189. const {
  190. childrens = []
  191. } = this
  192. if (!childrens.length) {
  193. return
  194. }
  195. const {
  196. sticky,
  197. stickyOffsetTop,
  198. zIndex,
  199. scrollTop,
  200. activeColor
  201. } = this
  202. const active = this.getActiveAnchorIndex()
  203. this.activeAnchorIndex = active
  204. if (sticky) {
  205. let isActiveAnchorSticky = false
  206. if (active !== -1) {
  207. isActiveAnchorSticky = childrens[active].top <= 0
  208. }
  209. childrens.forEach((item, index) => {
  210. if (index === active) {
  211. let wrapperStyle = ''
  212. let anchorStyle = {
  213. color: `${activeColor}`
  214. }
  215. if (isActiveAnchorSticky) {
  216. wrapperStyle = {
  217. height: `${childrens[index].height}px`
  218. }
  219. anchorStyle = {
  220. position: 'fixed',
  221. top: `${stickyOffsetTop}px`,
  222. zIndex: `${zIndex ? zIndex : this.$t.zIndex.indexListSticky}`,
  223. color: `${activeColor}`
  224. }
  225. }
  226. item.active = true
  227. item.wrapperStyle = wrapperStyle
  228. item.anchorStyle = anchorStyle
  229. } else if (index === active - 1) {
  230. const currentAnchor = childrens[index]
  231. const currentOffsetTop = currentAnchor.top
  232. const targetOffsetTop = index === childrens.length - 1 ? this.top : childrens[index + 1].top
  233. const parentOffsetHeight = targetOffsetTop - currentOffsetTop
  234. const translateY = parentOffsetHeight - currentAnchor.height
  235. const anchorStyle = {
  236. position: 'relative',
  237. transform: `translate3d(0, ${translateY}px, 0)`,
  238. zIndex: `${zIndex ? zIndex : this.$t.zIndex.indexListSticky}`,
  239. color: `${activeColor}`
  240. }
  241. item.active = false
  242. item.anchorStyle = anchorStyle
  243. } else {
  244. item.active = false
  245. item.wrapperStyle = ''
  246. item.anchorStyle = ''
  247. }
  248. })
  249. }
  250. },
  251. // 触摸移动
  252. onTouchMove(event) {
  253. this.touchMove = true
  254. const sidebarLength = this.childrens.length
  255. const touch = event.touches[0]
  256. const itemHeight = this.sidebar.height / sidebarLength
  257. let clientY = touch.clientY
  258. let index = Math.floor((clientY - this.sidebar.top) / itemHeight)
  259. if (index < 0) {
  260. index = 0
  261. } else if (index > sidebarLength - 1) {
  262. index = sidebarLength - 1
  263. }
  264. this.touchMoveIndex = index
  265. this.scrollToAnchor(index)
  266. },
  267. // 触摸停止
  268. onTouchStop() {
  269. this.touchMove = false
  270. this.scrollToAnchorIndex = null
  271. },
  272. // 获取当前的锚点序号
  273. getActiveAnchorIndex() {
  274. const {
  275. childrens,
  276. sticky
  277. } = this
  278. for (let i = this.childrens.length - 1; i >= 0; i--) {
  279. const preAnchorHeight = i > 0 ? childrens[i - 1].height : 0
  280. const reachTop = sticky ? preAnchorHeight : 0
  281. if (reachTop >= childrens[i].top) {
  282. return i
  283. }
  284. }
  285. return -1
  286. },
  287. // 滚动到对应的锚点
  288. scrollToAnchor(index) {
  289. if (this.scrollToAnchorIndex === index) {
  290. return
  291. }
  292. this.scrollToAnchorIndex = index
  293. const anchor = this.childrens.find(item => item.index === this.indexList[index])
  294. if (anchor) {
  295. const scrollTop = anchor.top + this.scrollTop
  296. this.$emit('select', {
  297. index: anchor.index,
  298. scrollTop: scrollTop
  299. })
  300. uni.pageScrollTo({
  301. duration:0,
  302. scrollTop: scrollTop
  303. })
  304. }
  305. }
  306. }
  307. }
  308. </script>
  309. <style lang="scss" scoped>
  310. .tn-index-list {
  311. position: relative;
  312. &__sidebar {
  313. display: flex;
  314. flex-direction: column;
  315. position: fixed;
  316. top: 50%;
  317. right: 0;
  318. text-align: center;
  319. transform: translateY(-50%);
  320. user-select: none;
  321. z-index: 99;
  322. &__item {
  323. font-weight: 500;
  324. padding: 8rpx 18rpx;
  325. font-size: 22rpx;
  326. line-height: 1;
  327. }
  328. }
  329. &__alert {
  330. display: flex;
  331. flex-direction: row;
  332. position: fixed;
  333. width: 120rpx;
  334. height: 120rpx;
  335. top: 50%;
  336. right: 90rpx;
  337. align-items: center;
  338. justify-content: center;
  339. margin-top: -60rpx;
  340. border-radius: 24rpx;
  341. font-size: 50rpx;
  342. color: #FFFFFF;
  343. background-color: $tn-font-sub-color;
  344. padding: 0;
  345. z-index: 9999999;
  346. text {
  347. line-height: 50rpx;
  348. }
  349. }
  350. }
  351. </style>