index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 =
  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. if (!isDef(res)) {
  100. return;
  101. }
  102. this.sidebar = {
  103. height: res.height,
  104. top: res.top,
  105. };
  106. });
  107. },
  108. setDiffData({ target, data }) {
  109. const diffData = {};
  110. Object.keys(data).forEach((key) => {
  111. if (target.data[key] !== data[key]) {
  112. diffData[key] = data[key];
  113. }
  114. });
  115. if (Object.keys(diffData).length) {
  116. target.setData(diffData);
  117. }
  118. },
  119. getAnchorRect(anchor) {
  120. return getRect(anchor, '.van-index-anchor-wrapper').then((rect) => ({
  121. height: rect.height,
  122. top: rect.top,
  123. }));
  124. },
  125. getActiveAnchorIndex() {
  126. const { children, scrollTop } = this;
  127. const { sticky, stickyOffsetTop } = this.data;
  128. for (let i = this.children.length - 1; i >= 0; i--) {
  129. const preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  130. const reachTop = sticky ? preAnchorHeight + stickyOffsetTop : 0;
  131. if (reachTop + scrollTop >= children[i].top) {
  132. return i;
  133. }
  134. }
  135. return -1;
  136. },
  137. onScroll() {
  138. const { children = [], scrollTop } = this;
  139. if (!children.length) {
  140. return;
  141. }
  142. const { sticky, stickyOffsetTop, zIndex, highlightColor } = this.data;
  143. const active = this.getActiveAnchorIndex();
  144. this.setDiffData({
  145. target: this,
  146. data: {
  147. activeAnchorIndex: active,
  148. },
  149. });
  150. if (sticky) {
  151. let isActiveAnchorSticky = false;
  152. if (active !== -1) {
  153. isActiveAnchorSticky =
  154. children[active].top <= stickyOffsetTop + scrollTop;
  155. }
  156. children.forEach((item, index) => {
  157. if (index === active) {
  158. let wrapperStyle = '';
  159. let anchorStyle = `
  160. color: ${highlightColor};
  161. `;
  162. if (isActiveAnchorSticky) {
  163. wrapperStyle = `
  164. height: ${children[index].height}px;
  165. `;
  166. anchorStyle = `
  167. position: fixed;
  168. top: ${stickyOffsetTop}px;
  169. z-index: ${zIndex};
  170. color: ${highlightColor};
  171. `;
  172. }
  173. this.setDiffData({
  174. target: item,
  175. data: {
  176. active: true,
  177. anchorStyle,
  178. wrapperStyle,
  179. },
  180. });
  181. } else if (index === active - 1) {
  182. const currentAnchor = children[index];
  183. const currentOffsetTop = currentAnchor.top;
  184. const targetOffsetTop =
  185. index === children.length - 1
  186. ? this.top
  187. : children[index + 1].top;
  188. const parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  189. const translateY = parentOffsetHeight - currentAnchor.height;
  190. const anchorStyle = `
  191. position: relative;
  192. transform: translate3d(0, ${translateY}px, 0);
  193. z-index: ${zIndex};
  194. color: ${highlightColor};
  195. `;
  196. this.setDiffData({
  197. target: item,
  198. data: {
  199. active: true,
  200. anchorStyle,
  201. },
  202. });
  203. } else {
  204. this.setDiffData({
  205. target: item,
  206. data: {
  207. active: false,
  208. anchorStyle: '',
  209. wrapperStyle: '',
  210. },
  211. });
  212. }
  213. });
  214. }
  215. },
  216. onClick(event) {
  217. this.scrollToAnchor(event.target.dataset.index);
  218. },
  219. onTouchMove(event) {
  220. const sidebarLength = this.children.length;
  221. const touch = event.touches[0];
  222. const itemHeight = this.sidebar.height / sidebarLength;
  223. let index = Math.floor((touch.clientY - this.sidebar.top) / itemHeight);
  224. if (index < 0) {
  225. index = 0;
  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(
  240. (item) => item.data.index === this.data.indexList[index]
  241. );
  242. if (anchor) {
  243. anchor.scrollIntoView(this.scrollTop);
  244. this.$emit('select', anchor.data.index);
  245. }
  246. },
  247. },
  248. });