写一个和 readlang 一样的页面,输入一段文字,可以把文字渲染成阅读组件,点击单词会在单词旁边展示意思,选中多个单词会翻译,点击单词的同时会在侧边栏调用词典,展示更具体的意思
- User
写一个和 readlang 一样的页面,输入一段文字,可以把文字渲染成阅读组件,点击单词会在单词旁边展示意思,选中多个单词会翻译,点击单词的同时会在侧边栏调用词典,展示更具体的意思
- Assistant
当然可以!下面是一个简化版的 Readlang 风格的阅读页面实现,使用 HTML、CSS 和 JavaScript(原生 + 一些 API 示例)来实现以下功能:
功能需求:
- 输入一段文字,渲染为可点击的阅读组件。
- 点击单词:在单词旁边显示翻译。
- 选中多个单词:显示整段翻译。
- 点击单词时,在右侧侧边栏显示词典信息(模拟词典 API)。
我们将使用:
- HTML:构建页面结构。
- CSS:样式。
- JavaScript:处理点击、选中、翻译和词典查询。
- 使用一个简单的翻译 API(如 LibreTranslate)或模拟翻译。
完整代码如下:
📁 文件结构:
- index.html
- style.css
- script.js
🔹 index.html
<div class="main-content"> <div id="reader" class="reader"></div> <div id="sidebar" class="sidebar"> <h3>词典</h3> <div id="dictionaryContent">点击单词查看释义</div> </div> </div> <div id="tooltip" class="tooltip"></div> <div id="selectionTranslation" class="selection-translation"></div>🔹 style.css
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.container { padding: 20px; }
.input-section { margin-bottom: 20px; }
#textInput { width: 100%; height: 100px; font-size: 16px; }
button { margin-top: 10px; padding: 8px 16px; font-size: 16px; }
.main-content { display: flex; gap: 20px; }
.reader { flex: 3; line-height: 1.6; font-size: 18px; position: relative; }
.reader span.word { cursor: pointer; padding: 2px 4px; border-radius: 4px; }
.reader span.word:hover { background-color: #f0f0f0; }
.sidebar { flex: 1; border-left: 1px solid #ccc; padding-left: 10px; }
.tooltip { position: absolute; background-color: #333; color: #fff; padding: 4px 8px; border-radius: 4px; font-size: 14px; display: none; z-index: 1000; }
.selection-translation { margin-top: 20px; font-style: italic; color: #333; }
🔹 script.js
const renderBtn = document.getElementById('renderBtn'); const textInput = document.getElementById('textInput'); const reader = document.getElementById('reader'); const tooltip = document.getElementById('tooltip'); const selectionTranslation = document.getElementById('selectionTranslation'); const dictionaryContent = document.getElementById('dictionaryContent');
renderBtn.addEventListener('click', () => { const text = textInput.value.trim(); if (!text) return;
reader.innerHTML = ''; const words = text.split(/(\s+)/); // 保留空格 words.forEach(word => { if (word.trim() === '') { reader.appendChild(document.createTextNode(word)); } else { const span = document.createElement('span'); span.className = 'word'; span.textContent = word; span.addEventListener('click', (e) => handleWordClick(e, word)); reader.appendChild(span); } }); });
function handleWordClick(e, word) { // 显示 tooltip showTooltip(e, '翻译中...'); fetchTranslation(word).then(translation => { showTooltip(e, translation); });
// 显示词典信息 fetchDictionary(word).then(def => { dictionaryContent.innerHTML = def; }); }
function showTooltip(e, text) { tooltip.textContent = text; tooltip.style.left = e.pageX + 'px'; tooltip.style.top = (e.pageY - 30) + 'px'; tooltip.style.display = 'block'; setTimeout(() => { tooltip.style.display = 'none'; }, 3000); }
// 模拟翻译 API async function fetchTranslation(text) { try { const res = await fetch('https://libretranslate.de/translate', { method: 'POST', body: JSON.stringify({ q: text, source: 'en', target: 'zh', format: 'text' }), headers: { 'Content-Type': 'application/json' } }); const data = await res.json(); return data.translatedText; } catch (err) { return '翻译失败'; } }
// 模拟词典 API async function fetchDictionary(word) { try { const res = await fetch(
https://api.dictionaryapi.dev/api/v2/entries/en/${word}); const data = await res.json(); if (data[0]) { const meanings = data[0].meanings.map(m =><div> <strong>${m.partOfSpeech}</strong>: ${m.definitions[0].definition} </div>).join(''); return<h4>${word}</h4>${meanings}; } else { return '未找到词典信息'; } } catch (err) { return '词典查询失败'; } }// 处理选中翻译 document.addEventListener('mouseup', () => { const selection = window.getSelection().toString().trim(); if (selection.split(' ').length > 1) { fetchTranslation(selection).then(translation => { selectionTranslation.textContent =
“${selection}” 的翻译:${translation}; }); } });使用说明:
- 输入英文文本,点击“渲染文本”。
- 点击任意单词,会显示翻译 tooltip,并在右侧显示词典信息。
- 选中多个单词,会在下方显示整段翻译。
注意事项:
- 由于跨域限制,某些 API(如 LibreTranslate)可能需要部署代理或使用本地服务器。
- 你可以替换为你自己的翻译 API 或词典服务。
希望这个示例对你有帮助!如果你需要 React/Vue 版本,也可以告诉我,我可以帮你重构成组件化版本。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.