index.js 6.7 KB

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