изменить стиль других меток, когда установлен флажок - PullRequest
0 голосов
/ 27 мая 2020

У меня проблема с установленным флажком. Я пытаюсь найти решение, когда мой текущий флажок установлен, чтобы изменить флажок непрозрачности другие. это возможно с CSS? Я сделал это с вводом CSS и меткой. Вот мой код.

ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

li {
  margin-bottom: 10px;
  display: block;
}

.styled-checkbox {
  position: absolute;
  opacity: 0;
}
.styled-checkbox:checked + label {
  opacity: 1;
}
.styled-checkbox:checked + label label {
  opacity: 0.5;
}
.styled-checkbox + label {
  position: relative;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  font-size: 0.875rem;
}
.styled-checkbox + label:before {
  content: '';
  margin-right: 10px;
  display: inline-block;
  width: 14px;
  height: 14px;
  vertical-align: text-top;
  border: 1px solid #000;
}
.styled-checkbox:checked + label:after {
  content: '';
  position: absolute;
  left: 1px;
  top: 7px;
  background: white;
  width: 2px;
  height: 2px;
  box-shadow: 2px 0 0 #000, 4px 0 0 #000, 4px -2px 0 #000, 4px -4px 0 #000, 4px -6px 0 #000, 4px -8px 0 #000;
  transform: rotate(45deg);
  transition: all 0.3s ease-out;
}
<ul>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-1" />
    <label for="styled-checkbox-1">Test one</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-2" />
    <label for="styled-checkbox-2">Test Two</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-3" />
    <label for="styled-checkbox-3">Test Three</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-4" />
    <label for="styled-checkbox-4">Test Four</label>
  </li>
</ul>

`

цель желания примерно такая

enter image description here

Надеюсь, вы понимаете проблему. Заранее спасибо

1 Ответ

1 голос
/ 27 мая 2020

Просто удалите это объявление: .styled-checkbox:checked + label label и добавьте еще одно .styled-checkbox.inactive + label, чтобы установить непрозрачность «неактивных» флажков.

  • После загрузки DOM получите все флажки и добавьте прослушиватель кликов
  • Если флажок не установлен, удалите «неактивный» класс из всех
  • Если установлен хотя бы один флажок, добавьте «неактивный» класс ко всем снятым флажкам

window.addEventListener('load', function() {
    let checks = document.querySelectorAll('.styled-checkbox');
    checks.forEach(function(check) {
        check.addEventListener('click', chkStyles);
    });
    function chkStyles() {
        // Verify if there's at least one checkbox checked
        let checked = document.querySelectorAll('.styled-checkbox:checked');
        if(checked.length == 0) {
            // No checked checkbox, remove all inactive
            checks.forEach(function(check) { check.classList.remove('inactive'); });
        } else {
            // At least 1 is checked
            checks.forEach(function(check) {
                if(check.checked) {
                    check.classList.remove('inactive');
                } else {
                    check.classList.add('inactive');
                }
            });
        }
    }
});
ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

li {
  margin-bottom: 10px;
  display: block;
}

.styled-checkbox {
  position: absolute;
  opacity: 0;
}
.styled-checkbox:checked + label {
  opacity: 1;
}
.styled-checkbox.inactive + label {
  opacity: 0.5;
}
.styled-checkbox + label {
  position: relative;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  font-size: 0.875rem;
}
.styled-checkbox + label:before {
  content: '';
  margin-right: 10px;
  display: inline-block;
  width: 14px;
  height: 14px;
  vertical-align: text-top;
  border: 1px solid #000;
}
.styled-checkbox:checked + label:after {
  content: '';
  position: absolute;
  left: 1px;
  top: 7px;
  background: white;
  width: 2px;
  height: 2px;
  box-shadow: 2px 0 0 #000, 4px 0 0 #000, 4px -2px 0 #000, 4px -4px 0 #000, 4px -6px 0 #000, 4px -8px 0 #000;
  transform: rotate(45deg);
  transition: all 0.3s ease-out;
}
<ul>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-1" />
    <label for="styled-checkbox-1">Test one</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-2" />
    <label for="styled-checkbox-2">Test Two</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-3" />
    <label for="styled-checkbox-3">Test Three</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-4" />
    <label for="styled-checkbox-4">Test Four</label>
  </li>
</ul>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...