javascript - 我如何在 CSS onClick 事件中使用 :after
问题描述
function mutevideo(evt) {
if (evt.target.classList.contains('fa-video-camera')) {
evt.target.classList.remove('fa-video-camera')
evt.target.classList.add('fa-video-camera:after') // need here above fontawesom class with slash
} else {
evt.target.classList.remove('fa-video-camera:after') // need here above fontawesom class with slash
evt.target.classList.add('fa-video-camera')
}
}
.fa-video-camera:after {
position: absolute;
content: "/";
color: white;
font-weight: 900;
font-size: 20px;
left: 10px;
top: 7px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<p class="bg-white text-danger rounded-circle"><i onClick=mutevideo(evt) class="fa fa-video-camera p-2 fa-1x"></i></p>
问题:-
在 fontawesome 4.7 版本中,它没有 5.x 版本中的斜线视频,所以在这里我编写了自己的 css:after
来实现这一目标,现在我将如何在页面加载后立即获得视频斜线,但是我的要求是我点击后需要在图标上加上斜线。
注意:-
这里我使用的是fontawesome 4.7版,任何帮助或建议都非常感谢
解决方案
您可以简单地使用自定义类.stop
在单击时在您的字体真棒图标中添加斜线。无需添加另一个图标即可添加斜线。
要在点击时显示和隐藏,我们可以简单地使用toggle()函数而不是做很多if's
and else
or add
or or remove
classes
运行下面的代码段。
function mutevideo(evt) {
if (evt.classList.contains('fa-video-camera')) {
evt.classList.toggle('stop')
}
}
.stop:after {
position: absolute;
content: "/";
color: white;
font-weight: bold;
font-size: 23px;
left: 11px;
top: 12px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<p class="bg-white rounded-circle"><i onClick=mutevideo(this) class="fa fa-video-camera p-2 fa-1x"></i></p>
推荐阅读
- python - 将函数/方法传递给 python 脚本
- delphi - Delphi XE2:如何在设计时使属性可设置?
- java - Java划分两个浮点数/双精度数(从变量中检索的值)
- c# - 我正在尝试将四叉树代码更新为八叉树
- python - 使用 Hparam 仪表板抛出错误的超参数调整
- docker - 由于 LaunchWaitCondition 导致无法创建多容器 docker 的 Elastic Beanstalk 环境
- python - 如何继承抽象基类?
- reactjs - 如何在 ReactJS 中进行初始化
- javascript - 单个JS函数替换多个字符串
- android - 以编程方式拍照并返回文件名和路径