前端小案例——购买电影票(HTML+CSS+JS, 附源码)

一、前言

实现功能:

这段代码实现了一个简单的电影票选座购买的功能界面。

  1. 在页面上展示了一个电影院的座位布局,以及右侧显示了电影信息、选座情况、票价、总计等内容。

  2. 用户可以通过点击座位来选择购买电影票,每个座位的状态会在点击时改变(从默认颜色变为红色),并在右侧显示已选座位的信息。

  3. 用户可以确认购买,确认购买后会出现购买成功的提示,并且页面会重置,清空已选座位和右侧信息。

实现逻辑:

  1. 首先,在 HTML 中定义了一个包含座位和右侧信息的页面结构。

  2. 使用 JavaScript 获取到需要操作的元素,包括座位列表、座位元素、默认座位颜色、选座数目、选座信息展示区域等。

  3. 使用循环创建了 64 个座位,并给每个座位添加了行列信息。同时初始化了默认座位颜色和选座数目变量。

  4. 定义了一个函数 addSeatClick,用于处理座位的点击事件。当座位被点击时,根据当前座位状态(默认或已选),改变座位颜色、更新选座数目、显示已选座位信息以及计算总金额等操作。

  5. 遍历所有座位元素,为每个座位添加点击事件处理函数。

  6. 监听确认购买按钮的点击事件。点击确认购买后,弹出购买成功提示,重置所有座位颜色,清空已选座位信息和总金额,并将选座数目重置为零。

二、项目运行效果

三、全部代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购买电影票</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
            list-style: none;
            background-repeat: no-repeat;
        }

        .con-box {
            width: 100%;
            height: 100vh;
            display: flex;
            justify-content: center;
            gap: 10px;
            margin: 0 auto;
        }

        .left {
            width: 60%;
            height: 100vh;
            border-right: 1px dashed #000;
        }

        .left p {
            width: 70%;
            height: 35px;
            line-height: 35px;
            text-align: center;
            margin: 30px auto;
            background-color: #f0f0f0;
            border-radius: 5px;
        }

        .seat {
            display: flex;
            width: 80%;
            height: 750px;
            flex-wrap: wrap;
            gap: 30px;
            justify-content: space-around;
            align-content: center;
            margin: 0 auto;
        }

        .seat li {
            width: 8%;
            height: 8%;
            border-radius: 5px;
            background-color: #b9ef9f;
            line-height: 60px;
            text-align: center;
            cursor: pointer;
        }

        .right {
            width: 35%;
            height: 100vh;
            color: #a79e9f;
            position: relative;
        }

        .right-con {
            width: 350px;
            height: 90vh;
            position: absolute;
            right: 0;
            top: 5%;
            line-height: 28px;
        }

        #seatNumbers {
            width: 240px;
        }

        .seat-number {
            display: inline-block;
            width: 100px;
            height: 30px;
            background-color: #efefef;
            border-radius: 5px;
            text-align: center;
            margin-left: 10px;
            margin-bottom: 10px;
            line-height: 30px;
            color: #000;
            border: #d1d1d1 1px solid;
        }

        .right-con button {
            width: 70px;
            height: 25px;
            margin: 25px 50px;
            background-color: #efefef;
            border: solid 0.5px #000;
            border-radius: 2px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="con-box">
        <div class="left">
            <p>屏幕</p>
            <ul class="seat"></ul>
        </div>

        <div class="right">
            <ul class="right-con">
                <li>影片:<span>后来的我们</span></li>
                <li>时间:<span>5月1日&nbsp;21:00</span></li>
                <li>票价:<span>50元</span></li>
                <li>座位:<p id="seatNumbers"></p>
                </li>
                <li>票数:<span></span></li>
                <li>总计:<span></span></li>
                <button>确认购买</button>
            </ul>
        </div>
    </div>

</body>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const seatList = document.querySelector('.seat');

        for (let i = 0; i < 64; i++) {
            const li = document.createElement('li');
            seatList.appendChild(li);
        }
        const seats = document.querySelectorAll('.seat li');
        const defaultColor = seats[0].style.backgroundColor;
        let selectedSeats = [];
        const seatNumbers = document.querySelector('#seatNumbers');

        for (let i = 1; i <= 8; i++) {
            for (let j = 1; j <= 8; j++) {
                let seatIndex = (i - 1) * 8 + (j - 1);
                seats[seatIndex].innerText = i + '-' + j;
            }
        }

        function addSeatClick(seat) {
            seat.addEventListener('click', () => {
                if (seat.style.backgroundColor === defaultColor) {
                    seat.style.backgroundColor = 'red';
                    selectedSeats.push(seat.innerText);

                    let numberP = document.createElement('p');
                    numberP.classList.add('seat-number');
                    seatNumbers.appendChild(numberP);

                    let row = seat.innerText.split('-')[0];
                    let column = seat.innerText.split('-')[1];
                    numberP.innerText = `${row}排${column}座`;

                    document.querySelector('.right-con li:nth-child(5) span').innerText = selectedSeats.length;
                    let total = selectedSeats.length * 50;
                    document.querySelector('.right-con li:nth-child(6) span').innerText = '¥' + total;
                } else {
                    seat.style.backgroundColor = defaultColor;

                    const seatIndex = selectedSeats.indexOf(seat.innerText);
                    if (seatIndex !== -1) {
                        selectedSeats.splice(seatIndex, 1);
                    }
                    seatNumbers.innerHTML = '';
                    selectedSeats.forEach(seat => {
                        let numberP = document.createElement('p');
                        numberP.classList.add('seat-number');
                        let seatInfo = document.createTextNode(`${seat.split('-')[0]}排${seat.split('-')[1]}座`);
                        numberP.appendChild(seatInfo);
                        seatNumbers.appendChild(numberP);
                    });

                    document.querySelector('.right-con li:nth-child(5) span').innerText = selectedSeats.length;
                    console.log('ss' + selectedSeats.length);
                    let total = selectedSeats.length * 50;
                    document.querySelector('.right-con li:nth-child(6) span').innerText = '¥' + total;
                }
            });
        }

        function but() {
            const confirmButton = document.querySelector('button');
            confirmButton.addEventListener('click', () => {
                alert('购买成功!');

                seats.forEach(seat => {
                    seat.style.backgroundColor = '#b9ef9f';
                });
                seatNumbers.innerHTML = '';
                selectedSeats = [];
                document.querySelector('.right-con li:nth-child(5) span').innerText = '';
                document.querySelector('.right-con li:nth-child(6) span').innerText = '';
            });
        }
        but()
        seats.forEach(seat => {
            addSeatClick(seat);
        });
    });

