html - 想要在纯 css 中复制 youtube 的自动播放切换按钮
问题描述
直到最近,youtube 还使用了一个名为“paper-toggle-button”的标签。我的脚本使用了它,但自从 youtube 删除它后,我不得不用一个普通的无聊复选框来解决。我不只是想要一个答案。我想了解它是如何工作的。让我感到困扰的是,这个高级 css 还没有为我点击。
我一直在尝试通过以各种方式展示如何制作滑动切换按钮的教程来复制它。但我对外观并不满意。我希望它看起来尽可能接近 youtube 的切换按钮。至少我学到了一件事。下面的代码不需要任何好的图片。
这需要我没有的高级 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。然后你有一个完整的控制悬停它(字体大小,颜色,...)。
推荐阅读
- c - 获取整数的负号并将其存储为 char 的最佳方法是什么?
- angularjs - 在sql server Angularjs asp.netmvc中数据更新之前显示记录的搜索方法
- python - 对多个数据帧执行类似的分析
- jboss - 部署自定义 SPI (FreeMarkerLoginFormsProvider) 时出现问题
- matlab-figure - 如何绘制一些数据的灰度及其置信区间
- c - 在对 execvp 执行无效命令后,命令和结果打印了两次
- python - 将实例字段转换为属性
- azure - Azure RM 模板。有条件地部署复制资源
- version - 如何将 Visual Studio 代码更新到最新版本
- sql - PLS-00103:在预期以下情况之一时遇到符号“CREATE”: