inProcess.nvue 8.6 KB

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