Я пытаюсь выяснить, как прочитать состояние / значение ползункового переключателя, который будет реагировать при перемещении - PullRequest
0 голосов
/ 30 марта 2020

Я создал скользящий переключатель, который будет скрывать или отображать части большой формы. HTML имеет следующий код:

<fieldset class="group">
            <legend>
                <h1>Form Type</h1>
            </legend>
            <label class="switch switch-left-right">
                <input class="switch-input" type="checkbox" id="formSwitch">
                <span  class="switch-label" data-on="Progress" data-off="Diagnostic"></span>
                <span class="switch-handle"></span>
            </label>
        </fieldset>

* * * * * * * * * * * * * * * * * * * * * * * * * * * CSS * * * за 1006 * *

Я пытаюсь настроить его так, чтобы при переключении находится на Диагностике c, она будет отображать группу полей формы, и если она находится в процессе выполнения, она будет скрывать те же поля формы. Я пробовал слушателей событий и слушателей onclick и, похоже, ничего не получалось Мне просто нужно понять, что «если он находится в одной позиции, сделай это» и «если он находится в другой позиции, сделай это» Это не похоже, что это будет проблемой, но это доказывает!

1 Ответ

0 голосов
/ 30 марта 2020

Вам просто нужно послушать «поменять» на флажок. Затем вы можете использовать свойство element.checked для установки классов или встроенного стиля, как я делаю в моем примере ниже

document.querySelector("#formSwitch").addEventListener("change", e => {
  const hidden = document.querySelector("#hidden")
  hidden.style.display = e.target.checked ? "none" : "block";
})
.switch {
    position: relative;
    display: block;
    vertical-align: top;
    width: 100px;
    height: 30px;
    padding: 3px;
    margin: 0 10px 10px 0;
    background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px);
    background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px);
    border-radius: 18px;
    box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
    cursor: pointer;
    box-sizing:content-box;
}
.switch-input {
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
    box-sizing:content-box;
}
.switch-label {
    position: relative;
    display: block;
    height: inherit;
    font-size: 10px;
    text-transform: uppercase;
    background: #eceeef;
    border-radius: inherit;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
    box-sizing:content-box;
}
.switch-label:before, .switch-label:after {
    position: absolute;
    top: 50%;
    margin-top: -.5em;
    line-height: 1;
    -webkit-transition: inherit;
    -moz-transition: inherit;
    -o-transition: inherit;
    transition: inherit;
    box-sizing:content-box;
}
.switch-label:before {
    content: attr(data-off);
    right: 11px;
    color: #FFFFFF;
    text-shadow: 0 1px rgba(255, 255, 255, 0.5);
}
.switch-label:after {
    content: attr(data-on);
    left: 11px;
    color: #FFFFFF;
    text-shadow: 0 1px rgba(0, 0, 0, 0.2);
    opacity: 0;
}
.switch-input:checked ~ .switch-label {
    background: #E1B42B;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
}
.switch-input:checked ~ .switch-label:before {
    opacity: 0;
}
.switch-input:checked ~ .switch-label:after {
    opacity: 1;
}
.switch-handle {
    position: absolute;
    top: 4px;
    left: 4px;
    width: 28px;
    height: 28px;
    background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0);
    background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0);
    border-radius: 100%;
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
}
.switch-handle:before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -6px 0 0 -6px;
    width: 12px;
    height: 12px;
    background: linear-gradient(to bottom, #eeeeee, #FFFFFF);
    background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF);
    border-radius: 6px;
    box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
}
.switch-input:checked ~ .switch-handle {
    left: 74px;
    box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
}

/* Transition
========================== */
.switch-label, .switch-handle {
    transition: All 0.3s ease;
    -webkit-transition: All 0.3s ease;
    -moz-transition: All 0.3s ease;
    -o-transition: All 0.3s ease;
}
.switch-left-right .switch-label {
    overflow: hidden;
}
.switch-left-right .switch-label:before, .switch-left-right .switch-label:after {
    width: 20px;
    height: 20px;
    top: 4px;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 11px 0 0 0;
    text-indent: -45px;
    border-radius: 20px;
    box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.2), inset 0 0 3px rgba(0, 0, 0, 0.1);
}
.switch-left-right .switch-label:before {
    background: #FF7F50;
    text-align: left;
    padding-left: 80px;
}
.switch-left-right .switch-label:after {
    text-align: left;
    text-indent: 9px;
    background: #FF7F50;
    left: -100px;
    opacity: 1;
    width: 100%;
}
.switch-left-right .switch-input:checked ~ .switch-label:before {
    opacity: 1;
    left: 90px;
}
.switch-left-right .switch-input:checked ~ .switch-label:after {
    left: 0;
}
.switch-left-right .switch-input:checked ~ .switch-label {
    background: inherit;
}
<fieldset class="group">
            <legend>
                <h1>Form Type</h1>
            </legend>
            <label class="switch switch-left-right">
                <input class="switch-input" type="checkbox" id="formSwitch">
                <span  class="switch-label" data-on="Progress" data-off="Diagnostic"></span>
                <span class="switch-handle"></span>
            </label>
        </fieldset>
        
        
        <div id="hidden">
          stuff that needs to be shown / hidden
        </div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...