</script>
</html>

四、答疑解惑

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

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

博主VX:18884281851

谢谢各位的支持~~

                        
 

毕业设计是高等教育阶段学生在完成学业前所进行的一项重要学术任务,旨在检验学生通过学习所获得的知识、技能以及对特定领域的深刻理解能力。这项任务通常要求学生运用所学专业知识,通过独立研究和创新,完成一个实际问题的解决方案或者开展一项有价值的项目。 首先,毕业设计的选择通常由学生根据个人兴趣、专业方向以及实际需求来确定。学生需要在导师的指导下明确研究目标、问题陈述,确立研究的范围和深度。毕业设计可以包括文献综述、需求分析、方案设计、实施与测试等多个阶段,以确保整个过程的科学性和系统性。 其次,毕业设计的完成通常需要学生具备一定的独立思考和解决问题的能力。在研究过程中,学生可能需要采用各种研究方法,如实验、调查、案例分析等,以获取必要的数据和信息。通过这些活动,学生能够培养扎实的专业技能,提升解决实际问题的实际能力。 第三,毕业设计的撰写是整个过程的重要组成部分。学生需要将研究过程、方法、结果以及结论等详细记录在毕业论文中,以展示其研究的全貌和成果。同时,撰写毕业设计还有助于提高学生的学术写作水平,培养清晰、逻辑的表达能力。 最后,毕业设计的评价通常由导师和相关专业人士进行。评价标准包括研究的创新性、实用性、方法的科学性以及论文的质量等方面。学生在毕业设计中获得的成绩也将直接影响其最终的学业成绩和学位授予。 总的来说,毕业设计是高等教育中的一项重要环节,通过此过程,学生不仅能够巩固所学知识,还能培养独立思考和解决问题的能力,为将来的职业发展奠定坚实的基础。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

花花´◡`

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

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

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

打赏作者

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

抵扣说明:

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

余额充值