Set word within toogle

0

Colleagues,

I do not know if my title was clear, but here on the site I got a toogle, but I need to put the word Yes and No inside it. The word Yes I was able to put, but it was almost out of toogle and the word No is not working. I do not know if I was clear in my doubt, so I'll put the photo and the code.

Thecodefollowsbelow:

CSS

.onoffinput.toggle{display:none;}.onoffinput.toggle+label{content:"Não";
    display: inline-block;
    position: relative;
    box-shadow: inset 0 0 0px 1px #d5d5d5;
    height: 30px;
    width: 70px;
    background: #DC143C;
    border-radius: 30px;    
}

.onoff input.toggle + label:before {
    content: "";
    display: block;
    height: 30px;
    border-radius: 30px;
    background: rgba(19, 191, 17, 0);
    transition: 0.1s ease-in-out;
}

.onoff input.toggle + label:after {
    content: "";
    position: absolute;
    height: 30px;
    width: 30px;
    top: 0;
    left: 0px;
    border-radius: 30px;
    background: #fff;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2), 0 2px 4px rgba(0, 0, 0, 0.2);
    transition: 0.1s ease-in-out;
}

.onoff input.toggle:checked + label:before {
    width: 70px;
    content: 'Sim';
    color: #FFF;
    font-family: 'Arial';
    background: #13bf11;
}

.onoff input.toggle:checked + label:after {
    left: 40px;    
    box-shadow: inset 0 0 0 1px #13bf11, 0 2px 4px rgba(0, 0, 0, 0.2);
}

HTML

<div class="col-lg-12"> 
    <div class="form-group">
        <div class="onoff">                 
            <input type="checkbox" class="toggle" id="onoff1">
            <label for="onoff1"></label>                        
        </div>                  
        <!--<span id="estado"></span> -->
        <input type="hidden" name="Ativo" id="campo" value="Não">                   
    </div>
</div>

JQuery

//<![CDATA[
window.onload=function(){

var onoff = document.getElementById('onoff1');
var estado = document.getElementById('estado');

onoff.addEventListener('change', function() {

    //estado.innerHTML = this.checked ? 'Sim' : 'Não';
    estado = this.checked ? 'Sim' : 'Não';
    // alert(estado);
    // var campo = document.getElementById("campo").value = estado.innerHTML;
    var campo = document.getElementById("campo").value = estado;
     //onoff.value = estado.innerHTML;
     //alert(onoff);

     if(campo === 'Sim'){
         var campoImagem = document.getElementById("campoImagem");
         $(campoImagem).css("display", "block");
     }else{
         var campoImagem = document.getElementById("campoImagem");
         $(campoImagem).css("display", "none");
     }

    $.ajax({
       // url: "index.html?status="+estado.innerHTML,
       data: {        
           estado: this.checked,
           campo: campo
       }
   }).done(function(msg) {     
   //if (msg == 'failed') return el.checked = !el.checked; // caso o servidor retorne "failed" mudar o estado do botão
    //   else ("Info gravada: " + msg);
   });
});
}//]]>
    
asked by anonymous 21.02.2017 / 15:59

2 answers

1

Try the following changes in your CSS and this addition in your JS:

CSS:

/* Modificação */
.onoff.ligado input.toggle:checked + label:before {
    width: 70px;
    content: '
//MODIFICAÇÕES

document.getElementById ('onoff1').addEventListener ('click', function (ev) {
    ev.target.parentNode.classList[ ev.target.checked ? 'add' : 'remove'] ('ligado');
  }, false);
a0 Sim'; color: #FFF; font-family: 'Arial'; background: #13bf11; line-height: 1.8rem; text-align: left; } .onoff input.toggle + label:before { width: 70px; content: 'Não
/* Modificação */
.onoff.ligado input.toggle:checked + label:before {
    width: 70px;
    content: '
//MODIFICAÇÕES

document.getElementById ('onoff1').addEventListener ('click', function (ev) {
    ev.target.parentNode.classList[ ev.target.checked ? 'add' : 'remove'] ('ligado');
  }, false);
a0 Sim'; color: #FFF; font-family: 'Arial'; background: #13bf11; line-height: 1.8rem; text-align: left; } .onoff input.toggle + label:before { width: 70px; content: 'Não %pre%a0'; color: #FFF; font-family: 'Arial'; background: #DC143C; line-height: 1.8rem; text-align: right; }
a0'; color: #FFF; font-family: 'Arial'; background: #DC143C; line-height: 1.8rem; text-align: right; }

JS

%pre%     
21.02.2017 / 17:46
0

Alternatively, without using javascript to control behavior:

HTML:

<label class="toggle">
  <input type="checkbox" id="onoff2">
  <div></div>
</label>

CSS:

/* .toggle é o "container" de tudo */
.toggle {
    position: relative;
    display:  inline-block;
    width:    70px;
    height:   30px;
}

/* oculta quaisquer "input"s dentro do .toggle */
.toggle input {
    display: none;
}

/* o div é o campo, vermelho ou verde, no qual o botão corre */
.toggle > div {
    position:         absolute;
    cursor:           pointer;
    width:            70px;
    height:           30px;
    border-radius:    15px;
    background-color: #DC143C;
    box-shadow:       inset 0 0 0px 1px #d5d5d5;
    transition: 0.1s ease-in-out;
}
.toggle > input:checked + div {
    background-color: #13BF11;
}

/* o div::before é o botão branco */
.toggle > div::before {
    content:          '';
    display:          block;
    background-color: #FFF;
    width:            30px;
    height:           30px;
    border-radius:    15px;
    box-shadow:       inset 0 0 0 1px rgba(0, 0, 0, 0.2), 0 2px 4px rgba(0, 0, 0, 0.2);
    transition:       0.1s ease-in-out;
}

.toggle > input:checked + div::before {
    transform:        translateX(40px);
}

/* o div::after vai conter o rótulo propriamente dito */
.toggle > div::after {
    content:     'Não';
    line-height: 20px;
    padding:     5px;
    margin-top:  -30px;
    float:       right;
    color:       #FFF;
    transition:  0.1s ease-in-out;
}

.toggle > input:checked + div::after {
    content:     'Sim';
    float:       left;
    transition:  0.1s ease-in-out;
}

The cat's leap is margin-top: -30px; , since the div :: after will end up "on the bottom line", so it becomes clipado and invisible.

    
21.02.2017 / 21:04