html+css+js 50个小项目之第一个 卡片

本项目需要掌握的技术要点:

1.flex布局

2.transition过度

3.js获取元素,动态添加和删除类名

 1.html

外面一个cotainer大盒子,container盒子里面有一个panel盒子和一个h3标签

代码如下:

<body>
  <div class="container">
    <div class="panel active" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F1110200ZS0%2F2011100ZS0-12-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666522242&t=2336a2a0c5838db0f0852ff76dcae54b')">
      <h3>explore</h3>
    </div>
    <div class="panel " style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F1110200ZS0%2F2011100ZS0-12-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666522242&t=2336a2a0c5838db0f0852ff76dcae54b')">
      <h3>explore</h3>
    </div>
    <div class="panel " style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F1110200ZS0%2F2011100ZS0-12-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666522242&t=2336a2a0c5838db0f0852ff76dcae54b')">
      <h3>explore</h3>
    </div>
    <div class="panel " style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F1110200ZS0%2F2011100ZS0-12-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666522242&t=2336a2a0c5838db0f0852ff76dcae54b')">
      <h3>explore</h3>
    </div>
    <div class="panel " style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F1110200ZS0%2F2011100ZS0-12-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666522242&t=2336a2a0c5838db0f0852ff76dcae54b')">
      <h3>explore</h3>
    </div>
  </div>
</body>

2.css

* {
  /* 设置为标准盒模型 */
  box-sizing: border-box;
}
body {
  /* 给body设置为弹性布局 */
  display: flex;
  /* 子元素能够水平居中 */
  justify-content: center;
  /* 子元素能够垂直居中 */
  align-items: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}
.container {
  /* 给container设置为弹性布局 */
  display: flex;
  width: 90vw;
}
.panel {
  /* 等比例缩放 */
  background-size: cover;
  background-repeat: no-repeat;
  /* 背景图片位置居中 */
  background-position: center;
  height: 80vh;
  border-radius: 100px;
  cursor: pointer;
  margin: 10px;
  flex: 1;
  position: relative;
  /* 过度 */
  transition: all 1s ease-in;
}
.panel h3 {
  font-size: 26px;
  position: absolute;
  left: 20px;
  bottom: 0;
  opacity: 0;
}
.panel.active {
  flex:5
}
.panel.active h3 {
  opacity: 1;
  transition:opacity 1s ease-in;
}

3.js

// 1.获取元素
const panels = document.querySelectorAll('.panel')
// 遍历数组,为每个元素添加点击事件
panels.forEach(item => {
  item.addEventListener('click',function() {
    // 先删除active类
    removeClass()
    // 再添加active类
    item.classList.add('active')
});
  })
  
function removeClass() {
  panels.forEach(panel=>{
    panel.classList.remove('active')
  })
}

4.展示

已展示 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值