背景
各位是否会遇到这样的情况,在百度百科或者其他网站阅读古诗词文言文时,常常正文一块,注释一块。可偏偏正文又比较长,注释也比较多,鼠标来回上下滚翻十分麻烦。
于是我想能否将正文注释转换成带悬停标注的内容,当鼠标悬停在注释标记上时,就会冒出注释来。
由于网上我并没有找到合适的工具,所以决定编写一个文本悬停注释工具,希望能为同好提供便利。
构思
1. 鼠标悬停显示文本(也称为悬停提示或工具提示)可以通过多种方式实现,比如使用 HTML 的 title
属性,使用 CSS 的 :hover
伪类和 ::after
或 ::before
伪元素,使用 JavaScript 显示工具提示,以及使用第三方库(如 Bootstrap 或 jQuery UI)等等;
2. 由于使用场景较简单,故采用CSS来实现;
3. 首先构建HTML模板,使用 CSS 的 :hover
伪类和 ::after
伪元素来实现悬停提示,模板主体内容留空;
4. 正文和注释内容使用GUI来采集。Tkinter 是 Python 内置的标准 GUI 库,功能强大且易于使用,因此采用 Python 的 tkinter
库来搭建 GUI 界面;
5. 然后使用Python将正文和注释转换为悬浮注释内容对应的HTML格式,替换进HTML模板的主体内容中;
6. 使用浏览器打开HTML文件即可浏览带有悬浮注释的诗词及文言文了。
HTML模板
基本框架
以下是HTML模板的基本框架,由于测试时发现悬浮注释可能超出网页边界,影响阅读,所以为网页加入了Header和Footer,同时预留了左右的边界。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TipTools文本悬浮注释工具</title>
<style>...</style>
</head>
<body>
<div class="container">
<div class="header">...</div>
<div class="content">...</div>
<div class="footer">...</div>
</div>
</body>
</html>
结构样式部分
.container {
flex: 1;
display: flex;
flex-direction: column;
}
这段样式设置了 html
和 body
元素的高度为 100%,并使用 flex
布局,使得内容部分可以动态适应页面高度。
.container {
flex: 1;
display: flex;
flex-direction: column;
}
container
是一个主容器,采用 flex
布局,占据整个页面的可用高度。
.header {
background-color: green;
color: white;
padding: 20px;
text-align: left;
}
header
定义了页面头部区域的样式,背景为绿色,文本为白色,左右内边距为20像素,左对齐。
.content {
flex: 1;
margin: 10px auto;
padding: 20px;
max-width: 800px;
width: 90%;
background-color: #f4f4f4;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
content
是页面的主要内容部分,它自适应页面宽度并居中。背景色为浅灰色,添加了一些阴影来提升视觉层次感。
.footer {
background-color: gray;
color: white;
padding: 10px;
text-align: right;
}
页脚的背景为灰色,字体颜色为白色,内容右对齐。
悬停提示CSS代码的实现
以下代码实现鼠标悬停显示文本的效果:
<style>
.tooltip {
position: relative;
display: inline-block;
cursor: pointer;
}
.tooltip .tooltip-text {
visibility: hidden;
width: 250px;
background-color: #333;
color: #fff;
text-align: left;
border-radius: 5px;
padding: 8px;
position: absolute;
z-index: 1;
bottom: 120%;
left: 50%;
transform: translateX(-50%);
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltip-text::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #333 transparent transparent transparent;
}
.tooltip:hover .tooltip-text {
visibility: visible;
opacity: 1;
}
</style>
tooltip
类表示带有悬浮提示的文本块,当鼠标悬停时,显示额外的注释信息。.tooltip-text
定义了悬浮注释框的样式,默认隐藏,鼠标悬停时通过:hover
状态显示。- 悬浮提示框设置为绝对定位,显示在文本的上方,通过透明度的渐变实现显示的动画效果。
内容部分
<div class="content">
<p>SubstituteText</p>
</div>
这个部分的内容会被动态替换(由Python代码生成的文本替换 SubstituteText
部分),并在最终生成的网页中显示带有注释的文本。
完整代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TipTools文本悬浮注释工具</title>
<style>
/* Apply height and flex to the entire body */
html, body {
height: 100%;
margin: 0;
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
}
/* Main container to take full height */
.container {
flex: 1;
display: flex;
flex-direction: colum