前端小案例——选项卡(HTML+CSS+JS, 附源码)

本文介绍了如何在HTML和JavaScript中创建一个可切换的选项卡功能,通过点击按钮隐藏和显示不同内容区域,同时展示了CSS样式的应用和事件监听器的使用。
摘要由CSDN通过智能技术生成

一、前言

实现功能:

        切换选项卡内容:点击不同的选项卡按钮时,会隐藏之前显示的选项卡内容,并显示当前点击按钮对应的选项卡内容。

实现逻辑:

  1. 在 HTML 中创建了两个ul盒子,分别用于显示选项卡按钮和选项卡内容。

  2. 默认显示第一个选项卡内容,即将第一个选项卡内容的 display 属性设置为 block。

  3. 通过 forEach 循环遍历选项卡按钮,给每个按钮添加点击事件监听器,当点击某个按钮时,隐藏之前显示的选项卡内容,然后显示当前点击按钮对应的选项卡内容。

  4. 给选项卡按钮添加样式,使其在被选中时呈现不同的效果。

二、项目运行效果

 三、全部代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选项卡</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        text-decoration: none;
        list-style: none;
        background-repeat: no-repeat;
    }
    .choice-box{
        display: flex;
        justify-content: space-between;
        position: relative;
        width: 700px;
        height: 500px;
        margin: 5% auto;
    }
    .choice{
        float: left;
        width: 14%;
        height: 100%;
        display: flex;
        flex-wrap: nowrap;
        flex-direction: column;
        justify-content: space-around;
    }
    .choice li a{
        display: block;
        width: 100%;
        height: 100%;
        font-size: 22px;
        color: #000;
        transition: all 0.2s linear;
        cursor: pointer;
    }
    .choice-con{
        position: relative;
        width: 80%;
        height: 100%;
    }
    .choice-con li{
        display: none;
        width: 100%;
        height: 100%;
        position: absolute;
        border-radius: 5%;
    }
    .choice-con li p{
        line-height: 500px;
        text-align: center;
        font-size: 28px;
        color: #fff;
    }
</style>
<body>
    <div class="choice-box">
        <ul class="choice">
            <li>
                <a class="choice-1">粉色</a>
            </li>
            <li>
                <a class="choice-2">绿色</a>            
            </li>
            <li>
                <a class="choice-3">蓝色</a>
            </li>
            <li>
                <a class="choice-4">紫色</a>
            </li>
            <li>
                <a class="choice-5">红色</a>
            </li>
            <li>
                <a class="choice-6">黑色</a>
            </li>
        </ul>

        <ul class="choice-con">
            <!-- 下面li标签可以添加自己的内容和样式 -->
            <li id="choice-1" style="background-color: pink">
                <p>我是粉色!</p>
            </li>
            <li id="choice-2" style="background-color: aquamarine;">
                <p>我是绿色!</p>
            </li>
            <li id="choice-3" style="background-color: #859aff;">
                <p>我是蓝色!</p>
            </li>
            <li id="choice-4" style="background-color: blueviolet;">
                <p>我是紫色!</p>
            </li>
            <li id="choice-5" style="background-color: tomato;">
                <p>我是红色!</p>
            </li>
            <li id="choice-6" style="background-color: #000;">
                <p>我是黑色!</p>
            </li>
        </ul>
    </div>
</body>
<script>
    let choice1 = document.querySelector('#choice-1');
    choice1.style.display = 'block';

    const choice = document.querySelector('.choice');
    let choiceLis = choice.querySelectorAll('li');
    choiceLis.forEach(function (li) {
        li.addEventListener('click', function () {
            if (choice1) {
                choice1.style.display = 'none';
            }
            let aHref = this.querySelector('a').getAttribute('class');
            let contID = aHref.slice(0);
            let contDIv = document.getElementById(contID);
            choice1 = contDIv;
            contDIv.style.display = 'block';
        })
    })

    let choiceAs = document.querySelectorAll('.choice a');
    console.log(choiceAs);
    choiceAs.forEach(function (a, index) {
        a.addEventListener('click', function () {
            choiceAs.forEach(function (link) {
                link.style.fontSize = '';
                link.style.letterSpacing ='';
            });
            this.style.fontSize = '35px';
            this.style.letterSpacing ='10px';
        });
        // 判断是否是第一个 <a> 标签
        if (index === 0) { 
            a.style.fontSize = '35px';
            a.style.letterSpacing ='10px';
        }
    });
</script>
</html>

四、答疑解惑

        这是一个非常适合练习JavaScript的小案例,各位小伙伴可以自行更改样式和内容,如果大家运行时出现问题或代码有什么不懂的地方都可以随时评论留言或联系博主。           

        还多请各位小可爱多多点赞支持,你们的支持是我最大的动力。

博主VX:18884281851

谢谢各位的支持~~

  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,以下是一个简单的前端HTML + CSS + JS注册和登录案例HTML代码: ``` <!DOCTYPE html> <html> <head> <title>注册和登录</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>欢迎注册和登录</h1> <div class="container"> <div class="form-container"> <h2>注册</h2> <form id="register-form" onsubmit="return validateRegisterForm()"> <label for="username">用户名:</label> <input type="text" name="username" id="username"> <label for="password">密码:</label> <input type="password" name="password" id="password"> <label for="email">电子邮件:</label> <input type="email" name="email" id="email"> <button type="submit">注册</button> </form> </div> <div class="form-container"> <h2>登录</h2> <form id="login-form" onsubmit="return validateLoginForm()"> <label for="username">用户名:</label> <input type="text" name="username" id="login-username"> <label for="password">密码:</label> <input type="password" name="password" id="login-password"> <button type="submit">登录</button> </form> </div> </div> <script src="script.js"></script> </body> </html> ``` CSS代码: ``` body { font-family: Arial, Helvetica, sans-serif; background-color: #f4f4f4; } h1 { text-align: center; margin-top: 50px; } .container { display: flex; flex-direction: row; justify-content: center; align-items: center; margin-top: 50px; } .form-container { background-color: #fff; padding: 20px; margin: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.3); } label { display: block; margin-bottom: 5px; } input[type="text"], input[type="password"], input[type="email"] { width: 100%; padding: 10px; margin-bottom: 10px; border: none; border-radius: 5px; background-color: #f4f4f4; } button[type="submit"] { background-color: #4CAF50; color: #fff; padding: 10px; border: none; border-radius: 5px; cursor: pointer; } button[type="submit"]:hover { background-color: #3e8e41; } ``` JavaScript代码: ``` function validateRegisterForm() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; var email = document.getElementById("email").value; if (username == "") { alert("请输入用户名"); return false; } if (password == "") { alert("请输入密码"); return false; } if (email == "") { alert("请输入电子邮件"); return false; } alert("注册成功!"); return true; } function validateLoginForm() { var username = document.getElementById("login-username").value; var password = document.getElementById("login-password").value; if (username == "") { alert("请输入用户名"); return false; } if (password == "") { alert("请输入密码"); return false; } alert("登录成功!"); return true; } ``` 这个案例包含一个注册表单和一个登录表单。当用户提交表单时,JavaScript代码会验证输入是否有效,并根据结果显示成功或失败消息。您可以将此代码作为起点,进行更多的开发和改进,以实现更完整的注册和登录功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

花花´◡`

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

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

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

打赏作者

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

抵扣说明:

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

余额充值