综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uv-index-list.vue 14KB

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