css画三角形加旋转动画(实现水平或垂直翻转卡片动画效果)
css画三角形加旋转动画(实现水平或垂直翻转卡片动画效果)
html:
<h1>垂直翻转</h1>
<div class="card vertical">
<div class="card-side front">
<div>正面</div>
</div>
<div class="card-side back">
<div>背面</div>
</div>
</div>
<h1>水平翻转</h1>
<div class="card horizontal">
<div class="card-side front">
<div>正面</div>
</div>
<div class="card-side back">
<div>背面</div>
</div>
</div>
css:
/*卡片样式*/
.card {
perspective: 150rem;
position: relative;
height: 20rem;
max-width: 200px;
box-shadow: none;
background: none;
}
.card-side {
height: 10rem;
border-radius: 15px;
transition: all 0.8s ease;
backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
width: 80%;
padding:2rem;
color: white
}
/*正面背景*/
.card-side.front {
background-color: #0093E9;
background-image: linear-gradient(160deg #0093E9 0% #80D0C7 100%);
}
/*背面背景*/
.card-side.back {
background-color: #4158D0;
background-image: linear-gradient(43deg #4158D0 0% #C850C0 46% #FFCC70 100%);
}
/*垂直翻转*/
.vertical .back{
transform: rotateX(-180deg);
}
.vertical:hover .card-side.front {
transform: rotateX(180deg);
}
.vertical:hover .card-side.back {
transform: rotateX(0deg);
}
/*水平翻转*/
.horizontal .back{
transform: rotateY(-180deg);
}
.horizontal:hover .card-side.front {
transform: rotateY(180deg);
}
.horizontal:hover .card-side.back {
transform: rotateY(0deg);
}
总结
翻转卡片效果使用了以下css的知识点:
- 背景色—— background-color
- 背景渐变 —— background-image: linear-gradient()
- css 转换 —— transform
- css 旋转 —— rotateY、rotateX
- css 透视 —— perspective