swiper-list.nvue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <template>
  2. <view class="tabs">
  3. <scroll-view ref="tabbar1" id="tab-bar" class="tab-bar" :scroll="false" :scroll-x="true" :show-scrollbar="false"
  4. :scroll-into-view="scrollInto">
  5. <view style="flex-direction: column;">
  6. <view style="flex-direction: row;">
  7. <view class="uni-tab-item" v-for="(tab,index) in tabList" :key="tab.id" :id="tab.id" :ref="'tabitem'+index"
  8. :data-id="index" :data-current="index" @click="ontabtap">
  9. <text class="uni-tab-item-title" :class="tabIndex==index ? 'uni-tab-item-title-active' : ''">{{tab.name}}</text>
  10. </view>
  11. </view>
  12. <view class="scroll-view-indicator">
  13. <view ref="underline" class="scroll-view-underline" :class="isTap ? 'scroll-view-animation':''"
  14. :style="{left: indicatorLineLeft + 'px', width: indicatorLineWidth + 'px'}"></view>
  15. </view>
  16. </view>
  17. </scroll-view>
  18. <view class="tab-bar-line"></view>
  19. <swiper class="tab-view" ref="swiper1" id="tab-bar-view" :current="tabIndex" :duration="300" @change="onswiperchange"
  20. @transition="onswiperscroll" @animationfinish="animationfinish" @onAnimationEnd="animationfinish">
  21. <swiper-item class="swiper-item" v-for="(page, index) in tabList" :key="index">
  22. <!-- #ifndef MP-ALIPAY -->
  23. <swiperPage class="swiper-page" :pid="page.pageid" ref="page"></swiperPage>
  24. <!-- #endif -->
  25. <!-- #ifdef MP-ALIPAY -->
  26. <swiperPage class="swiper-page" :pid="page.pageid" :ref="'page' + index"></swiperPage>
  27. <!-- #endif -->
  28. </swiper-item>
  29. </swiper>
  30. </view>
  31. </template>
  32. <script>
  33. // #ifdef APP-NVUE
  34. const dom = weex.requireModule('dom');
  35. // #endif
  36. // 缓存每页最多
  37. const MAX_CACHE_DATA = 100;
  38. // 缓存页签数量
  39. const MAX_CACHE_PAGE = 3;
  40. const TAB_PRELOAD_OFFSET = 1;
  41. import swiperPage from './swiper-page.nvue';
  42. export default {
  43. components: {
  44. swiperPage
  45. },
  46. data() {
  47. return {
  48. tabList: [],
  49. tabIndex: 0,
  50. cacheTab: [],
  51. scrollInto: "",
  52. indicatorLineLeft: 0,
  53. indicatorLineWidth: 0,
  54. isTap: false
  55. }
  56. },
  57. onLoad() {
  58. for (var i = 0; i < 6; i++) {
  59. this.tabList.push({
  60. id: "tab" + i,
  61. name: 'Tab ' + (i + 1),
  62. pageid: i + 1
  63. })
  64. }
  65. },
  66. onReady() {
  67. this._lastTabIndex = 0;
  68. this.swiperWidth = 0;
  69. this.tabbarWidth = 0;
  70. this.tabListSize = {};
  71. this._touchTabIndex = 0;
  72. // #ifndef MP-ALIPAY
  73. this.pageList = this.$refs.page;
  74. // #endif
  75. // #ifdef MP-ALIPAY
  76. this.pageList = [];
  77. for (var i = 0; i < this.tabList.length; i++) {
  78. this.pageList.push(this.$refs['page' + i][0]);
  79. }
  80. // #endif
  81. this.switchTab(this.tabIndex);
  82. this.getTabbarItemsSize();
  83. },
  84. methods: {
  85. ontabtap(e) {
  86. let index = e.target.dataset.current || e.currentTarget.dataset.current;
  87. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  88. this.isTap = true;
  89. var currentSize = this.tabListSize[index];
  90. this.updateIndicator(currentSize.left, currentSize.width);
  91. this._touchTabIndex = index;
  92. // #endif
  93. this.switchTab(index);
  94. },
  95. onswiperchange(e) {
  96. // 注意:百度小程序会触发2次
  97. // #ifndef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  98. let index = e.target.current || e.detail.current;
  99. this.switchTab(index);
  100. // #endif
  101. },
  102. onswiperscroll(e) {
  103. if (this.isTap) {
  104. return;
  105. }
  106. var offsetX = e.detail.dx;
  107. var preloadIndex = this._lastTabIndex;
  108. if (offsetX > TAB_PRELOAD_OFFSET) {
  109. preloadIndex++;
  110. } else if (offsetX < -TAB_PRELOAD_OFFSET) {
  111. preloadIndex--;
  112. }
  113. if (preloadIndex === this._lastTabIndex || preloadIndex < 0 || preloadIndex > this.pageList.length - 1) {
  114. return;
  115. }
  116. if (this.pageList[preloadIndex].dataList.length === 0) {
  117. this.loadTabData(preloadIndex);
  118. }
  119. /// 计算 tabbar 底线
  120. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  121. var percentage = Math.abs(this.swiperWidth / offsetX);
  122. var currentSize = this.tabListSize[this._lastTabIndex];
  123. var preloadSize = this.tabListSize[preloadIndex];
  124. var lineL = currentSize.left + (preloadSize.left - currentSize.left) / percentage;
  125. var lineW = currentSize.width + (preloadSize.width - currentSize.width) / percentage;
  126. this.updateIndicator(lineL, lineW);
  127. // #endif
  128. },
  129. animationfinish(e) {
  130. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  131. let index = e.detail.current;
  132. if (this._touchTabIndex === index) {
  133. this.isTap = false;
  134. }
  135. this._lastTabIndex = index;
  136. this.switchTab(index);
  137. this.updateIndicator(this.tabListSize[index].left, this.tabListSize[index].width);
  138. // #endif
  139. },
  140. getTabbarItemsSize() {
  141. // #ifdef APP-NVUE
  142. // 查询 tabbar 宽度
  143. uni.createSelectorQuery().in(this).select('#tab-bar').boundingClientRect().exec(rect => {
  144. this.tabbarWidth = rect[0].width;
  145. });
  146. // 查询 tabview 宽度
  147. uni.createSelectorQuery().in(this).select('#tab-bar-view').boundingClientRect().exec(rect => {
  148. this.swiperWidth = rect[0].width;
  149. });
  150. // 因 nvue 暂不支持 class 查询
  151. var queryTabSize = uni.createSelectorQuery().in(this);
  152. for (var i = 0; i < this.tabList.length; i++) {
  153. queryTabSize.select('#' + this.tabList[i].id).boundingClientRect();
  154. }
  155. queryTabSize.exec(rects => {
  156. console.log(JSON.stringify(rects));
  157. rects.forEach((rect) => {
  158. this.tabListSize[rect.dataset.id] = rect;
  159. })
  160. });
  161. // #endif
  162. // #ifdef MP-WEIXIN || H5 || MP-QQ
  163. uni.createSelectorQuery().in(this).select('.tab-view').fields({
  164. dataset: true,
  165. size: true,
  166. }, (res) => {
  167. this.swiperWidth = res.width;
  168. }).exec();
  169. uni.createSelectorQuery().in(this).selectAll('.uni-tab-item').boundingClientRect((rects) => {
  170. rects.forEach((rect) => {
  171. this.tabListSize[rect.dataset.id] = rect;
  172. })
  173. }).exec();
  174. // #endif
  175. // #ifdef APP-NVUE || H5 || MP-WEIXIN || MP-QQ
  176. setTimeout(() => {
  177. this.updateIndicator(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex].width);
  178. }, 100)
  179. // #endif
  180. },
  181. updateIndicator(left, width) {
  182. this.indicatorLineLeft = left;
  183. this.indicatorLineWidth = width;
  184. },
  185. switchTab(index) {
  186. if (this.pageList[index].dataList.length === 0) {
  187. this.loadTabData(index);
  188. }
  189. if (this.tabIndex === index) {
  190. return;
  191. }
  192. // 缓存 tabId
  193. if (this.pageList[this.tabIndex].dataList.length > MAX_CACHE_DATA) {
  194. let isExist = this.cacheTab.indexOf(this.tabIndex);
  195. if (isExist < 0) {
  196. this.cacheTab.push(this.tabIndex);
  197. }
  198. }
  199. this.tabIndex = index;
  200. // #ifdef APP-NVUE
  201. this.scrollTabTo(index);
  202. // #endif
  203. // #ifndef APP-NVUE
  204. this.scrollInto = this.tabList[index].id;
  205. // #endif
  206. // 释放 tabId
  207. if (this.cacheTab.length > MAX_CACHE_PAGE) {
  208. let cacheIndex = this.cacheTab[0];
  209. this.clearTabData(cacheIndex);
  210. this.cacheTab.splice(0, 1);
  211. }
  212. },
  213. scrollTabTo(index) {
  214. const el = this.$refs['tabitem' + index][0];
  215. let offset = 0;
  216. // TODO fix ios offset
  217. if (index > 0) {
  218. offset = this.tabbarWidth / 2 - this.tabListSize[index].width / 2;
  219. if (this.tabListSize[index].right < this.tabbarWidth / 2) {
  220. offset = this.tabListSize[0].width;
  221. }
  222. }
  223. dom.scrollToElement(el, {
  224. offset: -offset
  225. });
  226. },
  227. loadTabData(index) {
  228. this.pageList[index].loadData();
  229. },
  230. clearTabData(index) {
  231. this.pageList[index].clear();
  232. }
  233. }
  234. }
  235. </script>
  236. <style>
  237. /* #ifndef APP-PLUS */
  238. page {
  239. width: 100%;
  240. min-height: 100%;
  241. display: flex;
  242. }
  243. /* #endif */
  244. .tabs {
  245. flex: 1;
  246. flex-direction: column;
  247. overflow: hidden;
  248. background-color: #ffffff;
  249. /* #ifdef MP-ALIPAY || MP-BAIDU */
  250. height: 100vh;
  251. /* #endif */
  252. }
  253. .tab-bar {
  254. width: 750rpx;
  255. height: 84rpx;
  256. flex-direction: row;
  257. /* #ifndef APP-PLUS */
  258. white-space: nowrap;
  259. /* #endif */
  260. }
  261. /* #ifndef APP-NVUE */
  262. .tab-bar ::-webkit-scrollbar {
  263. display: none;
  264. width: 0 !important;
  265. height: 0 !important;
  266. -webkit-appearance: none;
  267. background: transparent;
  268. }
  269. /* #endif */
  270. .scroll-view-indicator {
  271. position: relative;
  272. height: 2px;
  273. background-color: transparent;
  274. }
  275. .scroll-view-underline {
  276. position: absolute;
  277. top: 0;
  278. bottom: 0;
  279. width: 0;
  280. background-color: #007AFF;
  281. }
  282. .scroll-view-animation {
  283. transition-duration: 0.2s;
  284. transition-property: left;
  285. }
  286. .tab-bar-line {
  287. height: 1rpx;
  288. background-color: #cccccc;
  289. }
  290. .tab-view {
  291. flex: 1;
  292. }
  293. .uni-tab-item {
  294. /* #ifndef APP-PLUS */
  295. display: inline-block;
  296. /* #endif */
  297. flex-wrap: nowrap;
  298. padding-left: 25px;
  299. padding-right: 25px;
  300. }
  301. .uni-tab-item-title {
  302. color: #555;
  303. font-size: 30rpx;
  304. height: 80rpx;
  305. line-height: 80rpx;
  306. flex-wrap: nowrap;
  307. /* #ifndef APP-PLUS */
  308. white-space: nowrap;
  309. /* #endif */
  310. }
  311. .uni-tab-item-title-active {
  312. color: #007AFF;
  313. }
  314. .swiper-item {
  315. flex: 1;
  316. flex-direction: column;
  317. }
  318. .swiper-page {
  319. flex: 1;
  320. flex-direction: row;
  321. position: absolute;
  322. left: 0;
  323. top: 0;
  324. right: 0;
  325. bottom: 0;
  326. }
  327. </style>