当前位置:首页 > VUE

vue实现电影排行

2026-02-18 09:26:55VUE

实现电影排行的Vue组件

使用Vue实现电影排行功能可以通过以下步骤完成。假设使用Vue 3和Composition API。

数据准备

创建一个电影数据数组,包含电影名称、评分和封面图等信息:

const movies = [
  { id: 1, title: '肖申克的救赎', rating: 9.7, cover: 'url1' },
  { id: 2, title: '教父', rating: 9.6, cover: 'url2' },
  { id: 3, title: '黑暗骑士', rating: 9.5, cover: 'url3' }
]

组件结构

创建电影排行组件的基本结构:

vue实现电影排行

<template>
  <div class="movie-ranking">
    <h3>电影排行榜</h3>
    <div class="ranking-list">
      <div 
        v-for="(movie, index) in sortedMovies" 
        :key="movie.id" 
        class="movie-item"
      >
        <span class="rank">{{ index + 1 }}</span>
        <img :src="movie.cover" :alt="movie.title">
        <div class="movie-info">
          <h4>{{ movie.title }}</h4>
          <div class="rating">
            <span>评分: {{ movie.rating }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

排序逻辑

在script部分实现按评分排序的逻辑:

import { ref, computed } from 'vue'

export default {
  setup() {
    const movies = ref([
      // 电影数据
    ])

    const sortedMovies = computed(() => {
      return [...movies.value].sort((a, b) => b.rating - a.rating)
    })

    return { sortedMovies }
  }
}

样式设计

添加基本CSS样式美化排行列表:

vue实现电影排行

.movie-ranking {
  max-width: 800px;
  margin: 0 auto;
}

.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.movie-item {
  display: flex;
  align-items: center;
  background: #f5f5f5;
  padding: 10px;
  border-radius: 8px;
}

.rank {
  font-size: 24px;
  font-weight: bold;
  margin-right: 20px;
  color: #ff9800;
}

.movie-item img {
  width: 80px;
  height: 120px;
  object-fit: cover;
  margin-right: 20px;
}

.movie-info {
  flex: 1;
}

.rating {
  color: #ff9800;
  font-weight: bold;
}

动态数据获取

如果需要从API获取数据,可以使用axios:

import { ref, onMounted } from 'vue'
import axios from 'axios'

export default {
  setup() {
    const movies = ref([])

    const fetchMovies = async () => {
      try {
        const response = await axios.get('API_URL')
        movies.value = response.data
      } catch (error) {
        console.error('获取电影数据失败:', error)
      }
    }

    onMounted(fetchMovies)

    return { movies }
  }
}

添加交互功能

可以增加点击事件查看电影详情:

<div 
  v-for="(movie, index) in sortedMovies" 
  :key="movie.id" 
  class="movie-item"
  @click="showDetail(movie)"
>
const showDetail = (movie) => {
  // 跳转到详情页或显示模态框
  console.log('查看电影:', movie.title)
}

响应式设计

添加媒体查询确保在不同设备上显示良好:

@media (max-width: 600px) {
  .movie-item {
    flex-direction: column;
    text-align: center;
  }

  .rank {
    margin-right: 0;
    margin-bottom: 10px;
  }

  .movie-item img {
    margin-right: 0;
    margin-bottom: 10px;
  }
}

以上实现包含了电影排行的核心功能,可以根据实际需求进一步扩展,如添加分页、筛选、搜索等功能。

标签: 电影排行vue
分享给朋友:

相关文章

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue核心实现

vue核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现原理的详细说明: 响应式系统 Vue 使用 Object.defineProper…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…