yah*_*rga 3 html css css3 css-transitions
我想创建一个切换button,将其形状从加号变为减号.
仅使用CSS, 不使用伪元素.
我想要的效果是让"+"符号中的垂直线收缩到水平线.
我知道这是可能的,但我不确定哪条路是最佳路线.我正在考虑做一些事情,height但我担心line-height浏览器会改变它在元素中的位置.
$('button').on("click", function(){
$(this).toggleClass('active');
});Run Code Online (Sandbox Code Playgroud)
button {
color: #ecf0f1;
background: #e74c3c;
width: 50px;
height: 50px;
border: 0;
font-size: 1.5em;
}
button span {
transition: all .75s ease-in-out;
}
button.active span {
/* Code to morph + to - */
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button><span>+</span></button>Run Code Online (Sandbox Code Playgroud)
Jac*_*ray 11
最简单的方法是使用+和-元素(使用伪元素最干净),然后将它们转换为"变形":
button {
color: #ecf0f1;
background: #e74c3c;
width: 50px;
height: 50px;
border: 0;
font-size: 1.5em;
position: relative;
}
button span {
position: absolute;
transition: .3s;
background: white;
border-radius: 2px;
}
button span:first-of-type {
top: 25%;
bottom: 25%;
width: 10%;
left: 45%;
}
button span:last-of-type {
left: 25%;
right: 25%;
height: 10%;
top: 45%;
}
button:hover span:first-of-type, button:hover span:last-of-type {
transform: rotate(90deg);
}
button:hover span:last-of-type {
left: 50%;
right: 50%;
}Run Code Online (Sandbox Code Playgroud)
<button>
<span></span><span></span>
</button>Run Code Online (Sandbox Code Playgroud)
dip*_*pas 10
注意:请停止编辑使答案不正确的问题
$('button').on("click", function(){
$(this).toggleClass('active');
});Run Code Online (Sandbox Code Playgroud)
button {
color: #ecf0f1;
background: #e74c3c;
width: 70px;
height: 70px;
position: relative;
font-size: 50px;
cursor: pointer;
border: 0;
outline: 0;
padding: 0
}
.plus,
.minus {
color: #fff;
padding: 10px;
width: 70px;
height: 70px;
line-height: 50px;
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
text-align: center;
box-sizing: border-box;
transition: .5s all ease-out;
}
.plus {
opacity: 1;
transform: rotate(0deg);
}
button.active .plus {
opacity: 0;
transform: rotate(90deg);
}
.minus {
opacity: 0;
transform: rotate(-90deg);
}
button.active .minus {
opacity: 1;
transform: rotate(0deg);
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
<button>
<span class="plus"><i class="fa fa-plus"></i></span>
<span class="minus"><i class="fa fa-minus"></i></span>
</button>Run Code Online (Sandbox Code Playgroud)
使用::before带有content属性的伪元素
$('button').on("click", function() {
$(this).toggleClass('active');
});Run Code Online (Sandbox Code Playgroud)
button {
color: #ecf0f1;
background: #e74c3c;
width: 50px;
height: 50px;
border: 0;
font-size: 1.5em;
}
button span {
transition: all .75s ease-in-out;
position:relative
}
button span::before {
content:"+"
}
button.active span::before {
content:"-"
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button><span></span></button>Run Code Online (Sandbox Code Playgroud)
不需要span,您可以使用text()jquery中的if语句来执行此操作
$('button').on("click", function() {
$(this).toggleClass('active');
$(this).text() == "+" ? $(this).text("-") : $(this).text("+");
});Run Code Online (Sandbox Code Playgroud)
button {
color: #ecf0f1;
background: #e74c3c;
width: 50px;
height: 50px;
border: 0;
font-size: 1.5em;
transition: all .75s ease-in-out;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button>+</button>Run Code Online (Sandbox Code Playgroud)