n-tabs.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <view class="tabs">
  3. <scroll-view ref="tabbar1" id="tab-bar" class="tab-bar" :scroll="false" :scroll-x="false" :show-scrollbar="false" :scroll-into-view="scrollInto">
  4. <view class="tabBox">
  5. <view class="es x-c" style="width:100%;flex-direction:row;min-height: 40px;">
  6. <view class="uni-tab-item" v-for="(tab,index) in tabs" :key="tab.id" :id="tab.id" :ref="'tabitem'+index"
  7. :data-id="index" :data-current="index" @click="ontabtap">
  8. <text class="uni-tab-item-title" :class="tabIndex==index ? 'uni-tab-item-title-active' : ''">{{tab.name}}</text>
  9. </view>
  10. <view class="es serchItem" :style="{width:searchWid+'px'}">
  11. <image class="es es-w-38 es-h-36" src="/static/image/course/new_search.png" @tap="handleSearch"></image>
  12. </view>
  13. </view>
  14. <view class="scroll-view-indicator" v-if="false">
  15. <view ref="underline" class="scroll-view-underline" :class="isTap ? 'scroll-view-animation':''" :style="{left: indicatorLineLeft + 'px', width: indicatorLineWidth + 'px'}"></view>
  16. </view>
  17. </view>
  18. </scroll-view>
  19. </view>
  20. </template>
  21. <script>
  22. // #ifdef APP-PLUS
  23. const dom = weex.requireModule('dom');
  24. // #endif
  25. export default {
  26. props: {
  27. tabs: {
  28. type: Array,
  29. default() {
  30. return []
  31. }
  32. },
  33. value: {
  34. type: [String, Number],
  35. default: 0
  36. },
  37. fixed: Boolean, // 是否悬浮,默认false
  38. tabWidth: Number, // 每个tab的宽度,默认不设置值,为flex平均分配; 如果指定宽度,则不使用flex,每个tab居左,超过则水平滑动(单位默认rpx)
  39. height: {
  40. type: Number,
  41. default: 64
  42. },
  43. norColor: {
  44. type: String,
  45. default: '#333333'
  46. },
  47. actColor: {
  48. type: String,
  49. default: '#FF5C03'
  50. },
  51. },
  52. data() {
  53. return {
  54. tabIndex: 0,
  55. cacheTab: [],
  56. scrollInto: "",
  57. navigateFlag: false,
  58. indicatorLineLeft: 0,
  59. indicatorLineWidth: 0,
  60. isTap: false,
  61. searchWid:72,
  62. }
  63. },
  64. mounted() {
  65. this._lastTabIndex = 0;
  66. this.swiperWidth = 0;
  67. this.tabbarWidth = 0;
  68. this.tabListSize = {};
  69. this._touchTabIndex = 0;
  70. this.pageList = [];
  71. setTimeout(e => {
  72. this.switchTab(this.tabIndex);
  73. this.selectorQuery();
  74. }, 200)
  75. },
  76. onReady() {
  77. },
  78. methods: {
  79. handleSearch() {
  80. this.$emit('search')
  81. },
  82. ontabtap(e) {
  83. let index = e.target.dataset.current || e.currentTarget.dataset.current;
  84. //let offsetIndex = this._touchTabIndex = Math.abs(index - this._lastTabIndex) > 1;
  85. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  86. this.isTap = true;
  87. var currentSize = this.tabListSize[index];
  88. this.updateIndicator(currentSize.left, currentSize.width);
  89. this._touchTabIndex = index;
  90. // #endif
  91. this.switchTab(index);
  92. },
  93. onswiperscroll(e) {
  94. },
  95. animationfinish(e) {
  96. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  97. let index = e.detail.current;
  98. if (this._touchTabIndex === index) {
  99. this.isTap = false;
  100. }
  101. this._lastTabIndex = index;
  102. this.switchTab(index);
  103. this.updateIndicator(this.tabListSize[index].left, this.tabListSize[index].width);
  104. // #endif
  105. },
  106. selectorQuery() {
  107. // #ifdef APP-NVUE
  108. dom.getComponentRect(this.$refs["tabbar1"], res => {
  109. this.tabbarWidth = res.size.width;
  110. });
  111. // dom.getComponentRect(this.$refs['swiper1'], res => {
  112. // this.swiperWidth = res.size.width;
  113. // console.log("qxj swiper res:"+JSON.stringify(res));
  114. // });
  115. // 因 nvue 暂不支持 class 查询
  116. var queryTabSize = uni.createSelectorQuery().in(this);
  117. for (var i = 0; i < this.tabs.length; i++) {
  118. queryTabSize.select('#' + this.tabs[i].id).boundingClientRect();
  119. }
  120. queryTabSize.exec(rects => {
  121. rects.forEach((rect) => {
  122. this.tabListSize[rect.dataset.id] = rect;
  123. });
  124. this.updateIndicator(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex].width);
  125. this.switchTab(this.tabIndex);
  126. });
  127. // #endif
  128. // #ifdef MP-WEIXIN || H5 || MP-QQ
  129. uni.createSelectorQuery().in(this).select('.tab-box').fields({
  130. dataset: true,
  131. size: true,
  132. }, (res) => {
  133. this.swiperWidth = res.width;
  134. }).exec();
  135. uni.createSelectorQuery().in(this).selectAll('.uni-tab-item').boundingClientRect((rects) => {
  136. rects.forEach((rect) => {
  137. this.tabListSize[rect.dataset.id] = rect;
  138. })
  139. this.updateIndicator(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex].width);
  140. }).exec();
  141. // #endif
  142. },
  143. getElementSize(dom, ref, id) {
  144. dom.getComponentRect(ref, res => {
  145. this.tabListSize[id] = res.size;
  146. });
  147. },
  148. updateIndicator(left, width) {
  149. let dWidth=26;
  150. this.searchWid=width;
  151. this.indicatorLineLeft = left+(width-dWidth)/2.0;
  152. this.indicatorLineWidth = dWidth;
  153. },
  154. switchTab(index) {
  155. if (this.tabIndex === index) {
  156. return;
  157. }
  158. // 缓存 tabId
  159. this.tabIndex = index;
  160. // #ifdef APP-NVUE
  161. //this.scrollTabTo(index);
  162. // #endif
  163. // #ifndef APP-NVUE
  164. this.scrollInto = this.tabList[index].id;
  165. // #endif
  166. },
  167. scrollTabTo(index) {
  168. const el = this.$refs['tabitem' + index][0];
  169. let offset = 0;
  170. // TODO fix ios offset
  171. if (index > 0) {
  172. offset = this.tabbarWidth / 2 - this.tabListSize[index].width / 2;
  173. if (this.tabListSize[index].right < this.tabbarWidth / 2) {
  174. offset = this.tabListSize[0].width;
  175. }
  176. }
  177. dom.scrollToElement(el, {
  178. offset: -offset
  179. });
  180. },
  181. loadTabData(index) {
  182. },
  183. clearTabData(index) {
  184. }
  185. }
  186. }
  187. </script>
  188. <style>
  189. /* #ifndef APP-PLUS */
  190. page {
  191. width: 100%;
  192. min-height: 100%;
  193. display: flex;
  194. }
  195. /* #endif */
  196. .tabs {
  197. flex: 1;
  198. flex-direction: column;
  199. overflow: hidden;
  200. background: transparent;
  201. /* #ifdef MP-ALIPAY || MP-BAIDU */
  202. height: 100vh;
  203. /* #endif */
  204. }
  205. .tab-bar {
  206. /* #ifdef APP-PLUS */
  207. width: 750rpx;
  208. /* #endif */
  209. height: 42px;
  210. flex-direction: row;
  211. /* #ifndef APP-PLUS */
  212. white-space: nowrap;
  213. /* #endif */
  214. background: transparent;
  215. }
  216. .tabBox{
  217. flex-direction: column;display:
  218. flex;flex: 1;
  219. background: transparent;
  220. position: relative;
  221. }
  222. /* #ifndef APP-NVUE */
  223. .tab-bar ::-webkit-scrollbar {
  224. display: none;
  225. width: 0 !important;
  226. height: 0 !important;
  227. -webkit-appearance: none;
  228. background: transparent;
  229. }
  230. /* #endif */
  231. .scroll-view-indicator {
  232. position: absolute;
  233. height: 2px;
  234. background-color: transparent;
  235. left: 0;right: 0;
  236. bottom: 4px;
  237. /* background-color: transparent; */
  238. }
  239. .scroll-view-underline {
  240. position: absolute;
  241. top: 0;
  242. bottom: 0;
  243. width: 0;
  244. background-color: #ffffff;
  245. }
  246. .scroll-view-animation {
  247. transition-duration: 0.2s;
  248. transition-property: left;
  249. }
  250. .tab-bar-line {
  251. height: 1px;
  252. background-color: #cccccc;
  253. }
  254. .tab-box {
  255. flex: 1;
  256. }
  257. .uni-tab-item {
  258. /* #ifndef APP-PLUS */
  259. display: inline-block;
  260. /* #endif */
  261. flex-wrap: nowrap;
  262. padding-left: 20px;
  263. padding-right: 20px;
  264. }
  265. .uni-tab-item-title {
  266. color: #fff;
  267. font-size: 16px;
  268. font-weight: 600;
  269. height: 40px;
  270. line-height: 40px;
  271. flex-wrap: nowrap;
  272. /* #ifndef APP-PLUS */
  273. white-space: nowrap;
  274. /* #endif */
  275. }
  276. .uni-tab-item-title-active {
  277. color: #ffffff;
  278. }
  279. .serchItem{
  280. padding-left: 20px;
  281. padding-right: 20px;
  282. height: 40px;
  283. line-height: 40px;
  284. position: absolute;
  285. right: 40rpx;
  286. top:14rpx;
  287. }
  288. .swiper-item {
  289. flex: 1;
  290. flex-direction: column;
  291. }
  292. .page-item {
  293. flex: 1;
  294. flex-direction: row;
  295. position: absolute;
  296. left: 0;
  297. top: 0;
  298. right: 0;
  299. bottom: 0;
  300. }
  301. </style>