index.js 6.7 KB

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