使用JS和CSS制作的小案例(day一)

一、写在开头

本项目是从github上摘取,自己练习使用后分享,方便登录github的小伙伴可以看本篇文章

50项目50天icon-default.png?t=N7T8https://github.com/bradtraversy/50projects50days有兴趣的小伙伴可以自己去github上查看。

二、项目开始

开始展示的是第一张图片,鼠标点击之后,对应的图片所占据的大小变化,达到展开的效果

这里,我先贴出对应的代码,在带大家一点点剖析一下。

html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="index.css">
    <title>扩展卡片</title>
</head>
<body>
    <!-- 扩展卡片开始 -->
        <div class="container">
            <div class="panel active" style="background-image: url(./images/test1.jpg);">
                <h3>Explore The World</h3>
            </div>
            <div class="panel" style="background-image: url(./images/test2.jpg);">
                <h3>Wild Forest</h3>
            </div>
            <div class="panel" style="background-image: url(./images/test3.jpg);">
                <h3>Sunny Beach</h3>
            </div>
            <div class="panel" style="background-image: url(./images/test4.png);">
                <h3>City on Winter</h3>
            </div>
            <div class="panel" style="background-image: url(./images/test5.jpg);">
                <h3>Mountains - Clouds</h3>
            </div>
        </div>
    <!-- 扩展卡片结束 -->
    <script src="index.js"></script>
</body>
</html>

css结构

* {
    box-sizing: border-box;
}

body {
    font-family: 'Muli', sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    overflow: hidden;
    margin: 0;
}

.container {
    display: flex;
    width: 90vw;
}
.panel {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    height: 80vh;
    border-radius: 50px;
    color: #fff;
    cursor: pointer;
    flex: 0.5;
    margin: 10px;
    position: relative;
    transition: all 700ms ease-in;
}

.panel h3{
    font-size: 24px;
    position: absolute;
    bottom: 20px;
    left: 20px;
    margin: 0;
    padding: 0;
}  
.panel.active {
    flex: 5;
}
.panel.active h3 {
    opacity: 1;
    transition: opacity 0.3s ease-in 0.4s;
}
@media (max-width: 480px) {
    .container {
      width: 100vw;
    }
  
    .panel:nth-of-type(4),
    .panel:nth-of-type(5) {
      display: none;
    }
  }

js代码

const panels = document.querySelectorAll('.panel');

panels.forEach(panel => {
    panel.addEventListener('click', () => {
        removeActiveClasses();
        panel.classList.add('active');
    });
})

function removeActiveClasses(){
    panels.forEach(panel => {
        panel.classList.remove('active');
    })
}

三、具体解析

html结构

css

js

四、最后

希望这个案例可以帮助一些刚接触html、css、JavaScript的小伙伴。学习知识很重要,但是必要的练习是必不可少的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有一个挚爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值