英文:
How to display cross mark in the toggle switch when disabled
问题
我正在使用一个切换开关,我想在启用时显示一个勾号,而在禁用时显示一个X号。
我能够显示勾号,但无法显示X号。请帮助我找出问题所在。
.switch {
  /* ... */
}
/* ... */
.tick,
.cross {
  /* ... */
}
/* ... */
<div class="toggle-switch-container">
  <label class="switch">
    <input type="checkbox" checked>
    <span class="slider round"></span>
    <span class="tick">✓</span>
    <span class="cross">✕</span>
  </label>
</div>
英文:
I am using a toggle switch and I want to show a tick mark when enabled and an x-mark when disabled.
and am able to show the tick mark but unable to display the x-mark. please help me where I am doing wrong.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}
.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}
input:checked+.slider {
  background-color: #2196F3;
}
input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}
input:checked+.slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}
.slider.round:before {
  border-radius: 50%;
}
.toggle-switch-container {
  display: flex;
  align-items: center;
}
/* Style the toggle switch label */
.toggle-switch-label {
  margin-left: 10px;
  font-size: 20px;
}
/* Tick and Cross marks */
.tick,
.cross {
  position: absolute;
  top: 2px;
  font-size: 22px;
  color: white;
  transition: .4s;
}
.tick {
  left: 35px;
  opacity: 0;
}
input:checked+.slider+.tick {
  opacity: 1;
  color: green;
}
.cross {
  right: 34px;
  opacity: 0;
}
input:not(:checked)+.slider+.cross {
  color: red;
  opacity: 1;
}
<!-- language: lang-html -->
<div class="toggle-switch-container">
  <label class="switch">
			<input type="checkbox" checked>
			<span class="slider round"></span>
			<span class="tick">&#10003;</span>
            <span class="cross">&#10005;</span>
		</label>
</div>
<!-- end snippet -->
when enable tick mark showing properly like this
and when disabled i want x-mark(crosser) like tick mark
thanks
答案1
得分: 1
这是因为您正在使用相邻兄弟选择器 + 来选择 .cross,但不像 .tick,它不是 .slider 的相邻兄弟。尝试使用一般兄弟选择器 ~,像这样:
英文:
It is because you are using the adjacent sibling selector + to select .cross but unlike .tick it is not the adjacent sibling to .slider. Try using general sibling selector ~ like this:
input:not(:checked) ~ .cross {
  color: red;
  opacity: 1;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论