【问题标题】:Checkbox doesn't apply stylesheets on JavaScript checkbox check复选框不会在 JavaScript 复选框检查上应用样式表
【发布时间】:2021-11-01 04:21:00
【问题描述】:

在选中 ::before{} 伪元素复选框时应用附加样式表的解决方案是什么?一旦我提供了 checked={checked} 属性,附加的样式表就可以工作了

export const ThemeSwithcer: FC = () => {
    const [checked, setChecked] = useState(false);

    useEffect(() => {                
        if (localStorage.getItem('theme') === 'theme-dark') {
            setTheme('theme-dark');
            setChecked(false);
        } else {
            setTheme('theme-light');
            setChecked(true);
        }
    }, [checked]);

    const setTheme = (themeName: string) => {
        localStorage.setItem('theme', themeName);
        document.documentElement.className = themeName;
    }

    const themeChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
        if (localStorage.getItem('theme') === 'theme-dark') {
            setTheme('theme-light');
        } else {
            setTheme('theme-dark');
        }
    }

    return (
        <label className="Theme-Switcher">
            <input className="Theme-Switcher-Util" type="checkbox" onChange={themeChangeHandler} checked={checked} />
            <span className="Theme-Switcher-Slider round"></span>
        </label>
    );
}
&:checked + .Theme-Switcher-Slider:before {
  -webkit-transform: translateX(24px);
  -ms-transform: translateX(24px);
  transform: translateX(24px);
  background: white url('https://i.ibb.co/7JfqXxB/sunny.png');
  background-repeat: no-repeat;
  background-position: center;
}

【问题讨论】:

    标签: reactjs typescript sass stylesheet


    【解决方案1】:

    TL;DR

    另外,你必须删除:

    useEffect(() => {                
      if (localStorage.getItem('theme') === 'theme-dark') {
        setTheme('theme-dark');
        setChecked(false);
      } else {
        setTheme('theme-light');
        setChecked(true);
      }
    }, [checked]);
    

    并且可以在这张图片中看到它是如何工作的:https://im5.ezgif.com/tmp/ezgif-5-f94608df776f.gif

    说明

    • 有了这个useEffect,你就可以重新设置check的值了。

    不清楚是否要将样式应用于选中的元素或旁边的跨度。

    所以,我为这两个示例创建了一个代码框,您可以查看:https://codesandbox.io/s/summer-brook-ktw08?file=/src/styles.css

    但代码是:

    在下一个代码中,您将看到一个示例:

    1. 普通复选框的 CSS 选择器
    2. 选中复选框的 CSS 选择器
    3. 之前选中复选框的 CSS 选择器
    4. 复选框之前的邻居的 CSS 选择器
    5. 选中复选框之前的邻居的 CSS 选择器

    前 3 条中的规则只是示例。最后 2 条中的规则是根据您的代码和特定需求显示月亮和太阳。

    重要我想说您缺少的部分是之前元素修改中的content: ' '; display: inline-block。基本上,如果没有这 2 个属性,您将无法看到之前的内容,因为它的默认大小是 0x0。

    /* 1. Checkbox */
    .Theme-Switcher-Util {
      margin-right: 36px;
    }
    
    /* 2. Checked Element */
    .Theme-Switcher-Util:checked {
      margin-right: 36px;
    }
    
    /* 3. Before of the Checked Element */
    .Theme-Switcher-Util:checked::before {
      content: ' ';
      display: inline-block;
      background: rgba(255, 0, 0, .5);
      width: 50px;
      height: 10px;
    }
    
    /* 4. Before of the Slider after the NOT Checked Element */
    .Theme-Switcher-Util + .Theme-Switcher-Slider::before {
      content: ' ';
      display: inline-block;
      height: 24px;
      width: 24px;
      background: white url('https://i.pinimg.com/564x/cc/d4/46/ccd4469667ba2915b82d48e28700843a.jpg');
      background-repeat: no-repeat;
      background-position: center;
      background-size: cover;
    }
    /* 5. Before of the Slider after the Checked Element */
    .Theme-Switcher-Util:checked + .Theme-Switcher-Slider::before {
      content: ' ';
      display: inline-block;
      height: 24px;
      width: 24px;
      background: white url('https://i.ibb.co/7JfqXxB/sunny.png');
      background-repeat: no-repeat;
      background-position: center;
    }
    

    【讨论】:

    • 我想要的是,当复选框状态设置为使用 JavaScript 选中时,提供的样式表将应用于 before 伪元素。这一般可以吗?
    • 在这种情况下,您需要的选择器是 3 .Theme-Switcher-Util:checked::before 用于检查之前或 5 .Theme-Switcher-Util:checked + .Theme-Switcher-Slider::before 用于它旁边的跨度。另外,包括`内容:''; display: inline-block;` 否则不起作用
    • 在代码框中,您可以通过复选框看到月亮/太阳 :)
    • 您可以检查这个特定的沙箱:codesandbox.io/s/adoring-cartwright-mvwei?file=/src/styles.css 与您的具体案例,没有其他示例
    • 你能指导我完成吗? =c 这里是完整的 scss 代码 sn-p jsfiddle.net/j1hv8Ly7
    猜你喜欢
    • 2017-09-07
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多