index.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { VantComponent } from '../common/component';
  2. import { GREEN } from '../common/color';
  3. const indexList = () => {
  4. const indexList = [];
  5. const charCodeOfA = 'A'.charCodeAt(0);
  6. for (let i = 0; i < 26; i++) {
  7. indexList.push(String.fromCharCode(charCodeOfA + i));
  8. }
  9. return indexList;
  10. };
  11. VantComponent({
  12. relation: {
  13. name: 'index-anchor',
  14. type: 'descendant',
  15. current: 'index-bar',
  16. linked() {
  17. this.updateData();
  18. },
  19. linkChanged() {
  20. this.updateData();
  21. },
  22. unlinked() {
  23. this.updateData();
  24. }
  25. },
  26. props: {
  27. sticky: {
  28. type: Boolean,
  29. value: true
  30. },
  31. zIndex: {
  32. type: Number,
  33. value: 1
  34. },
  35. highlightColor: {
  36. type: String,
  37. value: GREEN
  38. },
  39. scrollTop: {
  40. type: Number,
  41. value: 0,
  42. observer: 'onScroll'
  43. },
  44. stickyOffsetTop: {
  45. type: Number,
  46. value: 0
  47. },
  48. indexList: {
  49. type: Array,
  50. value: indexList()
  51. }
  52. },
  53. data: {
  54. activeAnchorIndex: null,
  55. showSidebar: false
  56. },
  57. methods: {
  58. updateData() {
  59. this.timer && clearTimeout(this.timer);
  60. this.timer = setTimeout(() => {
  61. this.children = this.getRelationNodes('../index-anchor/index');
  62. this.setData({
  63. showSidebar: !!this.children.length
  64. });
  65. this.setRect().then(() => {
  66. this.onScroll();
  67. });
  68. }, 0);
  69. },
  70. setRect() {
  71. return Promise.all([
  72. this.setAnchorsRect(),
  73. this.setListRect(),
  74. this.setSiderbarRect()
  75. ]);
  76. },
  77. setAnchorsRect() {
  78. return Promise.all(this.children.map(anchor => anchor
  79. .getRect('.van-index-anchor-wrapper')
  80. .then((rect) => {
  81. Object.assign(anchor, {
  82. height: rect.height,
  83. top: rect.top + this.data.scrollTop
  84. });
  85. })));
  86. },
  87. setListRect() {
  88. return this.getRect('.van-index-bar').then((rect) => {
  89. Object.assign(this, {
  90. height: rect.height,
  91. top: rect.top + this.data.scrollTop
  92. });
  93. });
  94. },
  95. setSiderbarRect() {
  96. return this.getRect('.van-index-bar__sidebar').then(res => {
  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 anchor
  116. .getRect('.van-index-anchor-wrapper')
  117. .then((rect) => ({
  118. height: rect.height,
  119. top: rect.top
  120. }));
  121. },
  122. getActiveAnchorIndex() {
  123. const { children } = this;
  124. const { sticky, scrollTop, 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 = [] } = this;
  136. if (!children.length) {
  137. return;
  138. }
  139. const { sticky, stickyOffsetTop, zIndex, highlightColor, scrollTop } = 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. }
  179. else if (index === active - 1) {
  180. const currentAnchor = children[index];
  181. const currentOffsetTop = currentAnchor.top;
  182. const targetOffsetTop = 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. }
  201. else {
  202. this.setDiffData({
  203. target: item,
  204. data: {
  205. active: false,
  206. anchorStyle: '',
  207. wrapperStyle: ''
  208. }
  209. });
  210. }
  211. });
  212. }
  213. },
  214. onClick(event) {
  215. this.scrollToAnchor(event.target.dataset.index);
  216. },
  217. onTouchMove(event) {
  218. const sidebarLength = this.children.length;
  219. const touch = event.touches[0];
  220. const itemHeight = this.sidebar.height / sidebarLength;
  221. let index = Math.floor((touch.clientY - this.sidebar.top) / itemHeight);
  222. if (index < 0) {
  223. index = 0;
  224. }
  225. else if (index > sidebarLength - 1) {
  226. index = sidebarLength - 1;
  227. }
  228. this.scrollToAnchor(index);
  229. },
  230. onTouchStop() {
  231. this.scrollToAnchorIndex = null;
  232. },
  233. scrollToAnchor(index) {
  234. if (typeof index !== 'number' || this.scrollToAnchorIndex === index) {
  235. return;
  236. }
  237. this.scrollToAnchorIndex = index;
  238. const anchor = this.children.find((item) => item.data.index === this.data.indexList[index]);
  239. if (anchor) {
  240. this.$emit('select', anchor.data.index);
  241. wx.pageScrollTo({
  242. duration: 0,
  243. scrollTop: anchor.top
  244. });
  245. }
  246. }
  247. }
  248. });