超实用!用 HTML 和 JavaScript 打造可翻转、可切换的词汇闪卡学习神器
这是一个设计精美的词汇闪卡HTML页面,我来补充JavaScript功能实现闪卡翻转和词汇切换: ```javascript // 词汇数据数组 const vocabulary = [ { word: "boy", meaning: "男孩", example: "The boy is playing football in the park." }, { word: "girl", meaning: "女孩", example: "The girl is reading a book under the tree." }, { word: "apple", meaning: "苹果", example: "She eats an apple every morning." }, { word: "computer", meaning: "电脑", example: "I use my computer to study programming." } ]; let currentIndex = 0; const flashcard = document.getElementById('flashcard'); const wordElement = document.getElementById('word'); const wordBackElement = document.getElementById('word-back'); const meaningElement = document.getElementById('meaning'); const exampleElement = document.getElementById('example'); // 翻转卡片函数 function flipCard() { flashcard.classList.toggle('flipped'); } // 更新卡片内容 function updateCard() { const currentWord = vocabulary[currentIndex]; wordElement.textContent = currentWord.word; wordBackElement.textContent = currentWord.word; meaningElement.textContent = currentWord.meaning; exampleElement.textContent = currentWord.example; // 如果不是翻转状态就翻转回来 if (flashcard.classList.contains('flipped')) { setTimeout(() => { flashcard.classList.remove('flipped'); }, 300); } } // 下一张卡片 function nextCard() { currentIndex = (currentIndex + 1) % vocabulary.length; updateCard(); } // 上一张卡片 function prevCard() { currentIndex = (currentIndex - 1 + vocabulary.length) % vocabulary.length; updateCard(); } // 初始化显示第一张卡片 updateCard(); // 添加控制按钮到HTML中 document.body.innerHTML += ` <div class="controls"> <button onclick="prevCard()">Previous</button> <button onclick="nextCard()">Next</button> </div> <div class="progress"> Card ${currentIndex + 1} of ${vocabulary.length} </div> `; // 更新进度显示的函数 function updateProgress() { const progressElement = document.querySelector('.progress'); if (progressElement) { progressElement.textContent = `Card ${currentIndex + 1} of ${vocabulary.length}`; } } // 修改nextCard和prevCard函数以更新进度 function nextCard() { currentIndex = (currentIndex + 1) % vocabulary.length; updateCard(); updateProgress(); } function prevCard() { currentIndex = (currentIndex - 1 + vocabulary.length) % vocabulary.length; updateCard(); updateProgress(); } ``` 代码逻辑说明: 1. 使用vocabulary数组存储所有词汇数据 2. currentIndex跟踪当前显示的词汇索引 3. flipCard()通过切换CSS类实现3D翻转效果 4. updateCard()更新卡片正反面内容 5. nextCard()和prevCard()实现前后导航功能 6. updateProgress()显示当前学习进度 额外注意事项: 1. 翻转动画使用CSS transition实现,确保性能流畅 2. 数组索引使用取模运算实现循环浏览 3. 更新卡片内容时自动检查并重置翻转状态 ######[AI写代码神器 | 719点数解答 | 2025-03-29 21:31:19]
- C语言实现整数“第二数位”翻转:代码详解与复杂度分析(字节豆包 | 523点数解答 | 2025-11-10 19:54:57)91
- C++实现整数“第二数位”翻转:代码详解与复杂度分析(字节豆包 | 462点数解答 | 2025-11-10 20:30:20)113
- 微信钱包前端演示代码:从问题修正到完善功能及安全提示(字节豆包 | 2097点数解答 | 2025-07-11 18:06:46)168
- 网页代码小优化:给男友爱的消息提示框升级秘籍 (字节豆包 | 139点数解答 | 2025-03-17 22:07:34)252
- Android开发:用Intent轻松实现Activity切换与数据传递(阿里通义 | 308点数解答 | 2023-11-07 22:46:17)319
- JavaScript开发:Button插件装载、切换及复选框、单选框处理全解析(阿里通义 | 378点数解答 | 2023-11-09 01:25:25)351
- Java JSP 代码:用 List 存储 Map 集合并循环添加姓名和年龄(GPT | 240点数解答 | 2024-11-25 09:17:43)259
- 英文打字练习程序:从问题重重到全面优化的蜕变(字节豆包 | 2538点数解答 | 2025-08-20 12:28:06)175
- 生日祝福代码优化建议与逻辑要点大揭秘(字节豆包 | 194点数解答 | 2025-03-09 11:37:59)204
- 程序员一年自我提升秘籍:工作、学习、生活全方位进阶( | 486点数解答 | 2024-01-02 22:15:17)392
- Octave绘制城市群人口流动:ABC三城随时间与迁徙率p变化图像揭秘(DeepSeek | 1013点数解答 | 2025-04-24 23:35:01)215
- 2025 暑假苍溪 - 广安 - 重庆红色研学:中小学生传承红色基因之旅 (字节豆包 | 1244点数解答 | 2025-06-19 16:32:34)251