Animation can be a powerful way to enhance a user experience. In this lesson, we'll walk through the creation of a card-flip animation, creating a nice visual effect when toggling between the front and back of a div meant to represent a card, like a playing card, or a flashcard. We'll create this effect using a touch of JavaScript and CSS transforms. No framework or library required.

.container {
display: flex;
flex-direction: column;
align-items: center;
width: 50vw;
margin: auto;
} .card {
margin-top: 1rem;
height: 60vh;
width: 100%;
position: relative;
} .cardFront,
.cardBack {
border-radius: 0.25rem;
height: 100%;
padding-left: 0.5rem;
padding-right: 0.5rem;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
width: 100%;
position: absolute;
backface-visibility: hidden;
transition: transform 0.5s ease;
} .cardBack {
transform: perspective(600px) rotateY(180deg);
background-color: #ebf4ff;
} .cardBack.flipped {
transform: perspective(600px) rotateY(0deg);
} .cardFront {
transform: perspective(600px) rotateY(0deg);
} .cardFront.flipped {
transform: perspective(600px) rotateY(-180deg);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>CSS Card Flip</title>
<link rel="stylesheet" href="./styles.css" />
</head>
<body>
<div class="container">
<button type="button" id="flip-btn">Flip Card</button>
<div class="card">
<div id="back" class="cardBack">Back</div>
<div id="front" class="cardFront">Front</div>
</div>
</div>
<script>
const front = document.getElementById('front')
const back = document.getElementById('back')
const btn = document.getElementById('flip-btn')
function handleFlip() {
front.classList.toggle('flipped')
back.classList.toggle('flipped')
}
btn.addEventListener('click', handleFlip)
</script>
</body>
</html>

最新文章

  1. List tuple 类型转成数组
  2. Nginx php-fpm php mysql
  3. android 退出整个程序
  4. C#开发---利用特性自定义数据导出到Excel
  5. Event Aggregator
  6. Balanced Lineup
  7. JVM分代垃圾回收策略的基础概念
  8. C# 伪造 referer 提交数据
  9. H.264 RTP 封包格式
  10. 【转】Flash字体嵌入方法
  11. System.Data.SqlClient.SqlException (0x80131904): EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配。上一计数 = 1,当前计数 = 0。 EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配。上一计数 = 1,当前计数 = 0。
  12. 理解性能的奥秘——应用程序中慢,SSMS中快(4)——收集解决参数嗅探问题的信息
  13. ♣eclipse护眼颜色和关键字颜色设置
  14. Word Ladder I &amp; II
  15. windows下配置nutch注意的问题
  16. 使用 p4-graphs 命令将p4程序依赖关系图形化
  17. springMvc REST 请求和响应
  18. spring加载jar包中多个配置文件(转载)
  19. JSP学习(三)JSP中的九个内置对象
  20. PyQt5(2)——调整布局(布局管理器)第一个程序

热门文章

  1. 协议——UART(RS232)
  2. quartz2.3.0(一)您的第一个Quartz程序
  3. jmeter_分布式测试
  4. html中实现某区域内右键自定义菜单
  5. Web SSH远程连接利器:gotty
  6. docker postgres 导出导入数据
  7. elasticsearch*3 + Es-Head + kibana Docker集群
  8. Matlab函数装饰器
  9. 怎么让自己的本地php网站让别人访问到
  10. UI5-技术篇-Hybrid App-2-Geolocation位置定位