index.vue 7.9 KB

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