CSS 卡牌反转效果
CSS实现卡牌翻转,先看效果:

源代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS-卡牌翻转效果</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.flip-card {
width: 268px;
height: 392px;
background: transparent;
perspective: 1000px; /* 开启透视效果,观察者与 z=0 平面的距离 */
}
.flip-card-inner {
position: relative;
width: 100%;
height: 100%;
text-align: center;
transition: transform .6s;
transform-style: preserve-3d; /* 是否让子元素处于3D空间效果中 */
}
/* 用特定css类名触发卡片翻转效果 */
.flipped .flip-card-inner {
transform: rotateY(-180deg);
}
.flip-card-front,
.flip-card-back {
position: absolute;
width: 100%;
height: 100%;
-webkit-backface-visibility: hidden;
backface-visibility: hidden; /* 背面元素朝向用户时,是否可见 */
border-radius: 16px;
box-shadow: 0 2px 8px 0 rgba(0, 0, 0, .16);
overflow: hidden;
}
.flip-card-front:hover,
.flip-card-back:hover {
cursor: pointer;
}
.flip-card-back {
display: flex;
justify-content: center;
align-items: center;
background: #fcfcfc url('./poker.png') center/388px 504px no-repeat; /* 给背面添加一个卡牌图案背景 */
}
.flip-card-front {
display: flex;
justify-content: center;
align-items: center;
background-color: #fff;
transform: rotateY(180deg);
}
.heart {
position: relative;
display: inline-block;
width: 96px;
height: 88px;
}
/* 做重合的两个长方形(+顶部圆角)对称设成斜45度,组合成“心形” */
.heart::before,
.heart::after {
content: "";
position: absolute;
top: 0;
width: 52px;
height: 80px;
background: #ff2200;
border-radius: 50px 50px 0 0;
}
/* 向左偏移并旋转 -45 度 */
.heart::before {
left: 50px;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
/* 向右偏移并旋转 45 度 */
.heart::after {
left: 0;
transform: rotate(45deg);
transform-origin: 100% 100%;
}
</style>
</head>
<body>
<div class="container">
<div class="flip-card " id="flip-card">
<div class="flip-card-inner">
<div class="flip-card-back"></div>
<div class="flip-card-front">
<span class="heart"></span>
</div>
</div>
</div>
</div>
<script>
const card = document.getElementById('flip-card');
card.addEventListener('click', () => {
card.classList.toggle('flipped');
});
</script>
</body>
</html>
浙公网安备 33010602011771号