shangpin.vue 9.7 KB

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