实现一个移动端影院选座位的功能
时间: 2023-12-15 21:06:52 浏览: 67
好的,下面是一个简单的实现移动端影院选座位的功能的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>影院选座位</title>
<style>
.screen {
text-align: center;
font-size: 24px;
margin-bottom: 20px;
}
.row {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 10px;
}
.seat {
width: 40px;
height: 40px;
display: flex;
justify-content: center;
align-items: center;
margin-right: 10px;
background-color: #eee;
color: #333;
border-radius: 5px;
cursor: pointer;
}
.seat.selected {
background-color: #f44336;
color: #fff;
}
.info {
text-align: center;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="screen">屏幕</div>
<div class="seats">
<div class="row">
<div class="seat" data-row="A" data-col="1">1</div>
<div class="seat" data-row="A" data-col="2">2</div>
<div class="seat" data-row="A" data-col="3">3</div>
<div class="seat" data-row="A" data-col="4">4</div>
<div class="seat" data-row="A" data-col="5">5</div>
</div>
<div class="row">
<div class="seat" data-row="B" data-col="1">1</div>
<div class="seat" data-row="B" data-col="2">2</div>
<div class="seat" data-row="B" data-col="3">3</div>
<div class="seat" data-row="B" data-col="4">4</div>
<div class="seat" data-row="B" data-col="5">5</div>
</div>
<div class="row">
<div class="seat" data-row="C" data-col="1">1</div>
<div class="seat" data-row="C" data-col="2">2</div>
<div class="seat" data-row="C" data-col="3">3</div>
<div class="seat" data-row="C" data-col="4">4</div>
<div class="seat" data-row="C" data-col="5">5</div>
</div>
<div class="row">
<div class="seat" data-row="D" data-col="1">1</div>
<div class="seat" data-row="D" data-col="2">2</div>
<div class="seat" data-row="D" data-col="3">3</div>
<div class="seat" data-row="D" data-col="4">4</div>
<div class="seat" data-row="D" data-col="5">5</div>
</div>
</div>
<div class="info">请先选择座位</div>
<script>
const seats = document.querySelectorAll('.seat');
const info = document.querySelector('.info');
const selectedSeats = [];
seats.forEach(seat => {
seat.addEventListener('click', () => {
const row = seat.dataset.row;
const col = seat.dataset.col;
const seatNum = `${row}${col}`;
if (selectedSeats.includes(seatNum)) {
selectedSeats.splice(selectedSeats.indexOf(seatNum), 1);
seat.classList.remove('selected');
} else {
selectedSeats.push(seatNum);
seat.classList.add('selected');
}
if (selectedSeats.length > 0) {
info.innerHTML = `已选座位:${selectedSeats.join(',')}`;
} else {
info.innerHTML = '请先选择座位';
}
});
});
</script>
</body>
</html>
```
这个示例代码中,我们使用了HTML、CSS和JavaScript来实现影院选座位的功能。座位的位置和状态使用了HTML的data-*属性来保存,点击座位时,使用JavaScript来处理座位的选择和取消选择,同时更新座位信息显示。在实际项目中,还需要考虑更多的细节,比如座位的状态(已售、已选等)、座位的价格、座位的类型(普通座、VIP座等)等。
阅读全文