index.js 8.3 KB

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