我想在白色圆圈内放置一个图标,表示切换是什么,但我无法让图标显示在正确的位置,更不用说跟随圆圈的go 向了.我一直在try 使用字体令人敬畏的图标.

我想要的是

Either one of these is my end goal, but I still have a hard time figuring out how to make it work. toggle with icons

我的现有代码

如果有帮助的话,this是我正在工作的网站.

这是一个css

/* Toggle Styles */
.toggle-container {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px
}

.toggle {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

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

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  border-radius: 50%;
  transition: 0.4s;
}

input:checked + .slider {
  background-color: #2FA28D; /* Color for dark mode background */
}

input:checked + .slider:before {
  transform: translateX(26px);
}

我不知道这个脚本是否重要,但它是这样的:

const modeToggle = document.getElementById('modeToggle');
const body = document.body;

modeToggle.addEventListener('change', () => {
  if (modeToggle.checked) {
    body.classList.add('dark-mode');
    body.classList.remove('light-mode');
    localStorage.setItem('mode', 'dark');
  } else {
    body.classList.add('light-mode');
    body.classList.remove('dark-mode');
    localStorage.setItem('mode', 'light');
  }
});

// Check localStorage for saved mode preference
const savedMode = localStorage.getItem('mode');
if (savedMode === 'dark') {
  body.classList.add('dark-mode');
  modeToggle.checked = true;
} else {
  body.classList.add('light-mode');
  modeToggle.checked = false;
}

我try 在切换中放置一个i元素,如下所示:

<label class="toggle">
  <input type="checkbox" id="modeToggle">
  <span class="slider round"></span>
  <i class="fas-fa-moon"></i>
</label>

与不带它的对比:

<label class="toggle">
  <input type="checkbox" id="modeToggle">
  <span class="slider round"></span>
</label>

我还try 了用其他各种我没有余地的方式来更新css.我对css还很陌生,所以现在任何东西都会有帮助.

推荐答案

  • 你可以使用一些图标或Unicode emoji作为子元素,也可以用css为它们设置动画.
  • 关于暗模式的切换,最好的方法是使用data-*属性,比如<html>元素中的data-mode="light".然后在JS内部,您可以在document.documentElement上切换它(请参见示例)
  • 记住用户期望的模式see this related answer

document.querySelector(".toggle input").addEventListener("change", (evt) => {
  document.documentElement.dataset.mode = evt.currentTarget.checked ? "dark" : "light";
});
.toggle {
  position: relative;
  overflow: hidden;
  display: inline-flex;
  gap: 0.75rem;
  align-items: center;
  padding: 0.5rem;
  background-color: #444;
  border-radius: 2rem;
  user-select: none;
  cursor: pointer;
  transition: translate 0.3s, background-color 0.8s;
  
  &::before {
    position: absolute;
    top: 1px;
    left: 1px;
    content: "";
    height: calc(100% - 2px);
    aspect-ratio: 1;
    border-radius: 50%;
    transition: translate 0.3s, background-color 0.8s;
    background: #fff;
  }
  
  & input {
    position: absolute;
    appearance: none;
  }
  
  & span {
    position: relative;
    transition: translate 0.6s ease 0.2s;
    
    &:nth-of-type(1) {
      translate: 0 150%;
    }
    
    &:nth-of-type(2) {
      translate: 0 0%;
    }
  }
  
  &:has(input:checked) {
    background-color: #ddd;
    
    & span:nth-of-type(1) {
      translate: 0 0%;
    }
    
    & span:nth-of-type(2) {
      translate: 0 -150%;
    }
  }
  
  &:has(input:checked)::before {
    translate: 100% 0;
    background-color: #444;
  }
}

/* Use like: */

[data-mode="dark"] {
  & body {
    background: #444;
    color: #eee;
  }
  & #someSpecificElement {
    /* Some dark mode styles here */
  }
}
<label class="toggle" arial-label="Toggle dark mode" tabindex=0>
  <input type="checkbox">
    <span>☀️</span>
    <span>?</span>
</label>

Html相关问答推荐

如何打破长URL,包含连字符在HTML与CSS

CSS位置:固定尊重母公司的保证金属性.""'为什么?

Tumblr博客:如何修复提交页面和询问页面框列计数问题?

在CSS中旋转排除的蒙版图像

如何使用html和css关键帧创建动画

如何创建剪切到路径的循环文本字幕?

如何生成随机字符串的字母数字字符集长度到html跨度?

使用CSS Flexbox时,我的图像未对齐

将精选选项的价格合并为一个价格HTML、PHP和JQuery

获取标准的Python脚本,以便使用FASK对网站进行Flask

如果最后一行的卡数少于其他行,则在使用flex grow使卡在调整大小时拉伸后,flex卡的宽度将被拉伸

悬停表格单元格文本时,使元素的字体大小更大,同时保持表格单元格的高度不变

如何从通过 find-each 方法(在 Rails 应用程序中)生成的列表创建下拉菜单?

如何调整边框半径以匹配父边框半径

将图像高度调整到容器 div 中而不使其高度增加

把标题、段落和图片放在一起

如何向上移动图像并溢出图像的一部分而另一部分不溢出

隐藏在标题后面的下拉菜单

让 Bootstrap 5 模式和背景占据父容器的宽度和高度,而不是全屏

为什么浏览器在 Select 一个包裹在 标签中的块级元素后包含相邻元素?