Ver Mensaje Individual
  #48 (permalink)  
Antiguo 05/08/2016, 06:03
Avatar de IsaBelM
IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: [APORTE] efectos sin jquery

Tags Input

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <html dir="ltr" lang="es-es">
  3. <meta charset="utf-8">
  4. <meta name="language" content="es">
  5. <meta name="distribution" content="global">
  6. <meta name="rating" content="General">
  7. <meta name="viewport" content="user-scalable=yes, width=device-width, initial-scale=1">
  8. *, *:before, *:after {
  9.     margin: 0;
  10.     padding: 0;
  11.     border: 0;
  12.     position: relative;
  13.     -webkit-tap-highlight-color: rgba(0,0,0,0);
  14.     -webkit-box-sizing: border-box;
  15.     -moz-box-sizing: border-box;
  16.     box-sizing: border-box;
  17. }
  18.  
  19.  
  20.  
  21. html {
  22.     width: 100%;
  23.     height: 100%;
  24.     -ms-touch-action: none;
  25. }
  26.  
  27.  
  28. body {
  29.     font-family: 'Open Sans', Helvetica, Arial, sans-serif;
  30.     -webkit-touch-callout: none;
  31.     -webkit-text-size-adjust: none;
  32.     -webkit-user-select: none;  
  33. }
  34.  
  35.  
  36. ul.cont-input {
  37.     width: 25rem;
  38.     overflow: auto;
  39.     list-style-type: none;
  40.     border: 1px solid rgb(193, 184, 184);
  41.     margin: 0 auto;
  42. }
  43.  
  44.  
  45. ul.cont-input > li {
  46.     float: left;
  47.     padding: .2rem .2rem .2rem 0;
  48. }
  49.  
  50.  
  51. ul.cont-input > li > span {
  52.     background-color: rgb(180, 212, 234);
  53.     border: 1px solid rgb(15, 216, 186);
  54.     border-radius: 3px;
  55.     display: inline-block;
  56.     padding: .1rem .9rem .1rem .1rem;
  57.     margin: 0 .1rem;
  58. }
  59.  
  60.  
  61. ul.cont-input > li > span > i {
  62.     font-size: .9rem;
  63.     position: absolute;
  64.     top: 0;
  65.     right: 3px;
  66.     display: inline;
  67.     font-family: arial, sans-serif;
  68.     line-height: .9rem;
  69.     color: rgb(255, 0, 0);
  70.     cursor: pointer;
  71. }
  72.  
  73.  
  74. input.inputTags {
  75.     min-width: 3rem;
  76.     -moz-box-sizing: border-box;
  77.     -webkit-box-sizing: border-box;
  78.     box-sizing: border-box;
  79.     -moz-box-shadow: none;
  80.     -webkit-box-shadow: none;
  81.     box-shadow: none;
  82.     background-color: inherit;
  83.     outline: none;
  84. }
  85. var inputTagging = {
  86.  
  87.     params : [],
  88.     exp : new RegExp('\\s'), //carácter con el que se creará un nuevo tag
  89.  
  90.     escritura: function() {
  91.  
  92.         Array.prototype.forEach.call(document.querySelectorAll('.inputTags'), function(obj, i) {
  93.  
  94.             inputTagging.params.push(new Array());
  95.  
  96.             obj.addEventListener('keyup', function() {
  97.  
  98.                 if (inputTagging.exp.test(this.value)) inputTagging.nuevoTag(this.value, this, i);
  99.  
  100.             }, false);
  101.  
  102.  
  103.             document.querySelectorAll('.cont-input')[i].addEventListener('click', function() {
  104.  
  105.                 obj.focus();
  106.  
  107.             }, false);
  108.         })
  109.  
  110.     },
  111.  
  112.  
  113.     nuevoTag: function(valTag, control, indice) {
  114.  
  115.         var tagLimpio = valTag.replace(this.exp, ''),  
  116.         lis = document.createElement('li'),
  117.         sp = document.createElement('span'),
  118.         cierre = document.createElement('i'),
  119.         LIcontrol = document.querySelectorAll('.r-input')[indice],
  120.         controlpadre = LIcontrol.parentNode;
  121.  
  122.         sp.textContent = tagLimpio;
  123.         cierre.textContent = 'x';
  124.  
  125.         cierre.addEventListener('click', function() {
  126.             var li = this.parentNode.parentNode,
  127.                 ul = li.parentNode,
  128.                 listaNodos = Array.prototype.slice.call(ul.children),
  129.                 indexLi = listaNodos.indexOf(li);
  130.             ul.removeChild(li);
  131.             control.focus();
  132.             inputTagging.params[indice].splice(indexLi, 1);
  133.             control.nextSibling.value = inputTagging.params[indice];
  134.         }, false);
  135.  
  136.         sp.appendChild(cierre);
  137.         lis.appendChild(sp);
  138.  
  139.         controlpadre.insertBefore(lis, LIcontrol);
  140.         control.value = '';
  141.         control.focus();
  142.  
  143.         this.params[indice].push(tagLimpio);
  144.         control.nextSibling.value = this.params[indice];
  145.     }
  146. }
  147.  
  148.  
  149. document.addEventListener('DOMContentLoaded', function() {
  150.  
  151.     inputTagging.escritura();
  152.  
  153. }, false);
  154. </head>
  155.  
  156.     <ul class="cont-input">
  157.         <li class="r-input">
  158.             <input type="text" class="inputTags" placeholder="tags con barra espaciadora"><input type="hidden" name="elinput[]">
  159.         </li>
  160.     </ul>
  161.  
  162. <br><br>
  163.  
  164.     <ul class="cont-input">
  165.         <li class="r-input">
  166.             <input type="text" class="inputTags" placeholder="tags con barra espaciadora"><input type="hidden" name="elinput[]">
  167.         </li>
  168.     </ul>
  169.  
  170. </body>
  171. </html>

Observaciones.-
  • Para cambiar el carácter con el que se crearán los tag, editar esta línea - exp : new RegExp('\\s') -con el carácter deseado. Por defecto es la barra espaciadora
  • El campo que se ha de recoger en el envío, es el campo hidden --> elinput[]

En caso de querer de reemplazar tanto por tags como por imágenes, añade este función
Código HTML:
Ver original
  1. imagenes: function(val) {
  2.  
  3.         return {
  4.  
  5.             'concepto1' : 'urlimg.jpg',
  6.             'concepto2' : 'urlimg.jpg',
  7.             'concepto3' : 'urlimg.jpg'
  8.  
  9.         } [val] || null;
  10.  
  11.     },
y reemplaza la línea 127
Código HTML:
Ver original
  1. sp.textContent = tagLimpio;
por
Código HTML:
Ver original
  1. var img = this.imagenes(tagLimpio);
  2.  
  3.         if (typeof img == 'object') {
  4.  
  5.             sp.textContent = tagLimpio;
  6.  
  7.         } else {
  8.  
  9.             sp.innerHTML = '<image src="' + img + '">';
  10.         }

DEMO
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}

Última edición por IsaBelM; 10/08/2016 a las 14:33