切换按钮与复选框的位置。

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

Toggle the position of a button with checkbox

问题

这个问题出现在尝试在网站中创建主题切换方案时,当我点击复选框时,X轴位置的翻译移动不会发生。

.alt-theme {
  height: 80vh;
  position: absolute;
  right: 10em;
}

.checkbox {
  opacity: 1;
  position: absolute;
}

.checkbox-label {
  background-color: #111;
  border-radius: 50px;
  cursor: pointer;
  
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  height: 26px;
  width: 50px;
  
  transform: scale(1.5);
}

.checkbox-label .ball {
  background-color: #fff;
  border-radius: 50%;
  position: absolute;
  top: 2px;
  left: 2px;
  height: 22px;
  width: 22px;
  transform: translateX(0);
  transition: transform 0.2s linear;
}
<div class="alt-theme">
  <label for="checkbox" class="checkbox-label">
    <i class="fa-solid fas fa-moon" style="color: #000; background: none;"></i>
    <i class="fa-solid fas fa-sun" style="color: #000; background: none;"></i>
    <div class="ball"></div>
  </label>
  
  <input type="checkbox" class="checkbox" />
</div>

这个复选框标签内的白色球的移动。

英文:

This problem appears while trying to make a theme switcher scheme within a website when I click on the checkbox the translation movement in the X position doesn´t happen.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.alt-theme {
  height: 80vh;
  position: absolute;
  right: 10em;
}

.checkbox {
  opacity: 1;
  position: absolute;
}

.checkbox-label {
  background-color: #111;
  border-radius: 50px;
  cursor: pointer;
  
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  height: 26px;
  width: 50px;
  
  transform: scale(1.5);
}

.checkbox-label .ball {
  background-color: #fff;
  border-radius: 50%;
  position: absolute;
  top: 2px;
  left: 2px;
  height: 22px;
  width: 22px;
  transform: translateX(0);
  transition: transform 0.2s linear;
}

<!-- language: lang-html -->

&lt;div class=&quot;alt-theme&quot;&gt;
  &lt;label for=&quot;checkbox&quot; class=&quot;checkbox-label&quot;&gt;
    &lt;i class=&quot;fa-solid fas fa-moon&quot; style=&quot;color: #000; background: none;&quot; /&gt;
    &lt;i class=&quot;fa-solid fas fa-sun&quot; style=&quot;color: #000; background: none;&quot; /&gt;
    &lt;div class=&quot;ball&quot; /&gt;
  &lt;/label&gt;
  
  &lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

The movement of this white ball inside the label of the checkbox.

切换按钮与复选框的位置。

答案1

得分: 1

以下是您要翻译的内容:

您采取的方法并不差,但还有改进的空间。请仔细阅读添加到代码中的注释,以更好地理解底层原理。

解决方案

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-css -->
/* 仅设置开关元素的位置 */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* 隐藏输入元素 --&gt; 这样你就看不到复选框的方框了 */
.switch input {
  display: none;
}

/* 设置背景 */
.slider {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #555;
  transition: 0.4s;
  cursor: pointer;
  border-radius: 34px;
}

/* 设置圆圈 */
.slider:before {
  position: absolute;
  width: 26px;
  height: 26px;
  left: 4px;
  bottom: 4px;
  background-color: #fff;
  transition: 0.4s;
  border-radius: 34px;

  /* 当复选框的值为假时设置图标 */
  font-family: "Font Awesome 5 Free";
  content: "\f186"; /* Font Awesome fas-moon */
  font-weight: 900;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 复选框的值为真时设置背景 */
.switch input:checked + .slider {
  background-color: #ccc;
}

/* 复选框的值为真时设置图标 */
.switch input:checked + .slider:before {
  content: "\f185"; /* Font Awesome fas-sun */
  transform: translateX(26px);
}

<!-- 语言:lang-html -->

<!-- 导入FontAwesome -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<!-- 开关输入 -->
<label class="switch" for="checkbox">
  <!-- 存储真/假值 -->
  <input type="checkbox" id="checkbox" />
  <!-- 显示带有月亮/太阳图标的圆圈 -->
  <div class="slider round"></div>
</label>

<!-- 结束代码片段 -->

更多信息

英文:

The approach you've taken is not bad, but there is room for improvement. Please read through the comments added to the code to better understand the underlying principles.

Solution

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

/* Just sets position of the switch element */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide input element --&gt; You won&#39;t see the checkbox square like this */
.switch input {
  display: none;
}

/* Sets background */
.slider {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #555;
  transition: 0.4s;
  cursor: pointer;
  border-radius: 34px;
}

/* Sets circle */
.slider:before {
  position: absolute;  
  width: 26px;
  height: 26px;
  left: 4px;
  bottom: 4px;
  background-color: #fff;
  transition: 0.4s;
  border-radius: 34px;
  
  /* Sets icon when value of checkbox is false */
  font-family: &quot;Font Awesome 5 Free&quot;;
  content: &quot;\f186&quot;; /* Font Awesome fas-moon */
  font-weight: 900;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Sets background when value of checkbox is TRUE */
.switch input:checked + .slider {
  background-color: #ccc;
}

/* Sets icon when value of checkbox is TRUE */
.switch input:checked + .slider:before {
  content: &quot;\f185&quot;; /* Font Awesome fas-sun */
  transform: translateX(26px);
}

<!-- language: lang-html -->

&lt;!-- Import FontAwesome --&gt;
&lt;link href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css&quot; rel=&quot;stylesheet&quot;&gt;

&lt;!-- Switch input --&gt;
&lt;label class=&quot;switch&quot; for=&quot;checkbox&quot;&gt;
  &lt;!-- Store true/false value --&gt;
  &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; /&gt;
  &lt;!-- Show circle with moon/sun icon --&gt;
  &lt;div class=&quot;slider round&quot; /&gt;
&lt;/label&gt;

<!-- end snippet -->

More information

huangapple
  • 本文由 发表于 2023年6月8日 22:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433073.html
匿名

发表评论

匿名网友

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

确定