本篇内容介绍了“CSS怎么实现响应式堆叠卡片悬停效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
在开始之前,我先介绍一下实现的整体大致思路:首先我们将在 HTM 中设计一个简单的卡片结构;然后将定义::before 和::after伪元素并将它们相对于父卡片绝对定位;接着使用transform属性将类“card-inner”的div从其原始位置移开;最后通过使用可以在悬停效果前后translate卡片的变换,在一堆卡片上添加悬停效果。
下面就直接上代码了!
示例代码一:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title></title> <style> body { color: #FDFAF6; background: #50CB93; } .card { position: relative; width: 400px; margin: 60px auto; } .card::before, .card::after { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .card::before, .card::after, .card .card-inner { background-color: #423F3E; border: 1px solid #01937C; transition: transform 0.5s; } .card::before, .card-inner { z-index: 1; } .card-inner { position: relative; padding: 4rem; } /* 将叠好的卡片放在不同的位置 */ .cards:hover { transform: translate(5px, 5px); } .cards:hover::before { transform: translate(-5px, -5px); } .cards:hover::after { transform: translate(-10px, -10px); } </style> </head> <body> <center> <h2>欢迎来到亿速云</h2> <div class="card-container"> <div class="card cards"> <div class="card-inner"> <h2>将鼠标移至方框内</h2> <h4 class="card-title"> 亿速云 </h4> <div class="card-body"> 编程学习平台 </div> </div> </div> </div> </center> </body> </html>
效果如下:
示例代码二:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title></title> <style> body { color: #FDFAF6; background: #50CB93; } :root { --offset-before: 8px; --offset-after: 16px; } .card { position: relative; width: 400px; margin: 60px auto; } .card::before, .card::after { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .card::before, .card::after, .card .card-inner { background-color: #423F3E; border: 1px solid #01937C; transition: transform 0.5s; } .card::before, .card-inner { z-index: 1; } .card-inner { position: relative; padding: 4rem; } .cards-diagonal::before { transform: translate(calc(-1 * 8px), calc(-1 * 8px)); } .cards-diagonal::after { transform: translate(calc(-1 * 16px), calc(-1 * 16px)); } .cards-diagonal:hover::before { transform: translate(8px, 8px); } .cards-diagonal:hover::after { transform: translate(16px, 16px); } .cards-rotate::before, .cards-rotate::after { transform-origin: 50% 100%; } .cards-rotate:hover { transform: translate(2.5px, 0) rotate(2.5deg); } .cards-rotate:hover::before { transform: translate(2.5px, 0) rotate(2.5deg); } .cards-rotate:hover::after { transform: translate(5px, 0) rotate(5deg); } } </style> </head> <body> <center> <h2>欢迎来到亿速云</h2> <div class="card-container"> <div class="card cards-diagonal"> <div class="card-inner"> <h2>将鼠标移至框内</h2> <h4 class="card-title">亿速云</h4> <div class="card-body"> 编程学习平台 </div> </div> </div> </div> <div class="card-container"> <div class="card cards-rotate"> <div class="card-inner"> <h2>将鼠标移至框内</h2> <h4 class="card-title">亿速云</h4> <div class="card-body"> 编程学习平台 </div> </div> </div> </div> </center> </body> </html>
效果如下:
示例代码三:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title></title> <style> body { color: #FDFAF6; background: #50CB93; } :root { --offset-before: 8px; --offset-after: 16px; } .card { position: relative; width: 400px; margin: 60px auto; } .card::before, .card::after { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .card::before, .card::after, .card .card-inner { background-color: #423F3E; border: 1px solid #01937C; transition: transform 0.5s; } .card::before, .card-inner { z-index: 1; } .card-inner { position: relative; padding: 4rem; } /*Stacked => Up*/ .cards-up::before, .cards-up::after { transform-origin: center bottom; } .cards-up:hover { transform: translate(0, -5px); } .cards-up:hover::before { transform: translate(0, 5px) scale(0.95); } .cards-up:hover::after { transform: translate(0, 10px) scale(0.90); } /*Stacked => Right */ .cards-right::before, .cards-right::after { transform-origin: left center; } .cards-right:hover { transform: translate(5px, 0); } .cards-right:hover::before { transform: translate(-10px, 0) scale(0.95); } } .cards-right:hover::after { transform: translate(-10px, 0) scale(0.90); } } </style> </head> <body> <center> <h2>欢迎来到亿速云</h2> <div class="card-container"> <div class="card cards-up"> <div class="card-inner"> <h2>将鼠标移至框内</h2> <h4 class="card-title">亿速云</h4> <div class="card-body"> 编程学习平台 </div> </div> </div> </div> <div class="card-container"> <div class="card cards-right"> <div class="card-inner"> <h2>将鼠标移至框内</h2> <h4 class="card-title">亿速云</h4> <div class="card-body"> 编程学习平台 </div> </div> </div> </div> </center> </body> </html>
效果如下:
“CSS怎么实现响应式堆叠卡片悬停效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。