DAY12-问题解答折叠卡片

前言

这个项目来源于GitHub上的一个开源项目https://github.com/bradtraversy/50projects50days,总共有50个用来练手的前端项目,我学习然后复现效果,并记录学习笔记和心得。

效果展示

faq-collapse

HTML&CSS

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>faq-collapse</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0">
    <script src="https://kit.fontawesome.com/4f492bdd9a.js" crossorigin="anonymous"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #f0f0f0;
            display: flex;
            justify-content: flex-start;
            flex-direction: column;
            align-items: center;
        }

        body, html {
            height: 100%;
        }

        .faq {
            list-style: none;
            display: flex;
            flex-direction: column;
            gap: 20px;
            width: 650px;
        }

        .title {
            font-weight: normal;
            font-size: 2rem;
            margin: 30px 0;
        }

        .faq li {
            border: 1px solid #9fa4a8;
            border-radius: 5px;
            padding: 30px;
            transition: all 300ms;
        }

        .question{
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        li i {
            cursor: pointer;
        }

        li .fa-angle-down{
            transform: translateX(-4px);
        }

        li .fa-circle-xmark{
            display: none;
        }

        li p{
            display: none;
            margin-top: 30px;
            font-size: 14px;
            user-select: none;
        }

        .expanded .fa-angle-down {
            display: none;
        }

        .expanded .fa-circle-xmark{
            display: block;
            color: #9fa4a8;
            font-size: 1.5rem;
        }

        .expanded p {
            display: block;
        }

        .expanded{
            background-color: #fff;
            box-shadow: -2px 5px 8px #cacaca;
            position: relative;
            overflow: hidden;
        }

        .expanded::after{
            content: '\f075';
            font-family: 'Font Awesome 5 Free';
            color: #2ecc71;
            font-size: 7rem;
            position: absolute;
            opacity: 0.2;
            top: 20px;
            left: 20px;
            z-index: 0;
        }
        .expanded::before{
            content: '\f075';
            font-family: 'Font Awesome 5 Free';
            color: #d6eaf8;
            font-size: 7rem;
            position: absolute;
            opacity: 0.5;
            top: -8px;
            left: -31px;
            z-index: 0;
            transform: rotateY(180deg);
        }
    </style>
</head>
<body>
<h1 class="title">Frequently Asked Questions</h1>
<ul class="faq">
    <li>
        <div class="question">
            <h3>Why shouldn't we trust atoms?</h3>
            <i class="fa-solid fa-angle-down"></i>
            <i class="fa-solid fa-circle-xmark"></i>
        </div>
        <p>They make up everything</p>
    </li>
    <li>
        <div class="question">
            <h3>What do you call someone with no body and no nose?</h3>
            <i class="fa-solid fa-angle-down"></i>
            <i class="fa-solid fa-circle-xmark"></i>
        </div>
        <p>Nobody knows</p>
    </li>
    <li>
        <div class="question">
            <h3>What's the object-oriented way to become wealthy?</h3>
            <i class="fa-solid fa-circle-xmark"></i>
            <i class="fa-solid fa-angle-down"></i>
        </div>
        <p>Inheritance</p>
    </li>
    <li>
        <div class="question">
            <h3>How many tickles does it take to tickle an octopus?</h3>
            <i class="fa-solid fa-angle-down"></i>
            <i class="fa-solid fa-circle-xmark"></i>
        </div>
        <p>Ten-tickles!</p>
    </li>
    <li>
        <div class="question">
            <h3>What is: 1 + 1?</h3>
            <i class="fa-solid fa-angle-down"></i>
            <i class="fa-solid fa-circle-xmark"></i>
        </div>
        <p>Depends on who are you asking.</p>
    </li>
</ul>
</body>
</html>

JS

let liList = document.querySelectorAll('.faq li');
liList.forEach(li =>{
    li.addEventListener('click',function (e){
        if (e.target.classList.contains('fa-solid')){
            li.classList.toggle('expanded')
        }
    })
})

总结

这个案例其实不难,FontAwesome的伪元素使用方式值得学习一下。


DAY12-问题解答折叠卡片
https://www.zhaojun.inkhttps://www.zhaojun.ink/archives/day12-faq-collapse
作者
卑微幻想家
发布于
2022-12-28
许可协议