8 changed files with 544 additions and 133 deletions
@ -0,0 +1,86 @@ |
|||
@keyframes fariy-fadein { |
|||
0% { |
|||
opacity: 0; |
|||
} |
|||
100% { |
|||
opacity: 1; |
|||
} |
|||
} |
|||
.fairy-tag-container { |
|||
width: 100%; |
|||
height: 38px; |
|||
border: 1px solid #e6e6e6; |
|||
background-color: #ffffff; |
|||
} |
|||
.fairy-tag-container:hover { |
|||
border-color: #d2d2d2; |
|||
} |
|||
.fairy-tag-container span.fairy-tag { |
|||
float: left; |
|||
font-size: 13px; |
|||
padding: 5px 8px; |
|||
margin-right: 5px; |
|||
margin-bottom: 5px; |
|||
border-radius: 2px; |
|||
line-height: 16px; |
|||
} |
|||
.fairy-tag-container span.fairy-tag a { |
|||
font-size: 11px; |
|||
font-weight: bolder; |
|||
color: #ffffff; |
|||
text-decoration: none; |
|||
margin-left: 6px; |
|||
} |
|||
.fairy-tag-container span.fairy-tag a:hover { |
|||
cursor: pointer; |
|||
} |
|||
.fairy-tag-container span.fairy-bg-red { |
|||
background-color: #FF5722; |
|||
} |
|||
.fairy-tag-container span.fairy-bg-orange { |
|||
background-color: #FFB800; |
|||
} |
|||
.fairy-tag-container span.fairy-bg-green { |
|||
background-color: #009688; |
|||
} |
|||
.fairy-tag-container span.fairy-bg-cyan { |
|||
background-color: #2F4056; |
|||
} |
|||
.fairy-tag-container span.fairy-bg-blue { |
|||
background-color: #1E9FFF; |
|||
} |
|||
.fairy-tag-container span.fairy-bg-black { |
|||
background-color: #393D49; |
|||
} |
|||
.fairy-tag-container span.fairy-bg-red, |
|||
.fairy-tag-container span.fairy-bg-orange, |
|||
.fairy-tag-container span.fairy-bg-green, |
|||
.fairy-tag-container span.fairy-bg-cyan, |
|||
.fairy-tag-container span.fairy-bg-blue, |
|||
.fairy-tag-container span.fairy-bg-black { |
|||
color: #ffffff; |
|||
} |
|||
.fairy-tag-container .fairy-anim-fadein { |
|||
animation: fariy-fadein 0.3s both; |
|||
} |
|||
.fairy-tag-container .fairy-tag-input[type='text'] { |
|||
width: 80px; |
|||
font-size: 13px; |
|||
padding: 6px; |
|||
background: transparent; |
|||
border: 0 none; |
|||
outline: 0; |
|||
} |
|||
.fairy-tag-container .fairy-tag-input[type='text']:focus::-webkit-input-placeholder { |
|||
color: transparent; |
|||
} |
|||
.fairy-tag-container .fairy-tag-input[type='text']:focus:-moz-placeholder { |
|||
color: transparent; |
|||
} |
|||
.fairy-tag-container .fairy-tag-input[type='text']:focus:-moz-placeholder { |
|||
color: transparent; |
|||
} |
|||
.fairy-tag-container .fairy-tag-input[type='text']:focus:-ms-input-placeholder { |
|||
color: transparent; |
|||
} |
|||
/*# sourceMappingURL=inputTag.css.map */ |
|||
@ -0,0 +1,104 @@ |
|||
@keyframes fariy-fadein { |
|||
0% { |
|||
opacity: 0 |
|||
} |
|||
|
|||
100% { |
|||
opacity: 1 |
|||
} |
|||
} |
|||
|
|||
.fairy-tag-container { |
|||
width: auto; |
|||
min-height: 100px; |
|||
padding: 5px; |
|||
border: 1px solid #e6e6e6; |
|||
background-color: #ffffff; |
|||
|
|||
&:hover { |
|||
border-color: #d2d2d2; |
|||
} |
|||
|
|||
span { |
|||
&.fairy-tag { |
|||
float: left; |
|||
font-size: 13px; |
|||
padding: 5px 8px; |
|||
margin-right: 5px; |
|||
margin-bottom: 5px; |
|||
border-radius: 2px; |
|||
line-height: 16px; |
|||
|
|||
a { |
|||
font-size: 11px; |
|||
font-weight: bolder; |
|||
color: #ffffff; |
|||
text-decoration: none; |
|||
margin-left: 6px; |
|||
|
|||
&:hover { |
|||
cursor: pointer; |
|||
} |
|||
} |
|||
} |
|||
|
|||
&.fairy-bg-red { |
|||
background-color: #FF5722; |
|||
} |
|||
|
|||
&.fairy-bg-orange { |
|||
background-color: #FFB800; |
|||
} |
|||
|
|||
&.fairy-bg-green { |
|||
background-color: #009688; |
|||
} |
|||
|
|||
&.fairy-bg-cyan { |
|||
background-color: #2F4056; |
|||
} |
|||
|
|||
&.fairy-bg-blue { |
|||
background-color: #1E9FFF; |
|||
} |
|||
|
|||
&.fairy-bg-black { |
|||
background-color: #393D49; |
|||
} |
|||
|
|||
&.fairy-bg-red, &.fairy-bg-orange, &.fairy-bg-green, &.fairy-bg-cyan, &.fairy-bg-blue, &.fairy-bg-black { |
|||
color: #ffffff; |
|||
} |
|||
} |
|||
|
|||
.fairy-anim-fadein { |
|||
animation: fariy-fadein .3s both; |
|||
} |
|||
|
|||
.fairy-tag-input[type='text'] { |
|||
width: 80px; |
|||
font-size: 13px; |
|||
padding: 6px; |
|||
background: transparent; |
|||
border: 0 none; |
|||
outline: 0; |
|||
|
|||
&:focus { |
|||
&::-webkit-input-placeholder { |
|||
color: transparent; |
|||
} |
|||
|
|||
&:-moz-placeholder { |
|||
color: transparent; |
|||
} |
|||
|
|||
&:-moz-placeholder { |
|||
color: transparent; |
|||
} |
|||
|
|||
&:-ms-input-placeholder { |
|||
color: transparent; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,152 @@ |
|||
/* |
|||
* Name: inputTag |
|||
* Author: cshaptx4869 |
|||
* Project: https://github.com/cshaptx4869/inputTag
|
|||
*/ |
|||
(function (define) { |
|||
define(['jquery'], function ($) { |
|||
"use strict"; |
|||
|
|||
class InputTag { |
|||
|
|||
options = { |
|||
elem: '.fairy-tag-input', |
|||
theme: ['fairy-bg-red', 'fairy-bg-orange', 'fairy-bg-green', 'fairy-bg-cyan', 'fairy-bg-blue', 'fairy-bg-black'], |
|||
data: [], |
|||
removeKeyNum: 8, |
|||
createKeyNum: 13, |
|||
permanentData: [], |
|||
}; |
|||
|
|||
get elem() { |
|||
return $(this.options.elem); |
|||
} |
|||
|
|||
get copyData() { |
|||
return [...this.options.data]; |
|||
} |
|||
|
|||
constructor(options) { |
|||
this.render(options); |
|||
} |
|||
|
|||
render(options) { |
|||
this.init(options); |
|||
this.listen(); |
|||
} |
|||
|
|||
init(options) { |
|||
var spans = '', that = this; |
|||
this.options = $.extend(this.options, options); |
|||
!this.elem.attr('placeholder') && this.elem.attr('placeholder', '添加标签'); |
|||
$.each(this.options.data, function (index, item) { |
|||
spans += that.spanHtml(item); |
|||
}); |
|||
this.elem.before(spans); |
|||
} |
|||
|
|||
listen() { |
|||
var that = this; |
|||
|
|||
this.elem.parent().on('click', 'a', function () { |
|||
that.removeItem($(this).parent('span')); |
|||
}); |
|||
|
|||
this.elem.parent().on('click', function () { |
|||
that.elem.focus(); |
|||
}); |
|||
|
|||
this.elem.keydown(function (event) { |
|||
var keyNum = (event.keyCode ? event.keyCode : event.which); |
|||
if (keyNum === that.options.removeKeyNum) { |
|||
if (!that.elem.val().trim()) { |
|||
var closeItems = that.elem.parent().find('a'); |
|||
if (closeItems.length) { |
|||
that.removeItem($(closeItems[closeItems.length - 1]).parent('span')); |
|||
event.preventDefault(); |
|||
} |
|||
} |
|||
} |
|||
}); |
|||
|
|||
this.elem.focusout(function(event){ |
|||
that.createItem(); |
|||
event.preventDefault(); |
|||
}) |
|||
} |
|||
|
|||
createItem() { |
|||
var value = this.elem.val().trim(); |
|||
|
|||
if (this.options.beforeCreate && typeof this.options.beforeCreate === 'function') { |
|||
var modifiedValue = this.options.beforeCreate(this.copyData, value); |
|||
if (typeof modifiedValue == 'string' && modifiedValue) { |
|||
value = modifiedValue; |
|||
} else { |
|||
value = ''; |
|||
} |
|||
} |
|||
|
|||
if (value) { |
|||
if (!this.options.data.includes(value)) { |
|||
this.options.data.push(value); |
|||
this.elem.before(this.spanHtml(value)); |
|||
this.onChange(value, 'create'); |
|||
} |
|||
} |
|||
|
|||
this.elem.val(''); |
|||
} |
|||
|
|||
removeItem(target) { |
|||
var that = this; |
|||
var closeSpan = target.remove(), |
|||
closeSpanText = $(closeSpan).children('span').text(); |
|||
var closeSpanTextIndex = $.inArray(closeSpanText, that.options.data); |
|||
var value = that.options.data.splice($.inArray(closeSpanText, that.options.data), 1); |
|||
value.length === 1 && that.onChange(value[0], 'remove',closeSpanTextIndex); |
|||
} |
|||
|
|||
randomColor() { |
|||
return this.options.theme[Math.floor(Math.random() * this.options.theme.length)]; |
|||
} |
|||
|
|||
spanHtml(value) { |
|||
return '<span class="fairy-tag fairy-anim-fadein ' + this.randomColor() + '">' + |
|||
'<span>' + value + '</span>' + |
|||
(this.options.permanentData.includes(value) ? '' : '<a href="#" title="删除标签">×</a>') + |
|||
'</span>'; |
|||
} |
|||
|
|||
onChange(value, type,index) { |
|||
this.options.onChange && typeof this.options.onChange === 'function' && this.options.onChange(this.copyData, value, type,index); |
|||
} |
|||
|
|||
getData() { |
|||
return this.copyData; |
|||
} |
|||
|
|||
clearData() { |
|||
this.options.data = []; |
|||
this.elem.prevAll('span.fairy-tag').remove(); |
|||
} |
|||
} |
|||
|
|||
return { |
|||
render(options) { |
|||
return new InputTag(options); |
|||
} |
|||
} |
|||
}); |
|||
}(typeof define === 'function' && define.amd ? define : function (deps, factory) { |
|||
var MOD_NAME = 'inputTag'; |
|||
if (typeof module !== 'undefined' && module.exports) { //Node
|
|||
module.exports = factory(require('jquery')); |
|||
} else if (window.layui && layui.define) { |
|||
layui.define('jquery', function (exports) { //layui加载
|
|||
exports(MOD_NAME, factory(layui.jquery)); |
|||
}); |
|||
} else { |
|||
window[MOD_NAME] = factory(window.jQuery); |
|||
} |
|||
})); |
|||
Loading…
Reference in new issue