html - 想要在纯 css 中复制 youtube 的自动播放切换按钮

标签 html css

直到最近 youtube 使用了一个名为“paper-toggle-button”的标签。我的脚本使用了它,但自从 youtube 删除它后,我不得不用一个普通的无聊复选框来解决。 我不只是想要一个答案。我想了解它是如何工作的。令我烦恼的是,这个高级 CSS 还没有为我点击。

我一直在尝试通过以各种方式展示如何制作滑动切换按钮的教程来复制它。但我对外观不满意。我希望它看起来尽可能靠近 youtube 的切换按钮。至少我学到了一件事。下面的代码不需要任何图片,这很好。

picture of the toggle button

这需要我没有的 css 高级知识。

这是一个例子,它看起来很丑。因为它必须例如手动将标签放在正确的位置。参见 .labelterm。当我无法使用本教程代码添加复选标记时,我放弃了。

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
    <title>Awesome checkbox</title>
    <style type="text/css">
        .mylabel {
            position: relative;
            display: block;
            width: 60px;
            height: 30px;
            margin-bottom: 15px;
        }
        .mylabel input {
            display: none;
        }
        .slidinggroove {
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            background: #ababab;
            /*background: rgba(0, 0, 0, 0.1);*/
            border-radius: 20px;
            transition: all 0.3s ease;
        }
        .slidinggroove:after {
            position: absolute;
            content: "";
            width: 28px;
            height: 28px;
            border-radius: 50%;
            background: #fff;
            /*background: rgba(255, 255, 255, 0.2);*/
            top: 1px;
            left: 1px;
            transition: all 0.3s ease;
        }
        input:checked + .slidinggroove {
            background: #5fcf80;
        }
        input:checked + .slidinggroove:after {
            transform: translateX(30px);
        }
        .labelterm {
            margin-left: 65px;
            font-size: 16px;
            color: #222;
            font-family: "Roboto", sans-serif;
            position: relative;
            top: 5px;
        }
    </style>
</head>

<body>
    <div class="mylabel">
        <input type="checkbox" id="coding">
        <div class="slidinggroove"></div>
        <label class="mylabel" for="coding" name="skills"><p class="labelterm">Test</p></label>
    </div>
</body>
</html>

最佳答案

以下是我将如何解决此问题以实现所示图片的外观:

.slidinggroove::before{
  position: absolute;
  left: 7px;
  top: 50%;
  transform: translateY(-7px);
  width: 20px;
  height: 20px;
  font-family: "Font Awesome 5 Free";
  font-weight: 900;
  content: "\f00c";
  display: block;
  color: #fff;
}

您必须将图标库作为字体导入,我建议使用 FontAwesome,如此处的工作片段所示:

.mylabel {
  position: relative;
  display: block;
  width: 60px;
  height: 30px;
  margin-bottom: 15px;
}

.mylabel input {
  display: none;
}

.slidinggroove {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: #ababab;
  /*background: rgba(0, 0, 0, 0.1);*/
  border-radius: 20px;
  transition: all 0.3s ease;
}

.slidinggroove:after {
  position: absolute;
  content: "";
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #fff;
  /*background: rgba(255, 255, 255, 0.2);*/
  top: 1px;
  left: 1px;
  transition: all 0.3s ease;
}

.slidinggroove:before {
  position: absolute;
  left: 7px;
  top: 50%;
  transform: translateY(-7px);
  width: 20px;
  height: 20px;
  font-family: "Font Awesome 5 Free";
  font-weight: 900;
  content: "\f00c";
  display: block;
  color: #fff;
}

input:checked+.slidinggroove {
  background: #5fcf80;
}

input:checked+.slidinggroove:after {
  transform: translateX(30px);
}

.labelterm {
  margin-left: 65px;
  font-size: 16px;
  color: #222;
  font-family: "Roboto", sans-serif;
  position: relative;
  top: 5px;
}
<html>

<head>
  <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
  <title>Awesome checkbox</title>
</head>

<body>
  <div class="mylabel">
    <input type="checkbox" id="coding">
    <div class="slidinggroove"></div>
    <label class="mylabel" for="coding" name="skills"><p class="labelterm">Test</p></label>
  </div>
</body>

</html>

它的工作方式如下,您创建一个伪元素,其内容将是 fontawesome 图标的 unicode。然后您可以完全控制悬停它(字体大小、颜色、...)。

关于html - 想要在纯 css 中复制 youtube 的自动播放切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66600215/

相关文章:

html - 创建 pin 时将图像传递到 Pinterest?

javascript - div 的子项是否有可能设置为指针事件 : none to have pointer events?

javascript - 单击特定按钮以显示特定 ID

具有最大宽度的居中流体 div

javascript - 下拉列表不显示元素 Bootstrap 3

javascript - 从 javascript 数组向 html 页面添加新元素

html - 第一个和最后一个 child 不使用类 div

javascript - 选中复选框时如何向文本区域添加值

css - 是否有根据 css 生成图像 Sprite 的繁重任务?

html - <br/> 高度调整与其他行对齐相同的高度