Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app" v-cloak>
    <tabs v-model="activeKey">
        <pane label="标签一" name="1">
            标签一的内容
        </pane>
        <pane label="标签二" name="2">
            标签二的内容
        </pane>
        <pane label="标签三" name="3">
            标签三的内容
        </pane>
    </tabs>
</div>


              
            
!

CSS

              
                [v-cloak] {
    display: none;
}

.tabs {
    font-size: 14px;
    color: #657180;
}

.tabs-bar:after {
    content: '';
    display: block;
    width: 100%;
    height: 1px;
    background: #d7dde4;
    margin-top: -1px;
}

.tabs-tab {
    display: inline-block;
    padding: 4px 16px;
    margin-right: 6px;
    background: #fff;
    border: 1px solid #d7dde4;
    cursor: pointer;
    position: relative;
}

.tabs-tab-active {
    color: #3399ff;
    border-top: 1px solid #3399ff;
    border-bottom: 1px solid #fff;
}

.tabs-tab-active:before {
    content: '';
    display: block;
    height: 1px;
    background: #3399ff;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
}

.tabs-content {
    padding: 8px 0;
}
              
            
!

JS

              
                Vue.component('pane', {
    name: 'pane',
    template: `
<div class="pane" v-show="show">
    <slot></slot>
</div>
`,
    props: {
        name: {
            type: String
        },
        label: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            show: true
        }
    },
    methods: {
        updateNav() {
            this.$parent.updateNav();
        }
    },
    watch: {
        label() {
            this.updateNav();
        }
    },
    mounted() {
        this.updateNav();
    }
});

Vue.component('tabs', {
    template: `
<div class="tabs">
    <div class="tabs-bar">
        <div :class="tabCls(item)" v-for="(item,index) in navList" @click="handleChange(index)">{{item.label}}</div>
    </div>
    <div class="tabs-content">
        <slot></slot>
    </div>
</div>   
    `,
    props: {
        value: {
            type: [String, Number]
        }
    },
    data() {
        return {
            currentValue: this.value,
            navList: []
        }
    },
    methods: {
        tabCls(item) {
            return ['tabs-tab', {
                'tabs-tab-active': item.name === this.currentValue
            }]
        },
        //点击tab标题时触发
        handleChange(index) {
            var nav = this.navList[index];
            var name = nav.name;

            // 改变当前选中的tab,并触发下面的watch
            this.currentValue = name;
            //更新value
            this.$emit('input', name);
            //触发一个自定义事件,供父级使用
            this.$emit('on-click', name)
        },
        getTabs() {
            //通过遍历子组件,得到所有的pane组件
            return this.$children.filter(function (item) {
                return item.$options.name === 'pane';
            })
        },
        updateNav() {
            this.navList = [];
            //设置this的引用,在function回调里,this指向的并不是Vue实例
            var _this = this;

            this.getTabs().forEach(function (pane, index) {
                _this.navList.push({
                    label: pane.label,
                    name: pane.name || index
                });
                //如果没有给pane设置name,默认设置它的索引
                if (!pane.name) {
                    pane.name = index;
                }
                //设置当前选中的tab的索引
                if (index === 0) {
                    if (!_this.currentValue) {
                        _this.currentValue = pane.name || index;
                    }
                }
            });
            this.updateStatus();
        },
        updateStatus() {
            var tabs = this.getTabs();
            var _this = this;

            //显示当前选中的tab对应的pane组件,隐藏没有选中的
            tabs.forEach(function (tab) {
                return tab.show = tab.name === _this.currentValue;
            })
        }
    },
    watch: {
        value(val) {
            this.currentValue = val;
        },
        currentValue() {
            //在当前选中的tab发生变化时,更新pane的显示状态
            this.updateStatus();
        }
    }
});
new Vue({
    el: "#app",
    data: {
        activeKey: '1'
    }
})
              
            
!
999px

Console