网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在Vue.js项目中实现一个标签页组件

本篇文章给大家分享的是有关怎么在Vue.js项目中实现一个标签页组件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联公司主营尤溪网站建设的网络公司,主营网站建设方案,app开发定制,尤溪h5重庆小程序开发公司搭建,尤溪网站营销推广欢迎尤溪等地区企业咨询

1 基础版

因为 tabs 组件中的标题是在 pane 组件中定义的,所以在初始化或者动态变化标题时,tabs 组件需要从 pane 组件中获取标题。

html:




 
 标签页组件
 



 
 
 火星疑似发现“外星人墓地”?至今无法解释
 
 
 全美沸腾!湖人队4年1.2亿迎顶级后卫,詹姆斯:有他就能夺冠
 
 
 阿米尔汗谈中国武侠 想拍印度版《鹿鼎记》
 
 

pane 组件:

Vue.component('pane', {
 name: 'pane',
 template: '\
 \
 \
 
\  ',  props: {  //标题  label: {  type: String,  default: ''  }  },  data: function () {  return {  //显示或隐藏  isShow: true  }  },  methods: {  //通知父组件,更新标题  init() {  this.$parent.init();  }  },  watch: {  //当 label 值发生变化时,更新标题  label() {  this.init();  }  },  //挂载时,更新标题  mounted() {  this.init();  } });

在 pane 组件中,我们做了以下设计:

tabs 组件:

Vue.component('tabs', {
 template: '\
 \
 \
 \
 \
 {{ item.label }}\
 
\  \  \  \  \  \  ',  props: {  value: {  type: [String, Number]  }  },  data: function () {  return {  currentIndex: this.value,  titleList: []//存放标题  }  },  methods: {  //设置样式  tabClass: function (item) {  return ['tabs-tab', {  //为当前选中的 tab 添加选中样式  'tabs-tab-active': (item.name === this.currentIndex)  }]  },  //获取定义的所有 pane 组件  getTabs() {  return this.$children.filter(function (item) {  return item.$options.name === 'pane';  })  },  //更新 pane 是否显示状态  updateIsShowStatus() {  var tabs = this.getTabs();  var that = this;  //迭代判断并设置某个标签页是显示还是隐藏状态  tabs.forEach(function (tab, index) {  return tab.isShow = (index === that.currentIndex);  })  },  //初始化  init() {  /**  * 初始化标题数组  */  this.titleList = [];  var that = this;//设置 this 引用  this.getTabs().forEach(function (tab, index) {  that.titleList.push({   label: tab.label,   name: index  });  //初始化默认选中的 tab 索引  if (index === 0) {   if (!that.currentIndex) {   that.currentIndex = index;   }  }  });  this.updateIsShowStatus();  },  //点击 tab 标题时,更新 value 值为相应的索引值  change: function (index) {  var nav = this.titleList[index];  var name = nav.name;  this.$emit('input', name);  }  },  watch: {  //当 value 值发生改变时,更新 currentIndex  value: function (val) {  this.currentIndex = val;  },  //当 currentIndex 值发生改变时,更新 pane 是否显示状态  currentIndex: function () {  this.updateIsShowStatus();  }  } });

总结如下:

样式:

[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:hover {
 color: #336699;
 font-weight: bolder;
}

.tabs-tab-active {
 color: #336699;
 border-top: 1px solid #336699;
 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;
}

.pane {
 margin-top: 26px;
 font-size: 16px;
 line-height: 24px;
 color: #333;
 text-align: justify;
}

效果:

怎么在Vue.js项目中实现一个标签页组件

2 关闭属性

我们为 pane 组件新增一个 closable 属性,用于控制该标签是否可关闭。

在子窗口组件的 props 中,新增 closable 属性:

props: {
	...
	//是否可关闭
	closable: {
		type: Boolean,
		default: false
	}
}

在标签页组件中的模板中,新增关闭标签:

...
template: '\
\
	\
		\
		\
			{{ item.label }}\
			\
			\
		\
		\
		 \
			\
		\
	 ',
...

在标签页组件中的方法中,新增了 close(),用于执行关闭标签页逻辑:

close: function (index, name) {
 //删除对应的标题元素
	this.titleList.splice(index, 1);

	var tabs = this.getTabs();
	var that = this;
	//迭代判断并设置点击的标签页是隐藏状态
	tabs.forEach(function (tab, index) {
		if (index === name) {
			return tab.isShow = false;
		}
	});
}

新增的样式:

.close{
 color: #FF6666;
}
.close::before {
 content: "\2716";
}

.close:hover {
 color: #990033;
 font-weight: bolder;
}

为需要添加关闭标签的 pane ,添加 closable 属性:


 
 
 火星疑似发现“外星人墓地”?至今无法解释
 
 
 全美沸腾!湖人队4年1.2亿迎顶级后卫,詹姆斯:有他就能夺冠
 
 
 阿米尔汗谈中国武侠 想拍印度版《鹿鼎记》
 
 

效果:

怎么在Vue.js项目中实现一个标签页组件

3 切换动画

我们在切换标签页时,加上滑动动画吧,这很简单,只要在激活的样式中加上 transform 与 transition 样式即可:

.tabs-tab-active {
 color: #336699;
 border-top: 1px solid #336699;
 border-bottom: 1px solid #fff;
 transform:translateY(-1px);
 transition: transform 0.5s;
}

效果:

怎么在Vue.js项目中实现一个标签页组件

以上就是怎么在Vue.js项目中实现一个标签页组件,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网页题目:怎么在Vue.js项目中实现一个标签页组件
路径分享:http://bjjierui.cn/article/ijgcjs.html

其他资讯