index.js 8.2KB

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