CSS100Day(26)-激励卡片切换

812 字
4 分钟
CSS100Day(26)-激励卡片切换

设计简介#

关键点如下

  1. 知识点1:没有什么知识点,注意动画的实现,分别两个进入一个退出一个,通过activeinactive类样式

效果展示#

1
The couch

If you want to grow, get outside
your comfort zone.

切换
2
Failing is learning

Pick yourself up, dust yourself off, and start again.

切换
3
Flowers and rainbows

Always be yourself,
unless you can be a unicorn.

切换

实现代码和步骤#

HTML部分#

<div class="frame">
<div class="card active">
<div class="number-bg"></div>
<div class="number">1</div>
<div class="text">
<span class="headline">The couch</span>
<p>
If you want to grow, get outside<br />
your comfort zone.
</p>
</div>
<div class="button">切换</div>
</div>
<div class="card">
<div class="number-bg"></div>
<div class="number">2</div>
<div class="text">
<span class="headline">Failing is learning</span>
<p>Pick yourself up, dust yourself off, and start again.</p>
</div>
<div class="button">切换</div>
</div>
<div class="card">
<div class="number-bg"></div>
<div class="number">3</div>
<div class="text">
<span class="headline">Flowers and rainbows</span>
<p>
Always be yourself,<br />
unless you can be a unicorn.
</p>
</div>
<div class="button">切换</div>
</div>
</div>

CSS部分#

.frame {
box-shadow: 1px 2px 10px 0 rgba(0, 0, 0, 0.3);
width: 400px;
height: 400px;
border-radius: 2px;
background: #c7d6e1;
overflow: hidden;
display: grid;
place-content: center;
}
.card {
pointer-events: none;
background: #fff;
border-radius: 2px;
width: 240px;
height: 150px;
position: absolute;
top: 125px;
left: 80px;
box-shadow: 4px 8px 12px 0 rgba(0, 0, 0, 0.2);
opacity: 0;
overflow: hidden;
&.active {
pointer-events: all;
z-index: 1;
animation: fadeIn 0.6s ease-in-out both 0.5s;
}
&.inactive {
pointer-events: none;
z-index: 2;
animation: fadeOut 0.5s ease-in-out both;
}
.number-bg {
position: absolute;
width: 60px;
height: 60px;
top: -31px;
right: -31px;
background: #8391a1;
transform: rotate(45deg);
}
.number {
position: absolute;
top: 6px;
right: 8px;
font-size: 13px;
line-height: 13px;
font-weight: 600;
color: #fff;
}
.text {
text-align: center;
padding: 20px 10px;
.headline {
display: block;
font-size: 14px;
line-height: 22px;
font-weight: 600;
color: #616e7d;
margin-bottom: 8px;
}
p {
font-size: 12px;
line-height: 17px;
color: #8391a1;
margin: 0;
}
}
.button {
position: absolute;
width: 100%;
height: 40px;
bottom: 0;
left: 0;
right: 0;
background: #3ca7fb;
cursor: pointer;
text-align: center;
text-transform: uppercase;
line-height: 38px;
font-size: 13px;
color: #fff;
transition: all 0.3s ease-in-out;
&:hover {
background: #2196f3;
}
}
}
@keyframes fadeIn {
from {
opacity: 0;
transform: scale(0.9);
}
to {
opacity: 1;
transform: scale(1);
}
}
@keyframes fadeOut {
from {
opacity: 1;
transform: scale(1);
}
to {
opacity: 0;
transform: scale(1.15);
}
}

JS部分#

<script>
let active = 1 const buttons = document.querySelectorAll(".button") buttons.forEach(function (button){" "}
{button.addEventListener("click", function () {
var currentCard = document.querySelector(".card:nth-child(" + active + ")")
if (currentCard) {
currentCard.classList.remove("active")
currentCard.classList.add("inactive")
}
if (active === 3) {
active = 0
}
active++
var nextCard = document.querySelector(".card:nth-child(" + active + ")")
if (nextCard) {
nextCard.classList.remove("inactive")
nextCard.classList.add("active")
}
})}
)
</script>
分享给其他人
如果该文章对您有帮助,欢迎分享给其他人阅读!
复制链接

评论区

图片
犟哟
Hello, I'm Juyial.
公告
欢迎来到我的博客,在这里可以一起进步,一起学习!2026年起航!
分类
站点统计
文章
26
分类
1
标签
3
总字数
47,878
运行时长
0
最后活动
0 天前

目录