如何在禁用时在切换开关上显示交叉标记

huangapple go评论66阅读模式
英文:

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">&#10003;</span>
    <span class="cross">&#10005;</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: &quot;&quot;;
  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 -->

&lt;div class=&quot;toggle-switch-container&quot;&gt;
  &lt;label class=&quot;switch&quot;&gt;
			&lt;input type=&quot;checkbox&quot; checked&gt;
			&lt;span class=&quot;slider round&quot;&gt;&lt;/span&gt;
			&lt;span class=&quot;tick&quot;&gt;&amp;#10003;&lt;/span&gt;
            &lt;span class=&quot;cross&quot;&gt;&amp;#10005;&lt;/span&gt;
		&lt;/label&gt;
&lt;/div&gt;

<!-- 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;
}

huangapple
  • 本文由 发表于 2023年4月17日 02:48:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定