u-index-list.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 }}</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] }}</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, sys, 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: 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. },
  190. mounted() {
  191. this.init()
  192. sleep(50).then(() => {
  193. this.setIndexListLetterInfo()
  194. })
  195. },
  196. methods: {
  197. addUnit,
  198. init() {
  199. // 设置列表的高度为整个屏幕的高度
  200. //减去this.customNavHeight,并将this.scrollViewHeight设置为maxHeight
  201. //解决当u-index-list组件放在tabbar页面时,scroll-view内容较少时,还能滚动
  202. let customNavHeight = getPx(this.customNavHeight)
  203. // this.scrollViewHeight = this.sys.windowHeight - customNavHeight
  204. this.getIndexListRect().then(async sizeScroll => {
  205. this.scrollViewHeight = sizeScroll.height ? sizeScroll.height : this.sys.windowHeight - customNavHeight
  206. this.topOffset = this.sys.windowHeight - this.scrollViewHeight
  207. // console.log('scrollViewHeight', this.scrollViewHeight)
  208. // console.log('topOffset', this.topOffset)
  209. })
  210. },
  211. // 索引列表被触摸
  212. touchStart(e) {
  213. // 获取触摸点信息
  214. const touchStartData = e.changedTouches[0]
  215. if (!touchStartData) return
  216. this.touching = true
  217. const {
  218. pageY,
  219. screenY
  220. } = touchStartData
  221. // 根据当前触摸点的坐标,获取当前触摸的为第几个字母
  222. // #ifdef APP-NVUE
  223. // 使用screenY要减去导航栏44和状态栏24高度
  224. const currentIndex = this.getIndexListLetter(screenY - 68)
  225. // #endif
  226. // #ifndef APP-NVUE
  227. const currentIndex = this.getIndexListLetter(pageY)
  228. // #endif
  229. this.setValueForTouch(currentIndex)
  230. },
  231. // 索引字母列表被触摸滑动中
  232. touchMove(e) {
  233. // 获取触摸点信息
  234. let touchMove = e.changedTouches[0]
  235. if (!touchMove) return;
  236. // 滑动结束后迅速开始第二次滑动时候 touching 为 false 造成不显示 indicator 问题
  237. if (!this.touching) {
  238. this.touching = true
  239. }
  240. const {
  241. pageY,
  242. screenY
  243. } = touchMove
  244. // #ifdef APP-NVUE
  245. // 使用screenY要减去导航栏44和状态栏24高度
  246. const currentIndex = this.getIndexListLetter(screenY - 68)
  247. // #endif
  248. // #ifndef APP-NVUE
  249. const currentIndex = this.getIndexListLetter(pageY)
  250. // #endif
  251. this.setValueForTouch(currentIndex)
  252. },
  253. // 触摸结束
  254. touchEnd(e) {
  255. // 延时一定时间后再隐藏指示器,为了让用户看的更直观,同时也是为了消除快速切换u-transition的show带来的影响
  256. sleep(300).then(() => {
  257. this.touching = false
  258. })
  259. },
  260. // 获取索引列表的尺寸以及单个字符的尺寸信息
  261. getIndexListLetterRect() {
  262. return new Promise(resolve => {
  263. // 延时一定时间,以获取dom尺寸
  264. // #ifndef APP-NVUE
  265. this.$uGetRect('.u-index-list__letter').then(size => {
  266. resolve(size)
  267. })
  268. // #endif
  269. // #ifdef APP-NVUE
  270. const ref = this.$refs['u-index-list__letter']
  271. dom.getComponentRect(ref, res => {
  272. resolve(res.size)
  273. })
  274. // #endif
  275. })
  276. },
  277. getIndexListScrollViewRect() {
  278. return new Promise(resolve => {
  279. // 延时一定时间,以获取dom尺寸
  280. // #ifndef APP-NVUE
  281. this.$uGetRect('.u-index-list__scroll-view').then(size => {
  282. resolve(size)
  283. })
  284. // #endif
  285. // #ifdef APP-NVUE
  286. const ref = this.$refs['u-index-list__scroll-view']
  287. dom.getComponentRect(ref, res => {
  288. resolve(res.size)
  289. })
  290. // #endif
  291. })
  292. },
  293. getIndexListRect() {
  294. return new Promise(resolve => {
  295. // 延时一定时间,以获取dom尺寸
  296. // #ifndef APP-NVUE
  297. this.$uGetRect('.u-index-list').then(size => {
  298. resolve(size)
  299. })
  300. // #endif
  301. // #ifdef APP-NVUE
  302. const ref = this.$refs['u-index-list']
  303. dom.getComponentRect(ref, res => {
  304. resolve(res.size)
  305. })
  306. // #endif
  307. })
  308. },
  309. // 设置indexList索引的尺寸信息
  310. setIndexListLetterInfo() {
  311. this.getIndexListLetterRect().then(size => {
  312. // console.log('getIndexListLetterRect', size)
  313. const {
  314. height
  315. } = size
  316. const sysData = sys()
  317. const windowHeight = sysData.windowHeight
  318. let customNavHeight = 0
  319. // 消除各端导航栏非原生和原生导致的差异,让索引列表字母对屏幕垂直居中
  320. if (this.customNavHeight == 0) {
  321. // #ifdef H5
  322. customNavHeight = sysData.windowTop
  323. // #endif
  324. // #ifndef H5
  325. // 在非H5中,为原生导航栏,其高度不算在windowHeight内,这里设置为负值,后面相加时变成减去其高度的一半
  326. customNavHeight = -(sysData.statusBarHeight + 44)
  327. // #endif
  328. } else {
  329. customNavHeight = getPx(this.customNavHeight)
  330. }
  331. this.getIndexListScrollViewRect().then(sizeScroll => {
  332. // console.log('sizeScroll', sizeScroll)
  333. this.letterInfo = {
  334. height,
  335. // 为了让字母列表对屏幕绝对居中,让其对导航栏进行修正,也即往上偏移导航栏的一半高度
  336. top: sizeScroll.height / 2,
  337. // top: (this.scrollViewHeight - height) / 2 + customNavHeight / 2,
  338. itemHeight: Math.floor(height / this.uIndexList.length)
  339. }
  340. })
  341. })
  342. },
  343. // 获取当前被触摸的索引字母
  344. getIndexListLetter(pageY) {
  345. this.pageY = pageY
  346. let {
  347. top,
  348. height,
  349. itemHeight
  350. } = this.letterInfo
  351. let index = this.currentIndex;
  352. // 对H5的pageY进行修正,这是由于uni-app自作多情在H5中将触摸点的坐标跟H5的导航栏结合导致的问题
  353. // #ifdef H5
  354. // pageY += sys().windowTop
  355. // #endif
  356. // 对第一和最后一个字母做边界处理,因为用户可能在字母列表上触摸到两端的尽头后依然继续滑动
  357. // console.log('top1', top)
  358. // console.log('height', height)
  359. top = top - (height / 2) // 减去transfrom的translateY值导致的高度
  360. pageY = pageY - this.topOffset
  361. // if (this.safeBottomFix) {
  362. // pageY = pageY + 34
  363. // }
  364. // console.log('topOffset', this.topOffset)
  365. // console.log('pageY', pageY)
  366. // console.log('top2', top)
  367. if (pageY < top) {
  368. index = 0
  369. } else if (pageY >= top + height) {
  370. // 如果超出了,取最后一个字母
  371. index = this.uIndexList.length - 1
  372. } else {
  373. // 将触摸点的Y轴偏移值,减去索引字母的top值,除以每个字母的高度,即可得到当前触摸点落在哪个字母上
  374. index = Math.floor((pageY - top) / itemHeight)
  375. }
  376. // console.log(index)
  377. return index
  378. },
  379. // 设置各项由触摸而导致变化的值
  380. async setValueForTouch(currentIndex) {
  381. // 如果偏移量太小,前后得出的会是同一个索引字母,为了防抖,进行返回
  382. if (currentIndex === this.activeIndex) return
  383. this.activeIndex = currentIndex
  384. // #ifndef APP-NVUE || MP-WEIXIN
  385. // 在非nvue中,由于anchor和item都在u-index-item中,所以需要对index-item进行偏移
  386. this.scrollIntoView = `u-index-item-${this.uIndexList[currentIndex].charCodeAt(0)}`
  387. // #endif
  388. // #ifdef MP-WEIXIN
  389. // 微信小程序下,scroll-view的scroll-into-view属性无法对slot中的内容的id生效,只能通过设置scrollTop的形式去移动滚动条
  390. const customNavHeight = this.customNavHeight
  391. // 获取header slot的尺寸信息
  392. const header = await this.getHeaderRect()
  393. // item的top值,在nvue下,模拟出的anchor的top,类似非nvue下的index-item的top
  394. let top = header.height
  395. // console.log(top)
  396. const anchors = this.anchors
  397. // 由于list组件无法获取cell的top值,这里通过header slot和各个item之间的height,模拟出类似非nvue下的位置信息
  398. let children = this.children.map((item, index) => {
  399. const child = {
  400. height: item.height,
  401. top: top
  402. }
  403. // 进行累加,给下一个item提供计算依据
  404. top = top + item.height
  405. // #ifdef APP-NVUE
  406. // 只有nvue下,需要将锚点的高度也累加,非nvue下锚点高度是包含在index-item中的。
  407. top = top + anchors[index].height
  408. // #endif
  409. return child
  410. })
  411. // console.log('this.children[currentIndex].top', children[currentIndex].top)
  412. if (children[currentIndex]?.top) {
  413. this.scrollTop = children[currentIndex].top - getPx(customNavHeight)
  414. }
  415. // #endif
  416. // #ifdef APP-NVUE
  417. // 在nvue中,由于cell和header为同级元素,所以实际是需要对header(anchor)进行偏移
  418. const anchor = `u-index-anchor-${this.uIndexList[currentIndex]}`
  419. // console.log(anchor)
  420. dom.scrollToElement(this.anchors[currentIndex].$refs[anchor], {
  421. offset: 0,
  422. animated: false
  423. })
  424. // #endif
  425. },
  426. getHeaderRect() {
  427. // 获取header slot的高度,因为list组件中获取元素的尺寸是没有top值的
  428. return new Promise(resolve => {
  429. if (!this.$slots.header) {
  430. resolve({
  431. width: 0,
  432. height: 0
  433. })
  434. }
  435. // #ifndef APP-NVUE
  436. this.$uGetRect('.u-index-list__header').then(size => {
  437. resolve(size)
  438. })
  439. // #endif
  440. // #ifdef APP-NVUE
  441. let headerRef = this.$refs.header
  442. if (!headerRef) {
  443. resolve({
  444. width: 0,
  445. height: 0
  446. })
  447. }
  448. dom.getComponentRect(headerRef, res => {
  449. resolve(res.size)
  450. })
  451. // #endif
  452. })
  453. },
  454. // scroll-view的滚动事件
  455. async scrollHandler(e) {
  456. if (this.touching || this.scrolling) return
  457. // 每过一定时间取样一次,减少资源损耗以及可能带来的卡顿
  458. this.scrolling = true
  459. sleep(10).then(() => {
  460. this.scrolling = false
  461. })
  462. let scrollTop = 0
  463. const len = this.children.length
  464. let children = this.children
  465. // #ifdef APP-NVUE
  466. // nvue下获取的滚动条偏移为负数,需要转为正数
  467. let sys = uni.getSystemInfoSync()
  468. scrollTop = Math.abs(e.contentOffset.y) / 10
  469. // console.log('native', e)
  470. // #endif
  471. // 获取header slot的尺寸信息
  472. const header = await this.getHeaderRect()
  473. // item的top值,在nvue下,模拟出的anchor的top,类似非nvue下的index-item的top
  474. let top = header.height
  475. const anchors = this.anchors
  476. // 由于list组件无法获取cell的top值,这里通过header slot和各个item之间的height,模拟出类似非nvue下的位置信息
  477. children = this.children.map((item, index) => {
  478. const child = {
  479. height: item.height,
  480. top: top
  481. }
  482. // 进行累加,给下一个item提供计算依据
  483. top = top + item.height
  484. // #ifdef APP-NVUE
  485. // 只有nvue下,需要将锚点的高度也累加,非nvue下锚点高度是包含在index-item中的。
  486. top = top + anchors[index].height
  487. // #endif
  488. return child
  489. })
  490. // #ifndef APP-NVUE
  491. // 非nvue通过detail获取滚动条位移
  492. scrollTop = e.detail.scrollTop
  493. // console.log('scrollTop', scrollTop, this.customNavHeight)
  494. // #endif
  495. // 在弹窗中需要加上弹窗距离顶部的高度topOffset
  496. scrollTop = scrollTop + getPx(this.customNavHeight)
  497. for (let i = 0; i < len; i++) {
  498. const item = children[i],
  499. nextItem = children[i + 1]
  500. // 如果滚动条高度小于第一个item的top值,此时无需设置任意字母为高亮
  501. if (scrollTop <= children[0].top || scrollTop >= children[len - 1].top + children[len - 1].height) {
  502. this.activeIndex = -1
  503. break
  504. } else if (!nextItem) {
  505. // 当不存在下一个item时,意味着历遍到了最后一个
  506. this.activeIndex = len - 1
  507. break
  508. } else if (scrollTop > item.top && scrollTop < nextItem.top) {
  509. this.activeIndex = i
  510. break
  511. }
  512. }
  513. },
  514. },
  515. }
  516. </script>
  517. <style lang="scss" scoped>
  518. @import "../../libs/css/components.scss";
  519. .u-index-list {
  520. &__letter {
  521. position: absolute;
  522. right: 0;
  523. text-align: center;
  524. z-index: 3;
  525. padding: 0 6px;
  526. width: 30px;
  527. &__item {
  528. width: 16px;
  529. height: 16px;
  530. border-radius: 100px;
  531. margin: 1px 0;
  532. @include flex;
  533. align-items: center;
  534. justify-content: center;
  535. &--active {
  536. background-color: $u-primary;
  537. }
  538. &__index {
  539. font-size: 12px;
  540. text-align: center;
  541. line-height: 12px;
  542. }
  543. }
  544. }
  545. &__indicator {
  546. width: 50px;
  547. height: 50px;
  548. border-radius: 100px 100px 0 100px;
  549. text-align: center;
  550. color: #ffffff;
  551. background-color: #c9c9c9;
  552. transform: rotate(-45deg);
  553. @include flex;
  554. justify-content: center;
  555. align-items: center;
  556. &__text {
  557. font-size: 28px;
  558. line-height: 28px;
  559. font-weight: bold;
  560. color: #fff;
  561. transform: rotate(45deg);
  562. text-align: center;
  563. }
  564. }
  565. }
  566. </style>