描述
以csdn平台为例,当我们要发表一片文章时,一般会选择几个标签作为文章的索引。这些标签可以是系统提供的,也可以是自定义的。
效果
image.png代码
这里主要利用了label标签的用法,当你点击label区域的内容,会将焦点转移到里面的input,这在各组件库里也是大量用到。
逻辑实现方面,必须区别出自定义的标签和系统标签,我们可以用一个flag字段做标记。如果flag为1,在删除的时候必须添加回系统标签列表,而自定义的标签直接删掉即可。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.css">
<style>
.label-cont {
width: 100%;
max-width: 100%;
line-height: 1.5715;
box-sizing: border-box;
padding: 4px 11px;
border: 1px solid #dcdfe6;
border-radius: 2px;
display: flex;
flex-direction: row;
align-items: center;
}
.label-cont:hover {
border-color: #177ddc;
border-right-width: 1px !important;
box-shadow: 0 0 0 2px rgba(23, 125, 220, 0.2);
transition: all 0.3s;
}
.label-cont .label-cont-outer {
word-break: break-all;
word-wrap: break-word;
}
.label-cont .label-cont-outer .label-cont-item {
padding: 2px 5px;
display: inline-flex;
align-items: center;
white-space: nowrap;
overflow: auto;
text-overflow: ellipsis;
background: #097EFF;
border-radius: 2px;
margin: 5px;
color:#fff;
}
.label-cont .label-cont-outer .label-cont-item i {
margin-left: 3px;
font-size: 10px;
opacity: 0.7;
}
.label-cont .label-cont-outer .label-cont-item i:hover {
opacity: 1;
}
.label-cont input {
width: auto;
border: 0;
outline: 0;
background-color: transparent;
}
div.pub-l-label{
margin-top:30px;
}
ul.label-cont-show {
padding: 0;
margin: 0;
width: 100%;
line-height: 1.5715;
display: flex;
flex-direction: row;
align-items: center;
}
ul.label-cont-show li {
background: rgba(255, 255, 255, 0.04);
border: 1px solid #dcdfe6;
border-radius: 2px;
margin-left: 9px;
padding: 3px 10px;
display: flex;
align-items: center;
}
ul.label-cont-show li:hover {
background: #097EFF;
cursor: pointer;
transition: all 0.2s;
color:#fff;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<body>
<div id="app">
<label class="label-cont" :class="{'label-error': myTags.length < 1}">
<div class="label-cont-outer">
<div class="label-cont-item" v-for="item in myTags">{{item.name}}
<i class="fa fa-times-circle" aria-hidden="true" @click="removeTags(item)">
</i>
</div>
</div>
<input type="text" @change="addTags(tag)" v-model="tag" placeholder="请选择或输入标签" />
</label>
<div class="pub-l-label">
<ul class="label-cont-show">
<li v-for="item in tags" @click="addTags(item)">{{item.name}}</li>
</ul>
</div>
</div>
</body>
</html>
<script>
new Vue({
el: '#app',
data: {
tag:'',
myTags: [],
tags: [{name:'历史',flag:1},{name:'地理',flag:1},{name:'政治',flag:1},{name:'科技',flag:1}]
},
methods: {
addTags(item) {
// 1、点击下方标签增加
// 2、输入自定义标签增加
if (this.myTags.length >= 10) {
this.tag = '';
return;
}
if (typeof item === 'string') {
this.myTags.push({ name: item, flag: 0 });
let temp = {};
this.myTags = this.myTags.reduce((cur, next) => {
temp[next.name] ? "" : temp[next.name] = true && cur.push(next);
return cur;
}, []);//对象去重
this.tag = '';
} else {
let index = this.tags.findIndex(v => v.name === item.name);
if (index > -1) {
this.myTags.push(item);
this.tags.splice(index, 1);
}
}
},
removeTags(item) {
if (item.flag === 1) {
this.tags.push(item);
}
let index = this.myTags.findIndex(v => v.name === item.name);
if (index > -1)
this.myTags.splice(index, 1);
}
}
})
</script>
网友评论