您好,登錄后才能下訂單哦!
本篇內容介紹了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。