index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <uni-shadow-root class="vant-index-bar-index"><view class="van-index-bar">
  3. <slot></slot>
  4. <view v-if="showSidebar" class="van-index-bar__sidebar" @click.stop.prevent="onClick" @touchmove.stop.prevent="onTouchMove" @touchend.stop.prevent="onTouchStop" @touchcancel.stop.prevent="onTouchStop">
  5. <view v-for="(item,index) in (indexList)" :key="item.index" class="van-index-bar__index" :style="'z-index: '+(zIndex + 1)+'; color: '+(activeAnchorIndex === index ? highlightColor : '')" :data-index="index">
  6. {{ item }}
  7. </view>
  8. </view>
  9. </view></uni-shadow-root>
  10. </template>
  11. <script>
  12. global['__wxRoute'] = 'vant/index-bar/index'
  13. import { GREEN } from '../common/color';
  14. import { VantComponent } from '../common/component';
  15. import { useChildren } from '../common/relation';
  16. import { getRect, isDef } from '../common/utils';
  17. import { pageScrollMixin } from '../mixins/page-scroll';
  18. const indexList = () => {
  19. const indexList = [];
  20. const charCodeOfA = 'A'.charCodeAt(0);
  21. for (let i = 0; i < 26; i++) {
  22. indexList.push(String.fromCharCode(charCodeOfA + i));
  23. }
  24. return indexList;
  25. };
  26. VantComponent({
  27. relation: useChildren('index-anchor', function () {
  28. this.updateData();
  29. }),
  30. props: {
  31. sticky: {
  32. type: Boolean,
  33. value: true,
  34. },
  35. zIndex: {
  36. type: Number,
  37. value: 1,
  38. },
  39. highlightColor: {
  40. type: String,
  41. value: GREEN,
  42. },
  43. stickyOffsetTop: {
  44. type: Number,
  45. value: 0,
  46. },
  47. indexList: {
  48. type: Array,
  49. value: indexList(),
  50. },
  51. },
  52. mixins: [
  53. pageScrollMixin(function (event) {
  54. this.scrollTop = (event === null || event === void 0 ? void 0 : event.scrollTop) || 0;
  55. this.onScroll();
  56. }),
  57. ],
  58. data: {
  59. activeAnchorIndex: null,
  60. showSidebar: false,
  61. },
  62. created() {
  63. this.scrollTop = 0;
  64. },
  65. methods: {
  66. updateData() {
  67. wx.nextTick(() => {
  68. if (this.timer != null) {
  69. clearTimeout(this.timer);
  70. }
  71. this.timer = setTimeout(() => {
  72. this.setData({
  73. showSidebar: !!this.children.length,
  74. });
  75. this.setRect().then(() => {
  76. this.onScroll();
  77. });
  78. }, 0);
  79. });
  80. },
  81. setRect() {
  82. return Promise.all([
  83. this.setAnchorsRect(),
  84. this.setListRect(),
  85. this.setSiderbarRect(),
  86. ]);
  87. },
  88. setAnchorsRect() {
  89. return Promise.all(this.children.map((anchor) => getRect(anchor, '.van-index-anchor-wrapper').then((rect) => {
  90. Object.assign(anchor, {
  91. height: rect.height,
  92. top: rect.top + this.scrollTop,
  93. });
  94. })));
  95. },
  96. setListRect() {
  97. return getRect(this, '.van-index-bar').then((rect) => {
  98. if (!isDef(rect)) {
  99. return;
  100. }
  101. Object.assign(this, {
  102. height: rect.height,
  103. top: rect.top + this.scrollTop,
  104. });
  105. });
  106. },
  107. setSiderbarRect() {
  108. return getRect(this, '.van-index-bar__sidebar').then((res) => {
  109. if (!isDef(res)) {
  110. return;
  111. }
  112. this.sidebar = {
  113. height: res.height,
  114. top: res.top,
  115. };
  116. });
  117. },
  118. setDiffData({ target, data }) {
  119. const diffData = {};
  120. Object.keys(data).forEach((key) => {
  121. if (target.data[key] !== data[key]) {
  122. diffData[key] = data[key];
  123. }
  124. });
  125. if (Object.keys(diffData).length) {
  126. target.setData(diffData);
  127. }
  128. },
  129. getAnchorRect(anchor) {
  130. return getRect(anchor, '.van-index-anchor-wrapper').then((rect) => ({
  131. height: rect.height,
  132. top: rect.top,
  133. }));
  134. },
  135. getActiveAnchorIndex() {
  136. const { children, scrollTop } = this;
  137. const { sticky, stickyOffsetTop } = this.data;
  138. for (let i = this.children.length - 1; i >= 0; i--) {
  139. const preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  140. const reachTop = sticky ? preAnchorHeight + stickyOffsetTop : 0;
  141. if (reachTop + scrollTop >= children[i].top) {
  142. return i;
  143. }
  144. }
  145. return -1;
  146. },
  147. onScroll() {
  148. const { children = [], scrollTop } = this;
  149. if (!children.length) {
  150. return;
  151. }
  152. const { sticky, stickyOffsetTop, zIndex, highlightColor } = this.data;
  153. const active = this.getActiveAnchorIndex();
  154. this.setDiffData({
  155. target: this,
  156. data: {
  157. activeAnchorIndex: active,
  158. },
  159. });
  160. if (sticky) {
  161. let isActiveAnchorSticky = false;
  162. if (active !== -1) {
  163. isActiveAnchorSticky =
  164. children[active].top <= stickyOffsetTop + scrollTop;
  165. }
  166. children.forEach((item, index) => {
  167. if (index === active) {
  168. let wrapperStyle = '';
  169. let anchorStyle = `
  170. color: ${highlightColor};
  171. `;
  172. if (isActiveAnchorSticky) {
  173. wrapperStyle = `
  174. height: ${children[index].height}px;
  175. `;
  176. anchorStyle = `
  177. position: fixed;
  178. top: ${stickyOffsetTop}px;
  179. z-index: ${zIndex};
  180. color: ${highlightColor};
  181. `;
  182. }
  183. this.setDiffData({
  184. target: item,
  185. data: {
  186. active: true,
  187. anchorStyle,
  188. wrapperStyle,
  189. },
  190. });
  191. }
  192. else if (index === active - 1) {
  193. const currentAnchor = children[index];
  194. const currentOffsetTop = currentAnchor.top;
  195. const targetOffsetTop = index === children.length - 1
  196. ? this.top
  197. : children[index + 1].top;
  198. const parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  199. const translateY = parentOffsetHeight - currentAnchor.height;
  200. const anchorStyle = `
  201. position: relative;
  202. transform: translate3d(0, ${translateY}px, 0);
  203. z-index: ${zIndex};
  204. color: ${highlightColor};
  205. `;
  206. this.setDiffData({
  207. target: item,
  208. data: {
  209. active: true,
  210. anchorStyle,
  211. },
  212. });
  213. }
  214. else {
  215. this.setDiffData({
  216. target: item,
  217. data: {
  218. active: false,
  219. anchorStyle: '',
  220. wrapperStyle: '',
  221. },
  222. });
  223. }
  224. });
  225. }
  226. },
  227. onClick(event) {
  228. this.scrollToAnchor(event.target.dataset.index);
  229. },
  230. onTouchMove(event) {
  231. const sidebarLength = this.children.length;
  232. const touch = event.touches[0];
  233. const itemHeight = this.sidebar.height / sidebarLength;
  234. let index = Math.floor((touch.clientY - this.sidebar.top) / itemHeight);
  235. if (index < 0) {
  236. index = 0;
  237. }
  238. else if (index > sidebarLength - 1) {
  239. index = sidebarLength - 1;
  240. }
  241. this.scrollToAnchor(index);
  242. },
  243. onTouchStop() {
  244. this.scrollToAnchorIndex = null;
  245. },
  246. scrollToAnchor(index) {
  247. if (typeof index !== 'number' || this.scrollToAnchorIndex === index) {
  248. return;
  249. }
  250. this.scrollToAnchorIndex = index;
  251. const anchor = this.children.find((item) => item.data.index === this.data.indexList[index]);
  252. if (anchor) {
  253. anchor.scrollIntoView(this.scrollTop);
  254. this.$emit('select', anchor.data.index);
  255. }
  256. },
  257. },
  258. });
  259. export default global['__wxComponents']['vant/index-bar/index']
  260. </script>
  261. <style platform="mp-weixin">
  262. @import '../common/index.css';.van-index-bar{position:relative}.van-index-bar__sidebar{display:flex;flex-direction:column;position:fixed;right:0;text-align:center;top:50%;transform:translateY(-50%);-webkit-user-select:none;user-select:none}.van-index-bar__index{font-size:var(--index-bar-index-font-size,10px);font-weight:500;line-height:var(--index-bar-index-line-height,14px);padding:0 var(--padding-base,4px) 0 var(--padding-md,16px)}
  263. </style>