u-index-list.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <template>
  2. <view ref="u-index-list" class="u-index-list">
  3. <!-- #ifdef APP-NVUE -->
  4. <list
  5. :scrollTop="scrollTop"
  6. enable-back-to-top
  7. :offset-accuracy="1"
  8. :style="{
  9. maxHeight: addUnit(scrollViewHeight)
  10. }"
  11. @scroll="scrollHandler"
  12. ref="u-index-list__scroll-view"
  13. class="u-index-list__scroll-view"
  14. >
  15. <cell
  16. v-if="$slots.header"
  17. ref="header"
  18. >
  19. <slot name="header" />
  20. </cell>
  21. <slot />
  22. <cell v-if="$slots.footer">
  23. <slot name="footer" />
  24. </cell>
  25. </list>
  26. <!-- #endif -->
  27. <!-- #ifndef APP-NVUE -->
  28. <scroll-view
  29. :scrollTop="scrollTop"
  30. :scrollIntoView="scrollIntoView"
  31. :offset-accuracy="1"
  32. :style="{
  33. maxHeight: addUnit(scrollViewHeight)
  34. }"
  35. scroll-y
  36. @scroll="scrollHandler"
  37. ref="u-index-list__scroll-view"
  38. class="u-index-list__scroll-view"
  39. >
  40. <view class="u-index-list__header" v-if="$slots.header">
  41. <slot name="header" />
  42. </view>
  43. <slot />
  44. <view class="u-index-list__footer" v-if="$slots.footer">
  45. <slot name="footer" />
  46. </view>
  47. </scroll-view>
  48. <!-- #endif -->
  49. <view
  50. class="u-index-list__letter"
  51. ref="u-index-list__letter"
  52. :style="{top: addUnit(letterInfo.top) , transform: 'translateY(-50%)'}"
  53. @touchstart.prevent="touchStart"
  54. @touchmove.prevent="touchMove"
  55. @touchend.prevent="touchEnd"
  56. @touchcancel.prevent="touchEnd"
  57. >
  58. <view
  59. class="u-index-list__letter__item"
  60. v-for="(item, index) in uIndexList"
  61. :key="index"
  62. :style="{
  63. backgroundColor: activeIndex === index ? activeColor : 'transparent'
  64. }"
  65. >
  66. <text
  67. class="u-index-list__letter__item__index"
  68. :style="{color: activeIndex === index ? '#fff' : inactiveColor}"
  69. >{{ item.key || item }}</text>
  70. </view>
  71. </view>
  72. <u-transition
  73. mode="fade"
  74. :show="touching"
  75. :customStyle="{
  76. position: 'absolute',
  77. right: '50px',
  78. top: addUnit(indicatorTop, 'px'),
  79. zIndex: 3
  80. }"
  81. >
  82. <view
  83. class="u-index-list__indicator"
  84. :class="['u-index-list__indicator--show']"
  85. :style="{
  86. height: addUnit(indicatorHeight),
  87. width: addUnit(indicatorHeight)
  88. }"
  89. >
  90. <text class="u-index-list__indicator__text">{{ uIndexList[activeIndex]?.key || uIndexList[activeIndex] }}</text>
  91. </view>
  92. </u-transition>
  93. </view>
  94. </template>
  95. <script>
  96. const indexList = () => {
  97. const indexList = [];
  98. const charCodeOfA = 'A'.charCodeAt(0);
  99. for (let i = 0; i < 26; i++) {
  100. indexList.push(String.fromCharCode(charCodeOfA + i));
  101. }
  102. return indexList;
  103. }
  104. import { props } from './props';
  105. import { mpMixin } from '../../libs/mixin/mpMixin';
  106. import { mixin } from '../../libs/mixin/mixin';
  107. import { addUnit, getWindowInfo, sleep, getPx } from '../../libs/function/index';
  108. // #ifdef APP-NVUE
  109. // 由于weex为阿里的KPI业绩考核的产物,所以不支持百分比单位,这里需要通过dom查询组件的宽度
  110. const dom = uni.requireNativePlugin('dom')
  111. // #endif
  112. /**
  113. * IndexList 索引列表
  114. * @description 通过折叠面板收纳内容区域
  115. * @tutorial https://uview-plus.jiangruyi.com/components/indexList.html
  116. * @property {String} inactiveColor 右边锚点非激活的颜色 ( 默认 '#606266' )
  117. * @property {String} activeColor 右边锚点激活的颜色 ( 默认 '#5677fc' )
  118. * @property {Array} indexList 索引字符列表,数组形式
  119. * @property {Boolean} sticky 是否开启锚点自动吸顶 ( 默认 true )
  120. * @property {String | Number} customNavHeight 自定义导航栏的高度 ( 默认 0 )
  121. * */
  122. export default {
  123. name: 'u-index-list',
  124. mixins: [mpMixin, mixin, props],
  125. // #ifdef MP-WEIXIN
  126. // 将自定义节点设置成虚拟的,更加接近Vue组件的表现,能更好的使用flex属性
  127. options: {
  128. virtualHost: true
  129. },
  130. // #endif
  131. data() {
  132. return {
  133. // 当前正在被选中的字母索引
  134. activeIndex: -1,
  135. touchmoveIndex: 1,
  136. // 索引字母的信息
  137. letterInfo: {
  138. height: 0,
  139. itemHeight: 0,
  140. top: 0
  141. },
  142. // 设置字母指示器的高度,后面为了让指示器跟随字母,并将尖角部分指向字母的中部,需要依赖此值
  143. indicatorHeight: 50,
  144. // 字母放大指示器的top值,为了让其指向当前激活的字母
  145. // indicatorTop: 0
  146. // 当前是否正在被触摸状态
  147. touching: false,
  148. // 滚动条顶部top值
  149. scrollTop: 0,
  150. // scroll-view的高度
  151. scrollViewHeight: 0,
  152. // 系统信息
  153. sys: {},
  154. scrolling: false,
  155. scrollIntoView: '',
  156. pageY: 0,
  157. topOffset: 0
  158. }
  159. },
  160. computed: {
  161. // 如果有传入外部的indexList锚点数组则使用,否则使用内部生成A-Z字母
  162. uIndexList() {
  163. return this.indexList.length ? this.indexList : indexList()
  164. },
  165. // 字母放大指示器的top值,为了让其指向当前激活的字母
  166. indicatorTop() {
  167. const {
  168. top,
  169. height,
  170. itemHeight
  171. } = this.letterInfo
  172. return Math.floor(top - (height / 2) + itemHeight * this.activeIndex + itemHeight - 70 / 2)
  173. }
  174. },
  175. watch: {
  176. // 监听字母索引的变化,重新设置尺寸
  177. uIndexList: {
  178. immediate: false,
  179. handler() {
  180. sleep(30).then(() => {
  181. this.setIndexListLetterInfo()
  182. })
  183. }
  184. }
  185. },
  186. created() {
  187. this.children = []
  188. this.anchors = []
  189. this.sys = getWindowInfo()
  190. },
  191. mounted() {
  192. this.init()
  193. sleep(50).then(() => {
  194. this.setIndexListLetterInfo()
  195. })
  196. },
  197. methods: {
  198. addUnit,
  199. init() {
  200. // 设置列表的高度为整个屏幕的高度
  201. //减去this.customNavHeight,并将this.scrollViewHeight设置为maxHeight
  202. //解决当u-index-list组件放在tabbar页面时,scroll-view内容较少时,还能滚动
  203. let customNavHeight = getPx(this.customNavHeight)
  204. // this.scrollViewHeight = this.sys.windowHeight - customNavHeight
  205. this.getIndexListRect().then(async sizeScroll => {
  206. this.scrollViewHeight = sizeScroll.height ? sizeScroll.height : this.sys.windowHeight - customNavHeight
  207. this.topOffset = this.sys.windowHeight - this.scrollViewHeight
  208. // console.log('scrollViewHeight', this.scrollViewHeight)
  209. // console.log('topOffset', this.topOffset)
  210. })
  211. },
  212. // 索引列表被触摸
  213. touchStart(e) {
  214. // 获取触摸点信息
  215. const touchStartData = e.changedTouches[0]
  216. if (!touchStartData) return
  217. this.touching = true
  218. const {
  219. pageY,
  220. screenY
  221. } = touchStartData
  222. // 根据当前触摸点的坐标,获取当前触摸的为第几个字母
  223. // #ifdef APP-NVUE
  224. // 使用screenY要减去导航栏44和状态栏24高度
  225. const currentIndex = this.getIndexListLetter(screenY - 68)
  226. // #endif
  227. // #ifndef APP-NVUE
  228. const currentIndex = this.getIndexListLetter(pageY)
  229. // #endif
  230. this.setValueForTouch(currentIndex)
  231. },
  232. // 索引字母列表被触摸滑动中
  233. touchMove(e) {
  234. // 获取触摸点信息
  235. let touchMove = e.changedTouches[0]
  236. if (!touchMove) return;
  237. // 滑动结束后迅速开始第二次滑动时候 touching 为 false 造成不显示 indicator 问题
  238. if (!this.touching) {
  239. this.touching = true
  240. }
  241. const {
  242. pageY,
  243. screenY
  244. } = touchMove
  245. // #ifdef APP-NVUE
  246. // 使用screenY要减去导航栏44和状态栏24高度
  247. const currentIndex = this.getIndexListLetter(screenY - 68)
  248. // #endif
  249. // #ifndef APP-NVUE
  250. const currentIndex = this.getIndexListLetter(pageY)
  251. // #endif
  252. this.setValueForTouch(currentIndex)
  253. },
  254. // 触摸结束
  255. touchEnd(e) {
  256. // 延时一定时间后再隐藏指示器,为了让用户看的更直观,同时也是为了消除快速切换u-transition的show带来的影响
  257. sleep(300).then(() => {
  258. this.touching = false
  259. })
  260. },
  261. // 获取索引列表的尺寸以及单个字符的尺寸信息
  262. getIndexListLetterRect() {
  263. return new Promise(resolve => {
  264. // 延时一定时间,以获取dom尺寸
  265. // #ifndef APP-NVUE
  266. this.$uGetRect('.u-index-list__letter').then(size => {
  267. resolve(size)
  268. })
  269. // #endif
  270. // #ifdef APP-NVUE
  271. const ref = this.$refs['u-index-list__letter']
  272. dom.getComponentRect(ref, res => {
  273. resolve(res.size)
  274. })
  275. // #endif
  276. })
  277. },
  278. getIndexListScrollViewRect() {
  279. return new Promise(resolve => {
  280. // 延时一定时间,以获取dom尺寸
  281. // #ifndef APP-NVUE
  282. this.$nextTick(() => {
  283. this.$uGetRect('.u-index-list__scroll-view').then(size => {
  284. resolve(size)
  285. })
  286. })
  287. // #endif
  288. // #ifdef APP-NVUE
  289. const ref = this.$refs['u-index-list__scroll-view']
  290. dom.getComponentRect(ref, res => {
  291. resolve(res.size)
  292. })
  293. // #endif
  294. })
  295. },
  296. getIndexListRect() {
  297. return new Promise(resolve => {
  298. // 延时一定时间,以获取dom尺寸
  299. // #ifndef APP-NVUE
  300. this.$uGetRect('.u-index-list').then(size => {
  301. resolve(size)
  302. })
  303. // #endif
  304. // #ifdef APP-NVUE
  305. const ref = this.$refs['u-index-list']
  306. dom.getComponentRect(ref, res => {
  307. resolve(res.size)
  308. })
  309. // #endif
  310. })
  311. },
  312. // 设置indexList索引的尺寸信息
  313. setIndexListLetterInfo() {
  314. this.getIndexListLetterRect().then(size => {
  315. // console.log('getIndexListLetterRect', size)
  316. const {
  317. height
  318. } = size
  319. const sysData = getWindowInfo()
  320. const windowHeight = sysData.windowHeight
  321. let customNavHeight = 0
  322. // 消除各端导航栏非原生和原生导致的差异,让索引列表字母对屏幕垂直居中
  323. if (this.customNavHeight == 0) {
  324. // #ifdef H5
  325. customNavHeight = sysData.windowTop
  326. // #endif
  327. // #ifndef H5
  328. // 在非H5中,为原生导航栏,其高度不算在windowHeight内,这里设置为负值,后面相加时变成减去其高度的一半
  329. customNavHeight = -(sysData.statusBarHeight + 44)
  330. // #endif
  331. } else {
  332. customNavHeight = getPx(this.customNavHeight)
  333. }
  334. this.getIndexListScrollViewRect().then(sizeScroll => {
  335. // console.log('sizeScroll', sizeScroll)
  336. this.letterInfo = {
  337. height,
  338. // 为了让字母列表对屏幕绝对居中,让其对导航栏进行修正,也即往上偏移导航栏的一半高度
  339. top: sizeScroll.height / 2,
  340. // top: (this.scrollViewHeight - height) / 2 + customNavHeight / 2,
  341. itemHeight: Math.floor(height / this.uIndexList.length)
  342. }
  343. })
  344. })
  345. },
  346. // 获取当前被触摸的索引字母
  347. getIndexListLetter(pageY) {
  348. this.pageY = pageY
  349. let {
  350. top,
  351. height,
  352. itemHeight
  353. } = this.letterInfo
  354. let index = this.currentIndex;
  355. // 对H5的pageY进行修正,这是由于uni-app自作多情在H5中将触摸点的坐标跟H5的导航栏结合导致的问题
  356. // #ifdef H5
  357. // pageY += getWindowInfo().windowTop
  358. // #endif
  359. // 对第一和最后一个字母做边界处理,因为用户可能在字母列表上触摸到两端的尽头后依然继续滑动
  360. // console.log('top1', top)
  361. // console.log('height', height)
  362. top = top - (height / 2) // 减去transfrom的translateY值导致的高度
  363. pageY = pageY - this.topOffset
  364. // if (this.safeBottomFix) {
  365. // pageY = pageY + 34
  366. // }
  367. // console.log('topOffset', this.topOffset)
  368. // console.log('pageY', pageY)
  369. // console.log('top2', top)
  370. if (pageY < top) {
  371. index = 0
  372. } else if (pageY >= top + height) {
  373. // 如果超出了,取最后一个字母
  374. index = this.uIndexList.length - 1
  375. } else {
  376. // 将触摸点的Y轴偏移值,减去索引字母的top值,除以每个字母的高度,即可得到当前触摸点落在哪个字母上
  377. index = Math.floor((pageY - top) / itemHeight)
  378. }
  379. // console.log(index)
  380. return index
  381. },
  382. // 设置各项由触摸而导致变化的值
  383. async setValueForTouch(currentIndex) {
  384. // 如果偏移量太小,前后得出的会是同一个索引字母,为了防抖,进行返回
  385. if (currentIndex === this.activeIndex) return
  386. this.activeIndex = currentIndex
  387. this.$emit('select', this.uIndexList[currentIndex])
  388. // #ifndef APP-NVUE || MP-WEIXIN
  389. // 在非nvue中,由于anchor和item都在u-index-item中,所以需要对index-item进行偏移
  390. if (typeof this.uIndexList[currentIndex] == 'string') {
  391. this.scrollIntoView = `u-index-item-${this.uIndexList[currentIndex].charCodeAt(0)}`
  392. } else {
  393. this.scrollIntoView = `u-index-item-${this.uIndexList[currentIndex].name.charCodeAt(0)}`
  394. }
  395. // #endif
  396. // #ifdef MP-WEIXIN
  397. // 微信小程序下,scroll-view的scroll-into-view属性无法对slot中的内容的id生效,只能通过设置scrollTop的形式去移动滚动条
  398. const customNavHeight = this.customNavHeight
  399. // 获取header slot的尺寸信息
  400. const header = await this.getHeaderRect()
  401. // item的top值,在nvue下,模拟出的anchor的top,类似非nvue下的index-item的top
  402. let top = header.height
  403. // console.log(top)
  404. const anchors = this.anchors
  405. // 由于list组件无法获取cell的top值,这里通过header slot和各个item之间的height,模拟出类似非nvue下的位置信息
  406. let children = this.children.map((item, index) => {
  407. const childHeight = item.height + getPx(this.itemMargin)
  408. const child = {
  409. height: childHeight,
  410. top: top
  411. }
  412. // 进行累加,给下一个item提供计算依据
  413. top = top + childHeight
  414. // #ifdef APP-NVUE
  415. // 只有nvue下,需要将锚点的高度也累加,非nvue下锚点高度是包含在index-item中的。
  416. top = top + anchors[index].height
  417. // #endif
  418. return child
  419. })
  420. // console.log('this.children[currentIndex].top', children[currentIndex].top)
  421. if (children[currentIndex]?.top || children[currentIndex].top === 0) {
  422. this.scrollTop = children[currentIndex].top - getPx(customNavHeight)
  423. }
  424. // #endif
  425. // #ifdef APP-NVUE
  426. // 在nvue中,由于cell和header为同级元素,所以实际是需要对header(anchor)进行偏移
  427. const anchor = `u-index-anchor-${this.uIndexList[currentIndex]}`
  428. // console.log(anchor)
  429. dom.scrollToElement(this.anchors[currentIndex].$refs[anchor], {
  430. offset: 0,
  431. animated: false
  432. })
  433. // #endif
  434. },
  435. getHeaderRect() {
  436. // 获取header slot的高度,因为list组件中获取元素的尺寸是没有top值的
  437. return new Promise(resolve => {
  438. if (!this.$slots.header) {
  439. resolve({
  440. width: 0,
  441. height: 0
  442. })
  443. }
  444. // #ifndef APP-NVUE
  445. this.$uGetRect('.u-index-list__header').then(size => {
  446. resolve(size)
  447. })
  448. // #endif
  449. // #ifdef APP-NVUE
  450. let headerRef = this.$refs.header
  451. if (!headerRef) {
  452. resolve({
  453. width: 0,
  454. height: 0
  455. })
  456. }
  457. dom.getComponentRect(headerRef, res => {
  458. resolve(res.size)
  459. })
  460. // #endif
  461. })
  462. },
  463. // scroll-view的滚动事件
  464. async scrollHandler(e) {
  465. if (this.touching || this.scrolling) return
  466. // 每过一定时间取样一次,减少资源损耗以及可能带来的卡顿
  467. this.scrolling = true
  468. sleep(10).then(() => {
  469. this.scrolling = false
  470. })
  471. let scrollTop = 0
  472. const len = this.children.length
  473. let children = this.children
  474. // #ifdef APP-NVUE
  475. // nvue下获取的滚动条偏移为负数,需要转为正数
  476. let sys = getWindowInfo()
  477. scrollTop = Math.abs(e.contentOffset.y) / 10
  478. // console.log('native', e)
  479. // #endif
  480. // 获取header slot的尺寸信息
  481. const header = await this.getHeaderRect()
  482. // item的top值,在nvue下,模拟出的anchor的top,类似非nvue下的index-item的top
  483. let top = header.height
  484. const anchors = this.anchors
  485. // 由于list组件无法获取cell的top值,这里通过header slot和各个item之间的height,模拟出类似非nvue下的位置信息
  486. children = this.children.map((item, index) => {
  487. const childHeight = item.height + getPx(this.itemMargin)
  488. const child = {
  489. height: childHeight,
  490. top: top
  491. }
  492. // 进行累加,给下一个item提供计算依据
  493. top = top + childHeight
  494. // #ifdef APP-NVUE
  495. // 只有nvue下,需要将锚点的高度也累加,非nvue下锚点高度是包含在index-item中的。
  496. top = top + anchors[index].height
  497. // #endif
  498. return child
  499. })
  500. // #ifndef APP-NVUE
  501. // 非nvue通过detail获取滚动条位移
  502. scrollTop = e.detail.scrollTop
  503. // console.log('scrollTop', scrollTop, this.customNavHeight)
  504. // #endif
  505. // 在弹窗中需要加上弹窗距离顶部的高度topOffset
  506. scrollTop = scrollTop + getPx(this.customNavHeight)
  507. for (let i = 0; i < len; i++) {
  508. const item = children[i],
  509. nextItem = children[i + 1]
  510. // 如果滚动条高度小于第一个item的top值,此时无需设置任意字母为高亮
  511. if (scrollTop <= children[0].top || scrollTop >= children[len - 1].top + children[len - 1].height) {
  512. this.activeIndex = -1
  513. break
  514. } else if (!nextItem) {
  515. // 当不存在下一个item时,意味着历遍到了最后一个
  516. this.activeIndex = len - 1
  517. break
  518. } else if (scrollTop > item.top && scrollTop < nextItem.top) {
  519. this.activeIndex = i
  520. break
  521. }
  522. }
  523. },
  524. },
  525. }
  526. </script>
  527. <style lang="scss" scoped>
  528. .u-index-list {
  529. &__letter {
  530. position: absolute;
  531. right: 0;
  532. text-align: center;
  533. z-index: 3;
  534. padding: 0 6px;
  535. width: 30px;
  536. &__item {
  537. width: 16px;
  538. height: 16px;
  539. border-radius: 100px;
  540. margin: 1px 0;
  541. @include flex;
  542. align-items: center;
  543. justify-content: center;
  544. &--active {
  545. background-color: $u-primary;
  546. }
  547. &__index {
  548. font-size: 12px;
  549. text-align: center;
  550. line-height: 12px;
  551. }
  552. }
  553. }
  554. &__indicator {
  555. width: 50px;
  556. height: 50px;
  557. border-radius: 100px 100px 0 100px;
  558. text-align: center;
  559. color: #ffffff;
  560. background-color: #c9c9c9;
  561. transform: rotate(-45deg);
  562. @include flex;
  563. justify-content: center;
  564. align-items: center;
  565. &__text {
  566. font-size: 28px;
  567. line-height: 28px;
  568. font-weight: bold;
  569. color: #fff;
  570. transform: rotate(45deg);
  571. text-align: center;
  572. }
  573. }
  574. }
  575. </style>