基于vue2實現(xiàn)簡單的答題組件
需求
實現(xiàn)一個答題組件,點擊正確的選項,該選項背景變綠色;點擊錯誤的選項,該選項背景變紅色。不管點擊了什么選項,延遲一秒后切換下一題。
每次出題,從題庫中選隨機(jī)選擇一道用戶此次進(jìn)入這個頁面后還沒有做過的題目,如果此次進(jìn)入這個頁面把所有題都做了,則重置,重新開始隨機(jī)選題。
頁面結(jié)構(gòu)
<div class="question"> {{questions[selectedQuestionIndex].question}} </div> <div class="choices"> <div class="choice" v-for="(item, index) in questions[selectedQuestionIndex].choices" :key="item.content" // 當(dāng)點擊了當(dāng)前的選項,則開始處理背景顏色 :style="{ background: selectedChoiceIndex === index ? feedbackColor : '' }" // 用來顯示點擊之后的背景顏色 @click="handleChoice(index)" > <span>{{item.index}}</span> <span>{{item.content}}</span> </div> </div>
數(shù)據(jù)結(jié)構(gòu)
data() { return { selectedQuestionIndex: 0, // 當(dāng)前顯示的問題的索引 selectedChoiceIndex: null, // 記錄用戶當(dāng)前選擇的選項索引 feedbackColor: '', // 記錄選項的反饋顏色 answeredQuestions: new Set(), // 已回答題目索引 questions: [ // 題庫 // 1 { question: '下列哪一項不是森林生態(tài)系統(tǒng)服務(wù)的一部分?', choices: [ { index: 'A: ', content: '氧氣生產(chǎn)' }, { index: 'B: ', content: '水土保持' }, { index: 'C: ', content: '提供化石燃料' }, { index: 'D: ', content: '生物多樣性維持' }, ], correctChoiceIndex: 2 }, // 2 { question: '碳匯是指什么?', choices: [ { index: 'A: ', content: '大量排放二氧化碳的地方' }, { index: 'B: ', content: '能夠吸收并儲存二氧化碳的自然系統(tǒng)' }, { index: 'C: ', content: '一種工業(yè)過程,用于減少溫室氣體' }, { index: 'D: ', content: '用于監(jiān)測大氣中二氧化碳水平的技術(shù)' }, ], correctChoiceIndex: 1 }, ] }; },
方法解析
有三個方法:
handleChoice
:根據(jù)用戶的選擇決定顯示什么背景顏色;切換下一題nextQuestion
:決定下一題選哪一道;重置選擇狀態(tài)和選項背景顏色resetQuiz
:重新開始測試
handleChoice(choiceIndex) { // 判斷是否答對 const currentQuestion = this.questions[this.selectedQuestionIndex]; // 獲取當(dāng)前問題 // 判斷用戶點擊的是否是正確選項,計算對應(yīng)的背景顏色 if (choiceIndex === currentQuestion.correctChoiceIndex) { this.feedbackColor = '#adce74'; // 綠色表示答對 } else { this.feedbackColor = '#ea6458'; // 紅色表示答錯 } // 設(shè)置選中選項索引 // 一旦selectedChoiceIndex有值了,那么頁面上就會渲染背景顏色 this.selectedChoiceIndex = choiceIndex; // 延遲一段時間切換到下一題 setTimeout(() => { this.nextQuestion(); }, 1000); // 延遲1秒 },
nextQuestion() { // 將當(dāng)前題目加入已回答集合 this.answeredQuestions.add(this.selectedQuestionIndex); // 篩選未回答的題目索引 const unansweredIndexes = this.questions .map((_, index) => index) // 得到一個全是問題編號的數(shù)組 .filter(index => !this.answeredQuestions.has(index)); // 過濾出不在answeredQuestions里面的問題的序號 if (unansweredIndexes.length > 0) { // 隨機(jī)選取一道未回答的題目 const randomIndex = Math.floor(Math.random() * unansweredIndexes.length); this.selectedQuestionIndex = unansweredIndexes[randomIndex]; } else { // 如果沒有未回答的題目,重置已回答集合并重新開始 this.answeredQuestions.clear(); this.resetQuiz(); } // 重置選中狀態(tài) this.selectedChoiceIndex = null; this.feedbackColor = ''; },
resetQuiz() { // 重新隨機(jī)選擇一個題目 this.selectedQuestionIndex = Math.floor(Math.random() * this.questions.length); }
組件代碼
<template> <div class="question-container"> <div class="question"> {{questions[selectedQuestionIndex].question}} </div> <div class="choices"> <div class="choice" v-for="(item, index) in questions[selectedQuestionIndex].choices" :key="item.content" :style="{ background: selectedChoiceIndex === index ? feedbackColor : '' }" @click="handleChoice(index)" > <span>{{item.index}}</span> <span>{{item.content}}</span> </div> </div> </div> </template> <script> import {mapMutations} from "vuex"; export default { name: "questionDetail", data() { return { selectedQuestionIndex: 0, selectedChoiceIndex: null, // 記錄用戶當(dāng)前選擇的選項索引 feedbackColor: '', // 記錄選項的反饋顏色 answeredQuestions: new Set(), // 已回答題目索引 questions: [ // 1 { question: '下列哪一項不是森林生態(tài)系統(tǒng)服務(wù)的一部分?', choices: [ { index: 'A: ', content: '氧氣生產(chǎn)' }, { index: 'B: ', content: '水土保持' }, { index: 'C: ', content: '提供化石燃料' }, { index: 'D: ', content: '生物多樣性維持' }, ], correctChoiceIndex: 2 }, // 2 { question: '碳匯是指什么?', choices: [ { index: 'A: ', content: '大量排放二氧化碳的地方' }, { index: 'B: ', content: '能夠吸收并儲存二氧化碳的自然系統(tǒng)' }, { index: 'C: ', content: '一種工業(yè)過程,用于減少溫室氣體' }, { index: 'D: ', content: '用于監(jiān)測大氣中二氧化碳水平的技術(shù)' }, ], correctChoiceIndex: 1 }, ] }; }, methods: { ...mapMutations(['addQuestion']), handleChoice(choiceIndex) { // 判斷是否答對 const currentQuestion = this.questions[this.selectedQuestionIndex]; if (choiceIndex === currentQuestion.correctChoiceIndex) { this.feedbackColor = '#adce74'; // 綠色表示答對 this.addQuestion(); } else { this.feedbackColor = '#ea6458'; // 紅色表示答錯 } // 設(shè)置選中選項索引 this.selectedChoiceIndex = choiceIndex; // 延遲一段時間切換到下一題 setTimeout(() => { this.nextQuestion(); }, 1000); // 延遲1秒 }, nextQuestion() { // 將當(dāng)前題目加入已回答集合 this.answeredQuestions.add(this.selectedQuestionIndex); // 篩選未回答的題目索引 const unansweredIndexes = this.questions .map((_, index) => index) .filter(index => !this.answeredQuestions.has(index)); if (unansweredIndexes.length > 0) { // 隨機(jī)選取一道未回答的題目 const randomIndex = Math.floor(Math.random() * unansweredIndexes.length); this.selectedQuestionIndex = unansweredIndexes[randomIndex]; } else { // 如果沒有未回答的題目,重置已回答集合并重新開始 this.answeredQuestions.clear(); this.resetQuiz(); } // 重置選中狀態(tài) this.selectedChoiceIndex = null; this.feedbackColor = ''; }, resetQuiz() { // 重新隨機(jī)選擇一個題目 this.selectedQuestionIndex = Math.floor(Math.random() * this.questions.length); } } }; </script> <style scoped lang="scss"> .question-container { width: 98%; height: calc(100vh - 210px); margin-top: 15px; padding: 0 15px 0 15px; //border: 1px red solid; overflow: auto; .question { font-family: 'SanJinSong-Cu', serif; color: #7c9a92; font-size: 35px; } .choices { margin-top: 30px; display: flex; flex-direction: column; gap: 15px; .choice { box-sizing: border-box; padding: 10px 20px; color: white; font-family: 'SanJinSong-Xi', serif; font-size: 25px; width: 100%; min-height: 80px; display: flex; align-items: center; border-radius: 8px; background: linear-gradient(to right, #1F6D5E, #43D6B9); cursor: pointer; transition: background 0.3s ease; } } } </style>
效果演示
到此這篇關(guān)于基于vue2實現(xiàn)簡單的答題組件的文章就介紹到這了,更多相關(guān)vue答題組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue給數(shù)組中對象排序 sort函數(shù)的用法
這篇文章主要介紹了vue給數(shù)組中對象排序 sort函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09vue3中WatchEffect高級偵聽器的實現(xiàn)
本文主要介紹了vue3中WatchEffect高級偵聽器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01vue 驗證碼界面實現(xiàn)點擊后標(biāo)灰并設(shè)置div按鈕不可點擊狀態(tài)
今天小編就為大家分享一篇vue 驗證碼界面實現(xiàn)點擊后標(biāo)灰并設(shè)置div按鈕不可點擊狀態(tài),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10淺談VUE單頁應(yīng)用首屏加載速度優(yōu)化方案
這篇文章主要介紹了淺談VUE單頁應(yīng)用首屏加載速度優(yōu)化方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08