index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <uni-shadow-root class="weapp-lib-index-bar-index"><view class="van-index-bar">
  3. <slot></slot>
  4. <view v-if="showSidebar" class="van-index-bar__sidebar" @click.stop.prevent="onClick" @touchmove.stop.prevent="onTouchMove" @touchend.stop.prevent="onTouchStop" @touchcancel.stop.prevent="onTouchStop">
  5. <view v-for="(item,index) in (indexList)" :key="item.index" class="van-index-bar__index" :style="'z-index: '+(zIndex + 1)+'; color: '+(activeAnchorIndex === index ? highlightColor : '')" :data-index="index">
  6. {{ item }}
  7. </view>
  8. </view>
  9. </view></uni-shadow-root>
  10. </template>
  11. <script>
  12. global['__wxRoute'] = 'weapp/lib/index-bar/index'
  13. "use strict";
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. var color_1 = require("../common/color");
  16. var component_1 = require("../common/component");
  17. var relation_1 = require("../common/relation");
  18. var utils_1 = require("../common/utils");
  19. var page_scroll_1 = require("../mixins/page-scroll");
  20. var indexList = function () {
  21. var indexList = [];
  22. var charCodeOfA = 'A'.charCodeAt(0);
  23. for (var i = 0; i < 26; i++) {
  24. indexList.push(String.fromCharCode(charCodeOfA + i));
  25. }
  26. return indexList;
  27. };
  28. (0, component_1.VantComponent)({
  29. relation: (0, relation_1.useChildren)('index-anchor', function () {
  30. this.updateData();
  31. }),
  32. props: {
  33. sticky: {
  34. type: Boolean,
  35. value: true,
  36. },
  37. zIndex: {
  38. type: Number,
  39. value: 1,
  40. },
  41. highlightColor: {
  42. type: String,
  43. value: color_1.GREEN,
  44. },
  45. stickyOffsetTop: {
  46. type: Number,
  47. value: 0,
  48. },
  49. indexList: {
  50. type: Array,
  51. value: indexList(),
  52. },
  53. },
  54. mixins: [
  55. (0, page_scroll_1.pageScrollMixin)(function (event) {
  56. this.scrollTop = (event === null || event === void 0 ? void 0 : event.scrollTop) || 0;
  57. this.onScroll();
  58. }),
  59. ],
  60. data: {
  61. activeAnchorIndex: null,
  62. showSidebar: false,
  63. },
  64. created: function () {
  65. this.scrollTop = 0;
  66. },
  67. methods: {
  68. updateData: function () {
  69. var _this = this;
  70. wx.nextTick(function () {
  71. if (_this.timer != null) {
  72. clearTimeout(_this.timer);
  73. }
  74. _this.timer = setTimeout(function () {
  75. _this.setData({
  76. showSidebar: !!_this.children.length,
  77. });
  78. _this.setRect().then(function () {
  79. _this.onScroll();
  80. });
  81. }, 0);
  82. });
  83. },
  84. setRect: function () {
  85. return Promise.all([
  86. this.setAnchorsRect(),
  87. this.setListRect(),
  88. this.setSiderbarRect(),
  89. ]);
  90. },
  91. setAnchorsRect: function () {
  92. var _this = this;
  93. return Promise.all(this.children.map(function (anchor) {
  94. return (0, utils_1.getRect)(anchor, '.van-index-anchor-wrapper').then(function (rect) {
  95. Object.assign(anchor, {
  96. height: rect.height,
  97. top: rect.top + _this.scrollTop,
  98. });
  99. });
  100. }));
  101. },
  102. setListRect: function () {
  103. var _this = this;
  104. return (0, utils_1.getRect)(this, '.van-index-bar').then(function (rect) {
  105. if (!(0, utils_1.isDef)(rect)) {
  106. return;
  107. }
  108. Object.assign(_this, {
  109. height: rect.height,
  110. top: rect.top + _this.scrollTop,
  111. });
  112. });
  113. },
  114. setSiderbarRect: function () {
  115. var _this = this;
  116. return (0, utils_1.getRect)(this, '.van-index-bar__sidebar').then(function (res) {
  117. if (!(0, utils_1.isDef)(res)) {
  118. return;
  119. }
  120. _this.sidebar = {
  121. height: res.height,
  122. top: res.top,
  123. };
  124. });
  125. },
  126. setDiffData: function (_a) {
  127. var target = _a.target, data = _a.data;
  128. var diffData = {};
  129. Object.keys(data).forEach(function (key) {
  130. if (target.data[key] !== data[key]) {
  131. diffData[key] = data[key];
  132. }
  133. });
  134. if (Object.keys(diffData).length) {
  135. target.setData(diffData);
  136. }
  137. },
  138. getAnchorRect: function (anchor) {
  139. return (0, utils_1.getRect)(anchor, '.van-index-anchor-wrapper').then(function (rect) { return ({
  140. height: rect.height,
  141. top: rect.top,
  142. }); });
  143. },
  144. getActiveAnchorIndex: function () {
  145. var _a = this, children = _a.children, scrollTop = _a.scrollTop;
  146. var _b = this.data, sticky = _b.sticky, stickyOffsetTop = _b.stickyOffsetTop;
  147. for (var i = this.children.length - 1; i >= 0; i--) {
  148. var preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  149. var reachTop = sticky ? preAnchorHeight + stickyOffsetTop : 0;
  150. if (reachTop + scrollTop >= children[i].top) {
  151. return i;
  152. }
  153. }
  154. return -1;
  155. },
  156. onScroll: function () {
  157. var _this = this;
  158. var _a = this, _b = _a.children, children = _b === void 0 ? [] : _b, scrollTop = _a.scrollTop;
  159. if (!children.length) {
  160. return;
  161. }
  162. var _c = this.data, sticky = _c.sticky, stickyOffsetTop = _c.stickyOffsetTop, zIndex = _c.zIndex, highlightColor = _c.highlightColor;
  163. var active = this.getActiveAnchorIndex();
  164. this.setDiffData({
  165. target: this,
  166. data: {
  167. activeAnchorIndex: active,
  168. },
  169. });
  170. if (sticky) {
  171. var isActiveAnchorSticky_1 = false;
  172. if (active !== -1) {
  173. isActiveAnchorSticky_1 =
  174. children[active].top <= stickyOffsetTop + scrollTop;
  175. }
  176. children.forEach(function (item, index) {
  177. if (index === active) {
  178. var wrapperStyle = '';
  179. var anchorStyle = "\n color: ".concat(highlightColor, ";\n ");
  180. if (isActiveAnchorSticky_1) {
  181. wrapperStyle = "\n height: ".concat(children[index].height, "px;\n ");
  182. anchorStyle = "\n position: fixed;\n top: ".concat(stickyOffsetTop, "px;\n z-index: ").concat(zIndex, ";\n color: ").concat(highlightColor, ";\n ");
  183. }
  184. _this.setDiffData({
  185. target: item,
  186. data: {
  187. active: true,
  188. anchorStyle: anchorStyle,
  189. wrapperStyle: wrapperStyle,
  190. },
  191. });
  192. }
  193. else if (index === active - 1) {
  194. var currentAnchor = children[index];
  195. var currentOffsetTop = currentAnchor.top;
  196. var targetOffsetTop = index === children.length - 1
  197. ? _this.top
  198. : children[index + 1].top;
  199. var parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  200. var translateY = parentOffsetHeight - currentAnchor.height;
  201. var anchorStyle = "\n position: relative;\n transform: translate3d(0, ".concat(translateY, "px, 0);\n z-index: ").concat(zIndex, ";\n color: ").concat(highlightColor, ";\n ");
  202. _this.setDiffData({
  203. target: item,
  204. data: {
  205. active: true,
  206. anchorStyle: anchorStyle,
  207. },
  208. });
  209. }
  210. else {
  211. _this.setDiffData({
  212. target: item,
  213. data: {
  214. active: false,
  215. anchorStyle: '',
  216. wrapperStyle: '',
  217. },
  218. });
  219. }
  220. });
  221. }
  222. },
  223. onClick: function (event) {
  224. this.scrollToAnchor(event.target.dataset.index);
  225. },
  226. onTouchMove: function (event) {
  227. var sidebarLength = this.children.length;
  228. var touch = event.touches[0];
  229. var itemHeight = this.sidebar.height / sidebarLength;
  230. var index = Math.floor((touch.clientY - this.sidebar.top) / itemHeight);
  231. if (index < 0) {
  232. index = 0;
  233. }
  234. else if (index > sidebarLength - 1) {
  235. index = sidebarLength - 1;
  236. }
  237. this.scrollToAnchor(index);
  238. },
  239. onTouchStop: function () {
  240. this.scrollToAnchorIndex = null;
  241. },
  242. scrollToAnchor: function (index) {
  243. var _this = this;
  244. if (typeof index !== 'number' || this.scrollToAnchorIndex === index) {
  245. return;
  246. }
  247. this.scrollToAnchorIndex = index;
  248. var anchor = this.children.find(function (item) { return item.data.index === _this.data.indexList[index]; });
  249. if (anchor) {
  250. anchor.scrollIntoView(this.scrollTop);
  251. this.$emit('select', anchor.data.index);
  252. }
  253. },
  254. },
  255. });
  256. export default global['__wxComponents']['weapp/lib/index-bar/index']
  257. </script>
  258. <style platform="mp-weixin">
  259. @import '../common/index.css';.van-index-bar{position:relative}.van-index-bar__sidebar{display:flex;flex-direction:column;position:fixed;right:0;text-align:center;top:50%;transform:translateY(-50%);-webkit-user-select:none;user-select:none}.van-index-bar__index{font-size:var(--index-bar-index-font-size,10px);font-weight:500;line-height:var(--index-bar-index-line-height,14px);padding:0 var(--padding-base,4px) 0 var(--padding-md,16px)}
  260. </style>