当前位置:首页 > VUE

vue实现题目翻页

2026-02-20 09:54:45VUE

实现思路

在Vue中实现题目翻页功能,通常需要结合分页逻辑和组件状态管理。核心是通过当前页码控制题目数据的显示,并提供翻页按钮切换页码。

基础实现方案

数据准备

vue实现题目翻页

  • 准备题目数组,每个元素代表一道题目
  • 定义当前页码的响应式数据
data() {
  return {
    questions: [
      {id: 1, title: "题目1", content: "内容1"},
      {id: 2, title: "题目2", content: "内容2"},
      // 更多题目...
    ],
    currentPage: 1,
    pageSize: 1 // 每页显示1题
  }
}

计算当前题目 使用计算属性获取当前页对应的题目:

computed: {
  currentQuestion() {
    const start = (this.currentPage - 1) * this.pageSize
    return this.questions.slice(start, start + this.pageSize)[0]
  }
}

翻页方法 实现上一页和下一页方法:

vue实现题目翻页

methods: {
  prevPage() {
    if (this.currentPage > 1) {
      this.currentPage--
    }
  },
  nextPage() {
    if (this.currentPage < this.totalPages) {
      this.currentPage++
    }
  }
},
computed: {
  totalPages() {
    return Math.ceil(this.questions.length / this.pageSize)
  }
}

模板部分

<template>
  <div class="question-container">
    <div v-if="currentQuestion">
      <h3>{{ currentQuestion.title }}</h3>
      <p>{{ currentQuestion.content }}</p>
    </div>

    <div class="pagination">
      <button 
        @click="prevPage" 
        :disabled="currentPage === 1">
        上一题
      </button>
      <span>第 {{ currentPage }} 题 / 共 {{ totalPages }} 题</span>
      <button 
        @click="nextPage" 
        :disabled="currentPage === totalPages">
        下一题
      </button>
    </div>
  </div>
</template>

进阶优化

添加过渡效果 可以为题目切换添加过渡动画:

<transition name="fade" mode="out-in">
  <div v-if="currentQuestion" :key="currentQuestion.id">
    <!-- 题目内容 -->
  </div>
</transition>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}

保存答题状态 使用对象记录每道题的答题状态:

data() {
  return {
    answers: {}
  }
},
methods: {
  saveAnswer(answer) {
    this.$set(this.answers, this.currentQuestion.id, answer)
  }
}

完整组件示例

<template>
  <div>
    <transition name="fade" mode="out-in">
      <div v-if="currentQuestion" :key="currentQuestion.id">
        <h3>{{ currentQuestion.title }}</h3>
        <p>{{ currentQuestion.content }}</p>

        <textarea 
          v-model="answers[currentQuestion.id]" 
          placeholder="输入答案">
        </textarea>
      </div>
    </transition>

    <div class="nav-buttons">
      <button @click="prevPage" :disabled="currentPage === 1">上一题</button>
      <span>{{ currentPage }}/{{ totalPages }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一题</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      questions: [], // 可通过API获取
      currentPage: 1,
      pageSize: 1,
      answers: {}
    }
  },
  computed: {
    currentQuestion() {
      const start = (this.currentPage - 1) * this.pageSize
      return this.questions.slice(start, start + this.pageSize)[0]
    },
    totalPages() {
      return Math.ceil(this.questions.length / this.pageSize)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  }
}
</script>

注意事项

  1. 对于大量题目考虑分页加载,不要一次性加载所有题目数据
  2. 移动端适配可以添加滑动翻页功能
  3. 考虑添加题目跳转功能,允许用户直接选择特定题号
  4. 对于考试场景,可以添加计时功能和自动保存答案的逻辑

标签: 翻页题目
分享给朋友:

相关文章

vue实现数字翻页递增

vue实现数字翻页递增

Vue 实现数字翻页递增效果 实现数字翻页递增效果可以通过 Vue 的过渡动画和动态数据绑定完成。以下是具体实现方法: 基本实现思路 使用 Vue 的 transition 组件结合 CSS 动画,…

vue中实现翻页功能

vue中实现翻页功能

使用 v-pagination 组件 Vue 的 v-pagination 组件(来自 Vuetify)提供了一种简单的方式实现翻页功能。安装 Vuetify 后,可以直接使用该组件。 <te…

php 实现翻页

php 实现翻页

实现基本分页功能 使用PHP的LIMIT子句结合MySQL实现分页是最常见的方法。需要计算当前页码和每页显示的记录数。 // 每页显示数量 $perPage = 10; // 当前页码(从URL参数…

js实现上下翻页

js实现上下翻页

实现上下翻页的JavaScript方法 监听键盘事件实现翻页 通过监听键盘的上下箭头键触发翻页动作,适用于单页应用或长页面浏览场景。 document.addEventListener('keyd…

js 实现图片翻页效果

js 实现图片翻页效果

使用CSS动画实现翻页效果 通过CSS的transform和transition属性创建平滑的翻页动画效果。HTML结构中需要将图片放置在容器内,并利用JavaScript切换类名触发动画。 <…

js实现上划翻页

js实现上划翻页

监听触摸事件 通过监听 touchstart、touchmove 和 touchend 事件来捕捉用户滑动行为。记录触摸起始位置和移动距离,判断是否为有效的上滑动作。 let startY = 0;…