扩展卡(Expanding Cards)

在这里插入图片描述
来源github,参考链接50 Projects In 50 Days - HTML, CSS & JavaScript

重点:
1、使用flex布局
2、使用[].filter.call方法

实现代码:
index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Expanding Cards</title>
  <link rel="stylesheet" href="../css/index.css">
</head>
<body>
  <div class="container">
    <div class="panel active" style="background-image: url('https://images5.alphacoders.com/706/thumb-1920-706467.jpg');"><h3>Lionel Messi 1</h3></div>
    <div class="panel" style="background-image: url('https://images3.alphacoders.com/260/thumb-1920-260247.jpg');"><h3>Lionel Messi 2</h3></div>
    <div class="panel" style="background-image: url('https://images8.alphacoders.com/571/thumb-1920-571558.jpg');"><h3>Lionel Messi 3</h3></div>
    <div class="panel" style="background-image: url('https://images4.alphacoders.com/244/thumb-1920-244338.jpg');"><h3>Lionel Messi 4</h3></div>
    <div class="panel" style="background-image: url('https://images3.alphacoders.com/183/thumb-1920-183967.jpg');"><h3>Lionel Messi 5</h3></div>
    <div class="panel" style="background-image: url('https://images5.alphacoders.com/521/thumb-1920-521476.jpg');"><h3>Lionel Messi 6</h3></div>
</div>
</body>
<script src="../js/index.js"></script>
</html>

index.css

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body,html{
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}
.container{
  display: flex;
  width: 90vw;
}
.container > .panel{
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  flex: 0.5;
  padding: 10px;
  color: #fff;
  border-radius: 50px;
  margin: 10px;
  transition:  flex .5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  position: relative;
  cursor: pointer;
  height: 90vh;
}
.container > .panel > h3{
  color: aquamarine;
  font-size: 25px;
  text-align: center;
  line-height: 90vh;
}
.container > .panel.active{
  flex: 5;
}

index.js

const panelItems = document.querySelectorAll(".container > .panel");
panelItems.forEach((item) => {
  item.addEventListener("click", () => {
    [].filter.call(item.parentElement.children, el => el !== item).forEach(el => el.classList.remove("active"));
    item.classList.add("active");
  })
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值