我来为您讲解一下使用CSS实现“加号”效果的攻略。
我来为您讲解一下使用CSS实现“加号”效果的攻略。
一、通过动态伪类实现
HTML结构:
<div class="box">
<div class="plus"></div>
</div>
CSS样式:
.box {
width: 50px;
height: 50px;
background-color: #f2f2f2;
position: relative;
}
.plus {
width: 15px;
height: 2px;
background-color: #333;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
}
.plus::before,
.plus::after {
content: "";
position: absolute;
width: 2px;
height: 15px;
background-color: #333;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
}
.plus::before {
transform: translate(-50%, -50%) rotate(-90deg);
}
.plus:hover::before,
.plus:hover::after {
height: 25px;
}
代码实现思路:
通过在伪类 ::before 和 ::after 上设置相应的样式,来实现鼠标悬浮时 + 变成 × 的效果。
二、通过CSS3动画实现
HTML结构同上。
CSS样式:
.box {
width: 50px;
height: 50px;
background-color: #f2f2f2;
position: relative;
}
.plus {
width: 15px;
height: 2px;
background-color: #333;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
transition: all .2s ease-in-out;
}
.plus:hover {
transform: translate(-50%, -50%) rotate(45deg);
}
.plus::before,
.plus::after {
content: "";
position: absolute;
width: 2px;
height: 15px;
background-color: #333;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(0);
cursor: pointer;
transition: all .2s ease-in-out;
}
.plus:hover::before {
transform: translate(-50%, -50%) rotate(-45deg);
}
.plus:hover::after {
transform: translate(-50%, -50%) rotate(45deg);
}
代码实现思路:
通过设置 transform: translate(-50%, -50%) rotate(45deg),使 + 变成斜着的 ×;通过设置 transform: translate(-50%, -50%) rotate(-45deg); 和 transform: translate(-50%, -50%) rotate(45deg);,使 × 旋转变成 +,从而实现加号、减号效果的切换。
以上就是通过CSS实现“加号”效果的完整攻略,希望能对您有所帮助。
编程基础网
本文标题为:css实现“加号”效果的实例代码
基础教程推荐
猜你喜欢
- 什么是JavaScript的防抖与节流 2023-08-11
- JS TextArea字符串长度限制代码集合 2023-12-15
- Select 选择器显示内容为icon图标选项(Ant Design of Vue) 2023-10-08
- 深入理解JavaScript系列(44):设计模式之桥接模式详解 2023-12-13
- Ajax提交post请求案例分析 2023-02-22
- CSS网页布局:div垂直居中的各种方法 2023-12-09
- ajax编写简单的登录页面 2023-01-31
- ajax基本通用代码示例 2022-12-28
- 浅析巧用Ajax的beforeSend提高用户体验 2022-12-28
- 前端面试复盘:vue技术面没有难倒我,hr面却是一把挂 2023-10-08
