一、CSS卡片翻轉
卡片翻轉是一種非常常用的交互效果,可以將卡片的正面和反面展現出來,增加信息呈現的方式和效果。下面是一個基礎的CSS卡片翻轉代碼實現。
.card {
position: relative;
width: 300px;
height: 200px;
transform-style: preserve-3d;
transition: all 0.6s ease;
}
.front,
.back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
transition: all 0.6s ease;
}
.front {
transform: rotateY(0deg);
background-color: #eee;
}
.back {
transform: rotateY(180deg);
background-color: #ccc;
}
.card:hover {
transform: rotateY(180deg);
}
這段代碼使用了CSS3中的3D變換,將卡片翻轉展示。其中,preserve-3d是保留元素自己的3D空間,backface-visibility用於控制反面可見性,避免翻轉時出現奇怪的效果。需要注意的是,代碼中card:hover用於在鼠標經過時觸發翻轉效果。
二、CSS卡片翻轉效果
卡片翻轉依然是本節的主題,但是這裡我們討論的是不同的翻轉效果展現方式。如下是一種氣泡翻轉卡片效果:
.card {
position: relative;
width: 300px;
height: 200px;
transform-style: preserve-3d;
cursor: pointer;
transition: all 0.5s ease;
}
.front,
.back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
transform-style: preserve-3d;
}
.front {
background-color: #fff;
box-shadow: 0 2px 0 rgba(0, 0, 0, 0.1);
transform: rotateX(0deg) rotateY(0deg) scale(1);
z-index: 2;
}
.back {
background-color: #fff;
box-shadow: 0 2px 0 rgba(0, 0, 0, 0.1);
transform: rotateX(90deg) rotateY(0deg) scale(1);
}
.card:hover .front {
transform: rotateX(-80deg) rotateY(0deg) scale(0.8);
z-index: 1;
}
.card:hover .back {
transform: rotateX(0deg) rotateY(0deg) scale(1);
z-index: 2;
}
該代碼中使用了rotateX,rotateY和scale等屬性,實現了卡片在不同方向的旋轉展現,同時實現了鼠標經過時的翻轉效果。需要注意的是,card:hover選擇器僅在鼠標經過時生效,這也是該效果的亮點之一。
三、CSS卡片布局
除了翻轉效果外,卡片的布局也是非常重要的一個方面。下面是一種基礎的卡片布局形式:
.card-wrapper {
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.card {
flex: 0 1 300px;
margin: 20px;
height: 200px;
background-color: #fff;
box-shadow: 0 2px 0 rgba(0, 0, 0, 0.1);
transition: all 0.2s ease;
cursor: pointer;
position: relative;
}
.card::before {
content: '';
position: absolute;
top: -1px;
bottom: -1px;
left: -1px;
right: -1px;
z-index: -1;
background-color: rgba(0, 0, 0, 0.1);
transform: skew(2deg) scale(1.2);
transition: all 0.2s ease;
opacity: 0;
}
.card:hover::before {
opacity: 1;
transform: skew(2deg) scale(1.3);
}
這段代碼使用了flex布局方式,實現了卡片在一行中的居中展現。同時,代碼中::before用於實現鼠標經過時的高亮展現,用戶體驗非常不錯。
四、CSS卡片疊加效果
除了基本的布局形式和交互效果外,卡片的疊加展現方式也是非常重要的一個方面。以下代碼實現了一種卡片疊加效果:
.card {
position: relative;
width: 300px;
height: 200px;
margin-right: 50px;
box-shadow: 0 2px 0 rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
cursor: pointer;
transform: rotate(0deg);
z-index: 1;
}
.card:hover {
transform: rotate(-10deg);
box-shadow: 2px 10px 50px rgba(0, 0, 0, 0.3);
z-index: 999999;
}
.card-wrapper .card:nth-of-type(2) {
position: absolute;
top: 50px;
right: -80px;
transform: rotate(-10deg);
z-index: 2;
transition: all 0.3s ease;
}
.card-wrapper .card:nth-of-type(2):hover {
transform: rotate(-20deg);
right: -40px;
z-index: 99999;
box-shadow: 2px 10px 50px rgba(0, 0, 0, 0.3);
}
這段代碼中,我們使用了transform屬性實現卡片在鼠標經過時的旋轉變形效果。同時,代碼中嵌套使用了nth-of-type選擇器,實現了兩張卡片的疊加效果。需要注意的是,卡片的z-index屬性需要進行適當的設置,避免遮擋問題。
五、CSS卡片樣式
卡片的樣式非常多樣化,下面是一種漂亮的卡片效果展現,可以為頁面提供很好的視覺效果:
.card-wrapper {
display: flex;
flex-wrap: wrap;
}
.card {
position: relative;
width: 300px;
height: 200px;
margin-right: 50px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease;
cursor: pointer;
overflow: hidden;
transform-style: preserve-3d;
}
.card::before {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-image: linear-gradient(160deg, #f30, #fc0, #ff0);
opacity: 0.8;
z-index: 1;
transform: translateX(-100%);
transition: all 0.3s ease;
}
.card:hover::before {
transform: translateX(0%);
}
.card .card-inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform-style: preserve-3d;
}
.card .card-back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #f7f7f7;
transform: rotateY(180deg);
}
.card:hover .card-inner {
transform: rotateY(-180deg);
}
.card .card-front {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: linear-gradient(160deg, #fff, #f7f7f7);
}
.card-caption {
position: absolute;
top: 130px;
left: 20px;
z-index: 2;
}
.card-caption h3 {
margin: 0;
font-size: 20px;
font-weight: 700;
color: #282828;
text-transform: uppercase;
letter-spacing: 2px;
}
.card-caption p {
margin: 10px 0 0 0;
font-size: 14px;
line-height: 1.5;
color: #282828;
}
該代碼使用了transform-style屬性實現了卡片正反面文字內容展現,同時使用了背景漸變色,組合box-shadow陰影效果,使卡片呈現出濃郁質感。需要注意的是,卡片內部的內容需要使用.card-inner進行包裹,將卡片正反面分別展現出來。
六、CSS卡片框架
卡片框架是卡片呈現效果的基礎,下面是一個基礎的卡片框架:
.card {
position: relative;
margin-bottom: 50px;
overflow: hidden;
min-width: 280px;
height: 420px;
margin-right: 24px;
background: #fff;
border: 1px solid #f1f1f1;
box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.12);
transition: 0.3s;
}
.card:hover {
box-shadow: 0px 4px 16px rgba(0, 0, 0, 0.16);
}
.card .card-header {
width: 100%;
height: 160px;
position: relative;
overflow: hidden;
}
.card .card-header img {
width: 100%;
height: 100%;
object-fit: cover;
z-index: -1;
}
.card .card-body {
padding: 24px;
text-align: center;
}
.card .card-body h2 {
font-size: 36px;
font-weight: 600;
margin-bottom: 12px;
color: #282828;
}
.card .card-body p {
font-size: 18px;
line-height: 1.6;
color: #7a7a7a;
margin-bottom: 24px;
}
.card .card-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 24px;
background: #f7f7f7;
border-top: 1px solid #f1f1f1;
position: absolute;
bottom: 0;
width: 100%;
}
.card .card-footer .card-author {
font-size: 14px;
font-weight: 600;
color: #282828;
text-transform: uppercase;
}
.card .card-footer .card-date {
font-size: 14px;
color: #7a7a7a;
}
該代碼使用了基礎的CSS樣式,呈現出了一種簡約、大氣的卡片效果。卡片框架是卡片效果的基礎,無論是翻轉、層疊還是其他效果都必須基於卡片框架而展開。
七、CSS圖片效果
圖片效果是卡片展現效果的一個重要方面,下面是一個基礎的圖片卡片效果:
.card {
position: relative;
margin-bottom: 50px;
overflow: hidden;
min-width: 280px;
height: 420px;
margin-right: 24px;
background: #fff;
border: 1px solid #f1f1f1;
box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.12);
transition: 0.3s;
}
.card:hover {
box-shadow: 0px 4px 16px rgba(0, 0, 0, 0.16);
}
.card .card-header {
width: 100%;
height: 160px;
position: relative;
overflow: hidden;
display: flex;
}
.card .card-header img {
width: 100%;
height: 100%;
object原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/235881.html
微信掃一掃
支付寶掃一掃