tabbar.nvue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <template>
  2. <view class="tabs">
  3. <scroll-view id="tab-bar" class="scroll-h" :scroll-x="true" :show-scrollbar="false" :scroll-into-view="scrollInto">
  4. <view v-for="(tab,index) in tabBars" :key="tab.id" class="uni-tab-item" :id="tab.id" :data-current="index" @click="ontabtap">
  5. <text class="uni-tab-item-title" :class="tabIndex==index ? 'uni-tab-item-title-active' : ''">{{tab.name}}</text>
  6. </view>
  7. </scroll-view>
  8. <view class="line-h"></view>
  9. <swiper :current="tabIndex" class="swiper-box" style="flex: 1;" :duration="300" @change="ontabchange">
  10. <swiper-item class="swiper-item" v-for="(tab,index1) in newsList" :key="index1">
  11. <!-- #ifdef APP-NVUE -->
  12. <list class="scroll-v list" enableBackToTop="true" scroll-y loadmoreoffset="15" @loadmore="loadMore(index1)">
  13. <refresh class="refresh" @refresh="onrefresh(index1)" @pullingdown="onpullingdown" :display="tab.refreshing ? 'show' : 'hide'">
  14. <div class="refresh-view">
  15. <image class="refresh-icon" :src="refreshIcon" :style="{width: (tab.refreshing || pulling) ? 0: '30px'}" :class="{'refresh-icon-active': tab.refreshFlag}"></image>
  16. <loading-indicator class="loading-icon" animating="true" v-if="tab.refreshing"></loading-indicator>
  17. <text class="loading-text">{{tab.refreshText}}</text>
  18. </div>
  19. </refresh>
  20. <cell v-for="(newsitem,index2) in tab.data" :key="newsitem.id">
  21. <media-item :options="newsitem" @close="close(index1,index2)" @click="goDetail(newsitem)"></media-item>
  22. </cell>
  23. <cell class="loading-more" v-if="tab.isLoading || tab.data.length > 4">
  24. <text class="loading-more-text">{{tab.loadingText}}</text>
  25. </cell>
  26. </list>
  27. <!-- #endif -->
  28. <!-- #ifndef APP-NVUE -->
  29. <scroll-view class="scroll-v list" enableBackToTop="true" scroll-y @scrolltolower="loadMore(index1)">
  30. <view v-for="(newsitem,index2) in tab.data" :key="newsitem.id">
  31. <media-item :options="newsitem" @close="close(index1,index2)" @click="goDetail(newsitem)"></media-item>
  32. </view>
  33. <view class="loading-more" v-if="tab.isLoading || tab.data.length > 4">
  34. <text class="loading-more-text">{{tab.loadingText}}</text>
  35. </view>
  36. </scroll-view>
  37. <!-- #endif -->
  38. </swiper-item>
  39. </swiper>
  40. </view>
  41. </template>
  42. <script>
  43. import mediaItem from './news-item.nvue';
  44. // 缓存每页最多
  45. const MAX_CACHE_DATA = 100;
  46. // 缓存页签数量
  47. const MAX_CACHE_PAGE = 3;
  48. const newsData = {
  49. data0: {
  50. "datetime": "40分钟前",
  51. "article_type": 0,
  52. "title": "uni-app行业峰会频频亮相,开发者反响热烈!",
  53. "source": "DCloud",
  54. "comment_count": 639
  55. },
  56. data1: {
  57. "datetime": "一天前",
  58. "article_type": 1,
  59. "title": "DCloud完成B2轮融资,uni-app震撼发布!",
  60. "image_url": "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b7c7f970-517d-11eb-97b7-0dc4655d6e68.jpg",
  61. "source": "DCloud",
  62. "comment_count": 11395
  63. },
  64. data2: {
  65. "datetime": "一天前",
  66. "article_type": 2,
  67. "title": "中国技术界小奇迹:HBuilder开发者突破200万",
  68. "image_url": "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b4cd3000-517d-11eb-a16f-5b3e54966275.jpg",
  69. "source": "DCloud",
  70. "comment_count": 11395
  71. },
  72. data3: {
  73. "article_type": 3,
  74. "image_list": [{
  75. "url": "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b2e201d0-517d-11eb-8a36-ebb87efcf8c0.jpg",
  76. "width": 563,
  77. "height": 316
  78. }, {
  79. "url": "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b4cd3000-517d-11eb-a16f-5b3e54966275.jpg",
  80. "width": 641,
  81. "height": 360
  82. }, {
  83. "url": "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b7c7f970-517d-11eb-97b7-0dc4655d6e68.jpg",
  84. "width": 640,
  85. "height": 360
  86. }],
  87. "datetime": "5分钟前",
  88. "title": "uni-app 支持使用 npm 安装第三方包,生态更趋丰富",
  89. "source": "DCloud",
  90. "comment_count": 11
  91. },
  92. data4: {
  93. "datetime": "2小时前",
  94. "article_type": 4,
  95. "title": "uni-app 支持原生小程序自定义组件,更开放、更自由",
  96. "image_url": "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b2e201d0-517d-11eb-8a36-ebb87efcf8c0.jpg",
  97. "source": "DCloud",
  98. "comment_count": 69
  99. }
  100. };
  101. export default {
  102. components: {
  103. mediaItem
  104. },
  105. data() {
  106. return {
  107. newsList: [],
  108. cacheTab: [],
  109. tabIndex: 0,
  110. tabBars: [{
  111. name: '关注',
  112. id: 'guanzhu'
  113. }, {
  114. name: '推荐',
  115. id: 'tuijian'
  116. }, {
  117. name: '体育',
  118. id: 'tiyu'
  119. }, {
  120. name: '热点',
  121. id: 'redian'
  122. }, {
  123. name: '财经',
  124. id: 'caijing'
  125. }, {
  126. name: '娱乐',
  127. id: 'yule'
  128. }, {
  129. name: '军事',
  130. id: 'junshi'
  131. }, {
  132. name: '历史',
  133. id: 'lishi'
  134. }, {
  135. name: '本地',
  136. id: 'bendi'
  137. }],
  138. scrollInto: "",
  139. showTips: false,
  140. navigateFlag: false,
  141. pulling: false,
  142. refreshIcon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAB5QTFRFcHBw3Nzct7e39vb2ycnJioqK7e3tpqam29vb////D8oK7wAAAAp0Uk5T////////////ALLMLM8AAABxSURBVHja7JVBDoAgDASrjqj//7CJBi90iyYeOHTPMwmFZrHjYyyFYYUy1bwUZqtJIYVxhf1a6u0R7iUvWsCcrEtwJHp8MwMdvh2amHduiZD3rpWId9+BgPd7Cc2LIkPyqvlQvKxKBJ//Qwq/CacAAwDUv0a0YuKhzgAAAABJRU5ErkJggg=="
  143. }
  144. },
  145. onLoad() {
  146. setTimeout(()=>{
  147. this.tabBars.forEach((tabBar) => {
  148. this.newsList.push({
  149. data: [],
  150. isLoading: false,
  151. refreshText: "",
  152. loadingText: '加载更多...'
  153. });
  154. });
  155. this.getList(0);
  156. },350)
  157. },
  158. methods: {
  159. getList(index) {
  160. let activeTab = this.newsList[index];
  161. let list = [];
  162. for (let i = 1; i <= 10; i++) {
  163. let item = Object.assign({}, newsData['data' + Math.floor(Math.random() * 5)]);
  164. item.id = this.newGuid();
  165. list.push(item);
  166. }
  167. activeTab.data = activeTab.data.concat(list);
  168. },
  169. goDetail(e) {
  170. if (this.navigateFlag) {
  171. return;
  172. }
  173. this.navigateFlag = true;
  174. uni.navigateTo({
  175. url: './detail/detail?title=' + e.title
  176. });
  177. setTimeout(() => {
  178. this.navigateFlag = false;
  179. }, 200)
  180. },
  181. close(index1, index2) {
  182. uni.showModal({
  183. content: '是否删除本条信息?',
  184. success: (res) => {
  185. if (res.confirm) {
  186. this.newsList[index1].data.splice(index2, 1);
  187. }
  188. }
  189. })
  190. },
  191. loadMore(e) {
  192. setTimeout(() => {
  193. this.getList(this.tabIndex);
  194. }, 500)
  195. },
  196. ontabtap(e) {
  197. let index = e.target.dataset.current || e.currentTarget.dataset.current;
  198. this.switchTab(index);
  199. },
  200. ontabchange(e) {
  201. let index = e.target.current || e.detail.current;
  202. this.switchTab(index);
  203. },
  204. switchTab(index) {
  205. if (this.newsList[index].data.length === 0) {
  206. this.getList(index);
  207. }
  208. if (this.tabIndex === index) {
  209. return;
  210. }
  211. // 缓存 tabId
  212. if (this.newsList[this.tabIndex].data.length > MAX_CACHE_DATA) {
  213. let isExist = this.cacheTab.indexOf(this.tabIndex);
  214. if (isExist < 0) {
  215. this.cacheTab.push(this.tabIndex);
  216. //console.log("cache index:: " + this.tabIndex);
  217. }
  218. }
  219. this.tabIndex = index;
  220. this.scrollInto = this.tabBars[index].id;
  221. // 释放 tabId
  222. if (this.cacheTab.length > MAX_CACHE_PAGE) {
  223. let cacheIndex = this.cacheTab[0];
  224. this.clearTabData(cacheIndex);
  225. this.cacheTab.splice(0, 1);
  226. //console.log("remove cache index:: " + cacheIndex);
  227. }
  228. },
  229. clearTabData(e) {
  230. this.newsList[e].data.length = 0;
  231. this.newsList[e].loadingText = "加载更多...";
  232. },
  233. refreshData() {},
  234. onrefresh(e) {
  235. var tab = this.newsList[this.tabIndex];
  236. if (!tab.refreshFlag) {
  237. return;
  238. }
  239. tab.refreshing = true;
  240. tab.refreshText = "正在刷新...";
  241. setTimeout(() => {
  242. this.refreshData();
  243. this.pulling = true;
  244. tab.refreshing = false;
  245. tab.refreshFlag = false;
  246. tab.refreshText = "已刷新";
  247. setTimeout(() => { // TODO fix ios和Android 动画时间相反问题
  248. this.pulling = false;
  249. }, 500);
  250. }, 2000);
  251. },
  252. onpullingdown(e) {
  253. var tab = this.newsList[this.tabIndex];
  254. if (tab.refreshing || this.pulling) {
  255. return;
  256. }
  257. if (Math.abs(e.pullingDistance) > Math.abs(e.viewHeight)) {
  258. tab.refreshFlag = true;
  259. tab.refreshText = "释放立即刷新";
  260. } else {
  261. tab.refreshFlag = false;
  262. tab.refreshText = "下拉可以刷新";
  263. }
  264. },
  265. newGuid() {
  266. let s4 = function() {
  267. return (65536 * (1 + Math.random()) | 0).toString(16).substring(1);
  268. }
  269. return (s4() + s4() + "-" + s4() + "-4" + s4().substr(0, 3) + "-" + s4() + "-" + s4() + s4() + s4()).toUpperCase();
  270. }
  271. }
  272. }
  273. </script>
  274. <style>
  275. /* #ifndef APP-PLUS */
  276. page {
  277. width: 100%;
  278. min-height: 100%;
  279. display: flex;
  280. }
  281. /* #endif */
  282. .tabs {
  283. flex: 1;
  284. flex-direction: column;
  285. overflow: hidden;
  286. background-color: #ffffff;
  287. /* #ifndef APP-PLUS */
  288. height: 100vh;
  289. /* #endif */
  290. }
  291. .scroll-h {
  292. width: 750rpx;
  293. /* #ifdef H5 */
  294. width:100%;
  295. /* #endif */
  296. height: 80rpx;
  297. flex-direction: row;
  298. /* #ifndef APP-PLUS */
  299. white-space: nowrap;
  300. /* #endif */
  301. /* flex-wrap: nowrap; */
  302. /* border-color: #cccccc;
  303. border-bottom-style: solid;
  304. border-bottom-width: 1px; */
  305. }
  306. .line-h {
  307. height: 1rpx;
  308. background-color: #cccccc;
  309. }
  310. .uni-tab-item {
  311. /* #ifndef APP-PLUS */
  312. display: inline-block;
  313. /* #endif */
  314. flex-wrap: nowrap;
  315. padding-left: 34rpx;
  316. padding-right: 34rpx;
  317. }
  318. .uni-tab-item-title {
  319. color: #555;
  320. font-size: 30rpx;
  321. height: 80rpx;
  322. line-height: 80rpx;
  323. flex-wrap: nowrap;
  324. /* #ifndef APP-PLUS */
  325. white-space: nowrap;
  326. /* #endif */
  327. }
  328. .uni-tab-item-title-active {
  329. color: #007AFF;
  330. }
  331. .swiper-box {
  332. flex: 1;
  333. }
  334. .swiper-item {
  335. flex: 1;
  336. flex-direction: row;
  337. }
  338. .scroll-v {
  339. flex: 1;
  340. /* #ifndef MP-ALIPAY */
  341. flex-direction: column;
  342. /* #endif */
  343. width: 750rpx;
  344. width:100%;
  345. }
  346. .update-tips {
  347. position: absolute;
  348. left: 0;
  349. top: 41px;
  350. right: 0;
  351. padding-top: 5px;
  352. padding-bottom: 5px;
  353. background-color: #FDDD9B;
  354. align-items: center;
  355. justify-content: center;
  356. text-align: center;
  357. }
  358. .update-tips-text {
  359. font-size: 14px;
  360. color: #ffffff;
  361. }
  362. .refresh {
  363. width: 750rpx;
  364. width:100%;
  365. height: 64px;
  366. justify-content: center;
  367. }
  368. .refresh-view {
  369. flex-direction: row;
  370. flex-wrap: nowrap;
  371. align-items: center;
  372. justify-content: center;
  373. }
  374. .refresh-icon {
  375. width: 30px;
  376. height: 30px;
  377. transition-duration: .5s;
  378. transition-property: transform;
  379. transform: rotate(0deg);
  380. transform-origin: 15px 15px;
  381. }
  382. .refresh-icon-active {
  383. transform: rotate(180deg);
  384. }
  385. .loading-icon {
  386. width: 20px;
  387. height: 20px;
  388. margin-right: 5px;
  389. color: #999999;
  390. }
  391. .loading-text {
  392. margin-left: 2px;
  393. font-size: 16px;
  394. color: #999999;
  395. }
  396. .loading-more {
  397. align-items: center;
  398. justify-content: center;
  399. padding-top: 10px;
  400. padding-bottom: 10px;
  401. text-align: center;
  402. }
  403. .loading-more-text {
  404. font-size: 28rpx;
  405. color: #999;
  406. }
  407. </style>