index.js 8.0 KB

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