swiper-list-nvue.nvue 13 KB

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