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

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

Toggle the position of a button with checkbox

问题

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

  1. .alt-theme {
  2. height: 80vh;
  3. position: absolute;
  4. right: 10em;
  5. }
  6. .checkbox {
  7. opacity: 1;
  8. position: absolute;
  9. }
  10. .checkbox-label {
  11. background-color: #111;
  12. border-radius: 50px;
  13. cursor: pointer;
  14. display: flex;
  15. align-items: center;
  16. justify-content: space-between;
  17. position: relative;
  18. height: 26px;
  19. width: 50px;
  20. transform: scale(1.5);
  21. }
  22. .checkbox-label .ball {
  23. background-color: #fff;
  24. border-radius: 50%;
  25. position: absolute;
  26. top: 2px;
  27. left: 2px;
  28. height: 22px;
  29. width: 22px;
  30. transform: translateX(0);
  31. transition: transform 0.2s linear;
  32. }
  1. <div class="alt-theme">
  2. <label for="checkbox" class="checkbox-label">
  3. <i class="fa-solid fas fa-moon" style="color: #000; background: none;"></i>
  4. <i class="fa-solid fas fa-sun" style="color: #000; background: none;"></i>
  5. <div class="ball"></div>
  6. </label>
  7. <input type="checkbox" class="checkbox" />
  8. </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 -->

  1. .alt-theme {
  2. height: 80vh;
  3. position: absolute;
  4. right: 10em;
  5. }
  6. .checkbox {
  7. opacity: 1;
  8. position: absolute;
  9. }
  10. .checkbox-label {
  11. background-color: #111;
  12. border-radius: 50px;
  13. cursor: pointer;
  14. display: flex;
  15. align-items: center;
  16. justify-content: space-between;
  17. position: relative;
  18. height: 26px;
  19. width: 50px;
  20. transform: scale(1.5);
  21. }
  22. .checkbox-label .ball {
  23. background-color: #fff;
  24. border-radius: 50%;
  25. position: absolute;
  26. top: 2px;
  27. left: 2px;
  28. height: 22px;
  29. width: 22px;
  30. transform: translateX(0);
  31. transition: transform 0.2s linear;
  32. }

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

  1. &lt;div class=&quot;alt-theme&quot;&gt;
  2. &lt;label for=&quot;checkbox&quot; class=&quot;checkbox-label&quot;&gt;
  3. &lt;i class=&quot;fa-solid fas fa-moon&quot; style=&quot;color: #000; background: none;&quot; /&gt;
  4. &lt;i class=&quot;fa-solid fas fa-sun&quot; style=&quot;color: #000; background: none;&quot; /&gt;
  5. &lt;div class=&quot;ball&quot; /&gt;
  6. &lt;/label&gt;
  7. &lt;input type=&quot;checkbox&quot; class=&quot;checkbox&quot; /&gt;
  8. &lt;/div&gt;

<!-- end snippet -->

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

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

答案1

得分: 1

以下是您要翻译的内容:

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

解决方案

  1. <!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
  2. <!-- 语言:lang-css -->
  3. /* 仅设置开关元素的位置 */
  4. .switch {
  5. position: relative;
  6. display: inline-block;
  7. width: 60px;
  8. height: 34px;
  9. }
  10. /* 隐藏输入元素 --&gt; 这样你就看不到复选框的方框了 */
  11. .switch input {
  12. display: none;
  13. }
  14. /* 设置背景 */
  15. .slider {
  16. position: absolute;
  17. top: 0;
  18. left: 0;
  19. right: 0;
  20. bottom: 0;
  21. background-color: #555;
  22. transition: 0.4s;
  23. cursor: pointer;
  24. border-radius: 34px;
  25. }
  26. /* 设置圆圈 */
  27. .slider:before {
  28. position: absolute;
  29. width: 26px;
  30. height: 26px;
  31. left: 4px;
  32. bottom: 4px;
  33. background-color: #fff;
  34. transition: 0.4s;
  35. border-radius: 34px;
  36. /* 当复选框的值为假时设置图标 */
  37. font-family: "Font Awesome 5 Free";
  38. content: "\f186"; /* Font Awesome fas-moon */
  39. font-weight: 900;
  40. font-size: 15px;
  41. display: flex;
  42. align-items: center;
  43. justify-content: center;
  44. }
  45. /* 复选框的值为真时设置背景 */
  46. .switch input:checked + .slider {
  47. background-color: #ccc;
  48. }
  49. /* 复选框的值为真时设置图标 */
  50. .switch input:checked + .slider:before {
  51. content: "\f185"; /* Font Awesome fas-sun */
  52. transform: translateX(26px);
  53. }
  54. <!-- 语言:lang-html -->
  55. <!-- 导入FontAwesome -->
  56. <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
  57. <!-- 开关输入 -->
  58. <label class="switch" for="checkbox">
  59. <!-- 存储真/假值 -->
  60. <input type="checkbox" id="checkbox" />
  61. <!-- 显示带有月亮/太阳图标的圆圈 -->
  62. <div class="slider round"></div>
  63. </label>
  64. <!-- 结束代码片段 -->

更多信息

英文:

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 -->

  1. /* Just sets position of the switch element */
  2. .switch {
  3. position: relative;
  4. display: inline-block;
  5. width: 60px;
  6. height: 34px;
  7. }
  8. /* Hide input element --&gt; You won&#39;t see the checkbox square like this */
  9. .switch input {
  10. display: none;
  11. }
  12. /* Sets background */
  13. .slider {
  14. position: absolute;
  15. top: 0;
  16. left: 0;
  17. right: 0;
  18. bottom: 0;
  19. background-color: #555;
  20. transition: 0.4s;
  21. cursor: pointer;
  22. border-radius: 34px;
  23. }
  24. /* Sets circle */
  25. .slider:before {
  26. position: absolute;
  27. width: 26px;
  28. height: 26px;
  29. left: 4px;
  30. bottom: 4px;
  31. background-color: #fff;
  32. transition: 0.4s;
  33. border-radius: 34px;
  34. /* Sets icon when value of checkbox is false */
  35. font-family: &quot;Font Awesome 5 Free&quot;;
  36. content: &quot;\f186&quot;; /* Font Awesome fas-moon */
  37. font-weight: 900;
  38. font-size: 15px;
  39. display: flex;
  40. align-items: center;
  41. justify-content: center;
  42. }
  43. /* Sets background when value of checkbox is TRUE */
  44. .switch input:checked + .slider {
  45. background-color: #ccc;
  46. }
  47. /* Sets icon when value of checkbox is TRUE */
  48. .switch input:checked + .slider:before {
  49. content: &quot;\f185&quot;; /* Font Awesome fas-sun */
  50. transform: translateX(26px);
  51. }

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

  1. &lt;!-- Import FontAwesome --&gt;
  2. &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;
  3. &lt;!-- Switch input --&gt;
  4. &lt;label class=&quot;switch&quot; for=&quot;checkbox&quot;&gt;
  5. &lt;!-- Store true/false value --&gt;
  6. &lt;input type=&quot;checkbox&quot; id=&quot;checkbox&quot; /&gt;
  7. &lt;!-- Show circle with moon/sun icon --&gt;
  8. &lt;div class=&quot;slider round&quot; /&gt;
  9. &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:

确定