[Improvement] Tab: add line-width prop (#988)

This commit is contained in:
neverland 2018-05-04 20:22:12 +08:00 committed by GitHub
parent 61e895789a
commit 8fb0596f36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 18 deletions

View File

@ -141,6 +141,7 @@ In swipeable mode, you can switch tabs with swipe gestrue in the content
| v-model | Index of active tab | `String` `Number` | `0` | - | | v-model | Index of active tab | `String` `Number` | `0` | - |
| type | There are two style tabs, set this attribute to change tab style | `String` | `line` | `card` | | type | There are two style tabs, set this attribute to change tab style | `String` | `line` | `card` |
| duration | Toggle tab's animation time | `Number` | `0.2` | - | - | | duration | Toggle tab's animation time | `Number` | `0.2` | - | - |
| line-width | Width of tab line (px) | `Number` | Equal to current tab width | - |
| swipe-threshold | Set swipe tabs threshold | `Number` | `4` | - | - | | swipe-threshold | Set swipe tabs threshold | `Number` | `4` | - | - |
| sticky | Whether to use sticky mode | `Boolean` | `false` | - | | sticky | Whether to use sticky mode | `Boolean` | `false` | - |
| swipeable | Whether to switch tabs with swipe gestrue in the content | `Boolean` | `false` | - | | swipeable | Whether to switch tabs with swipe gestrue in the content | `Boolean` | `false` | - |

View File

@ -143,6 +143,7 @@ export default {
| v-model | 当前激活的 tab | `String` `Number` | `0` | - | | v-model | 当前激活的 tab | `String` `Number` | `0` | - |
| type | Tab 样式类型 | `String` | `line` | `card` | | type | Tab 样式类型 | `String` | `line` | `card` |
| duration | 切换 tab 的动画时间 | `Number` | `0.2` | - | | duration | 切换 tab 的动画时间 | `Number` | `0.2` | - |
| line-width | 底部条宽度px | `Number` | 与当前 Tab 等宽 | - |
| swipe-threshold | 滚动阀值,设置 Tab 超过多少个可滚动 | `Number` | `4` | - | | swipe-threshold | 滚动阀值,设置 Tab 超过多少个可滚动 | `Number` | `4` | - |
| sticky | 是否使用粘性定位布局 | `Boolean` | `false` | - | | sticky | 是否使用粘性定位布局 | `Boolean` | `false` | - |
| swipeable | 是否可以滑动内容切换 | `Boolean` | `false` | - | | swipeable | 是否可以滑动内容切换 | `Boolean` | `false` | - |

View File

@ -8,7 +8,7 @@
]" ]"
> >
<div :class="b('nav', [type])" ref="nav"> <div :class="b('nav', [type])" ref="nav">
<div v-if="type === 'line'" :class="b('nav-bar')" :style="navBarStyle" /> <div v-if="type === 'line'" :class="b('line')" :style="lineStyle" />
<div <div
v-for="(tab, index) in tabs" v-for="(tab, index) in tabs"
ref="tabs" ref="tabs"
@ -53,6 +53,8 @@ export default create({
props: { props: {
sticky: Boolean, sticky: Boolean,
lineWidth: Number,
swipeable: Boolean,
active: { active: {
type: [Number, String], type: [Number, String],
default: 0 default: 0
@ -68,8 +70,7 @@ export default create({
swipeThreshold: { swipeThreshold: {
type: Number, type: Number,
default: 4 default: 4
}, }
swipeable: Boolean
}, },
data() { data() {
@ -77,7 +78,7 @@ export default create({
tabs: [], tabs: [],
position: 'content-top', position: 'content-top',
curActive: 0, curActive: 0,
navBarStyle: {} lineStyle: {}
}; };
}, },
@ -97,12 +98,12 @@ export default create({
tabs(tabs) { tabs(tabs) {
this.correctActive(this.curActive || this.active); this.correctActive(this.curActive || this.active);
this.setNavBar(); this.setLine();
}, },
curActive() { curActive() {
this.scrollIntoView(); this.scrollIntoView();
this.setNavBar(); this.setLine();
// scroll to correct position // scroll to correct position
if (this.position === 'page-top' || this.position === 'content-bottom') { if (this.position === 'page-top' || this.position === 'content-bottom') {
@ -117,7 +118,7 @@ export default create({
mounted() { mounted() {
this.correctActive(this.active); this.correctActive(this.active);
this.setNavBar(); this.setLine();
this.$nextTick(() => { this.$nextTick(() => {
if (this.sticky) { if (this.sticky) {
@ -153,12 +154,12 @@ export default create({
// whether to bind content swipe listener // whether to bind content swipe listener
swipeableHandler(init) { swipeableHandler(init) {
const swipeableEl = this.$refs.content; const { content } = this.$refs;
const action = init ? on : off;
(init ? on : off)(swipeableEl, 'touchstart', this.touchStart); action(content, 'touchstart', this.touchStart);
(init ? on : off)(swipeableEl, 'touchmove', this.touchMove); action(content, 'touchmove', this.touchMove);
(init ? on : off)(swipeableEl, 'touchend', this.onTouchEnd); action(content, 'touchend', this.onTouchEnd);
(init ? on : off)(swipeableEl, 'touchcancel', this.onTouchEnd); action(content, 'touchcancel', this.onTouchEnd);
}, },
// watch swipe touch end // watch swipe touch end
@ -192,16 +193,19 @@ export default create({
}, },
// update nav bar style // update nav bar style
setNavBar() { setLine() {
this.$nextTick(() => { this.$nextTick(() => {
if (!this.$refs.tabs) { if (!this.$refs.tabs) {
return; return;
} }
const tab = this.$refs.tabs[this.curActive]; const tab = this.$refs.tabs[this.curActive];
this.navBarStyle = { const width = this.lineWidth || tab.offsetWidth;
width: `${tab.offsetWidth || 0}px`, const left = tab.offsetLeft + (tab.offsetWidth - width) / 2;
transform: `translate(${tab.offsetLeft || 0}px, 0)`,
this.lineStyle = {
width: `${width}px`,
transform: `translateX(${left}px)`,
transitionDuration: `${this.duration}s` transitionDuration: `${this.duration}s`
}; };
}); });

View File

@ -76,7 +76,7 @@ $van-tabs-card-height: 30px;
} }
} }
&__nav-bar { &__line {
z-index: 1; z-index: 1;
left: 0; left: 0;
bottom: 15px; bottom: 15px;