u-index-list.vue 14 KB

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