当前位置:首页 > VUE

vue实现影院选座

2026-02-24 15:47:43VUE

Vue实现影院选座功能

影院选座功能通常需要结合UI交互、数据管理和状态同步。以下是一个基于Vue的实现方案:

数据结构设计

选座功能的核心是座位布局数据,建议使用二维数组表示:

data() {
  return {
    seats: [
      [{id: 'A1', status: 'available'}, {id: 'A2', status: 'available'}],
      [{id: 'B1', status: 'unavailable'}, {id: 'B2', status: 'available'}]
    ],
    selectedSeats: []
  }
}

座位渲染组件

创建可复用的座位组件,根据状态显示不同样式:

vue实现影院选座

<template>
  <div 
    class="seat"
    :class="[seat.status, {selected: isSelected}]"
    @click="handleClick">
    {{ seat.id }}
  </div>
</template>

<script>
export default {
  props: ['seat', 'selectedSeats'],
  computed: {
    isSelected() {
      return this.selectedSeats.includes(this.seat.id)
    }
  },
  methods: {
    handleClick() {
      if (this.seat.status !== 'unavailable') {
        this.$emit('select', this.seat.id)
      }
    }
  }
}
</script>

选座逻辑实现

在父组件中处理座位选择和取消选择:

methods: {
  toggleSeat(seatId) {
    const index = this.selectedSeats.indexOf(seatId)
    if (index > -1) {
      this.selectedSeats.splice(index, 1)
    } else {
      this.selectedSeats.push(seatId)
    }
  }
}

屏幕和座位布局

完整的影院布局应包括屏幕和座位区:

vue实现影院选座

<template>
  <div class="cinema">
    <div class="screen">荧幕</div>
    <div class="seats-container">
      <div v-for="(row, rowIndex) in seats" :key="rowIndex" class="row">
        <div class="row-label">{{ String.fromCharCode(65 + rowIndex) }}</div>
        <Seat 
          v-for="seat in row" 
          :key="seat.id"
          :seat="seat"
          :selected-seats="selectedSeats"
          @select="toggleSeat"/>
      </div>
    </div>
  </div>
</template>

样式设计

CSS样式应明确区分不同状态的座位:

.seat {
  width: 30px;
  height: 30px;
  margin: 5px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  border-radius: 4px;
}

.available {
  background-color: #e0e0e0;
}

.unavailable {
  background-color: #f44336;
  cursor: not-allowed;
}

.selected {
  background-color: #4caf50;
}

.screen {
  width: 80%;
  height: 20px;
  margin: 20px auto;
  background: #2196f3;
  color: white;
  text-align: center;
}

.row {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.row-label {
  width: 20px;
  margin-right: 10px;
}

数据持久化

与后端API交互保存选座结果:

methods: {
  async confirmSelection() {
    try {
      const response = await axios.post('/api/reserve', {
        seats: this.selectedSeats
      })
      // 处理成功响应
    } catch (error) {
      // 处理错误
    }
  }
}

优化考虑

  1. 添加座位间距和排列优化,模拟真实影院布局
  2. 实现连座优先选择逻辑
  3. 添加选座限制(最多可选座位数)
  4. 考虑响应式设计适应不同屏幕尺寸
  5. 添加选座动画效果提升用户体验

这个实现方案提供了影院选座的核心功能,可根据实际需求进一步扩展和完善。

标签: 影院vue
分享给朋友:

相关文章

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现闪烁

vue实现闪烁

Vue实现元素闪烁效果 使用CSS动画实现 通过Vue绑定class结合CSS动画实现闪烁效果,代码简洁且性能较好。 <template> <div :class="{…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…