n-tabs.vue 9.0 KB

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