13. Twikoo 美化:评论框体验双重增强(输入提示 + 表情放大)
🚀 前言:功能介绍
本指南将,为您的Twikoo评论区添加两个非常实用的功能:
- 输入框提示气泡:当访客点击“昵称”、“邮箱”、“网址”输入框时,上方会弹出一个友好的提示气泡。
- 表情悬停放大:当访客在表情选择面板中,将鼠标悬停在任意一个表情上时,会显示一个放大的预览图,方便看清表情细节。
🗺️ 核心流程概览
- 添加整合后的CSS样式:将两个功能所需的所有CSS代码合并到一个文件中。
- 添加表情放大的JavaScript脚本:将实现放大功能的JS代码添加到另一个文件中。
- 确保文件被正确注入:检查并确认您的主题配置加载了这两个文件。
第一步:添加整合后的CSS代码
我们将把“输入提示”和“表情放大”两个功能所需的全部CSS代码,合并到您的自定义CSS文件中(例如 source/custom/css/tip_style.css
)。
请将下面代码框中的所有内容,完整地复制并添加到您的自定义CSS文件中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
.el-input.el-input--small.el-input-group.el-input-group--prepend:nth-child(1):before { content: '输入QQ号会自动获取昵称和头像🐧'; }
.el-input.el-input--small.el-input-group.el-input-group--prepend:nth-child(2):before { content: '收到回复将会发送到您的邮箱📧'; }
.el-input.el-input--small.el-input-group.el-input-group--prepend:nth-child(3):before { content: '可以通过昵称访问您的网站🔗'; }
.el-input.el-input--small.el-input-group.el-input-group--prepend:focus-within::before, .el-input.el-input--small.el-input-group.el-input-group--prepend:focus-within::after { display: block; }
.el-input.el-input--small.el-input-group.el-input-group--prepend::before { display: none; position: absolute; top: -60px; white-space: nowrap; border-radius: 10px; left: 50%; transform: translate(-50%); padding: 14px 18px; background: #444; color: #fff; z-index: 10; }
.el-input.el-input--small.el-input-group.el-input-group--prepend::after { display: none; content: ''; position: absolute; border: 12px solid transparent; border-top-color: #444; left: 50%; transform: translate(-50%, -48px); z-index: 10; }
#owo-big { position: fixed; display: none; align-items: center; background-color: rgb(255, 255, 255); border: 1px #aaa solid; border-radius: 10px; z-index: 9999; transform: translate(0, -105%); overflow: hidden; animation: owoIn 0.3s cubic-bezier(0.42, 0, 0.3, 1.11); }
[data-theme=dark] #owo-big { background-color: #4a4a4a }
#owo-big img { width: 100%; }
@keyframes owoIn { 0% { transform: translate(0, -95%); opacity: 0; } 100% { transform: translate(0, -105%); opacity: 1; } }
|
第二步:添加表情放大的JavaScript脚本
接下来,将实现“表情放大”功能的JavaScript代码,添加到您的自定义JS文件中(例如 source/custom/js/tip_main.js
)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| if (document.getElementById('post-comment')) owoBig();
function owoBig() { let flag = 1, owo_time = '', m = 3; let div = document.createElement('div'), body = document.querySelector('body'); div.id = 'owo-big'; body.appendChild(div)
let observer = new MutationObserver(mutations => {
for (let i = 0; i < mutations.length; i++) { let dom = mutations[i].addedNodes, owo_body = ''; if (dom.length == 2 && dom[1].className == 'OwO-body') owo_body = dom[1]; else continue; if (document.body.clientWidth <= 768) owo_body.addEventListener('contextmenu', e => e.preventDefault()); owo_body.onmouseover = (e) => { if (flag && e.target.tagName == 'IMG') { flag = 0; owo_time = setTimeout(() => { let height = e.target.clientHeight * m, width = e.target.clientWidth * m, left = (e.x - e.offsetX) - (width - e.target.clientWidth) / 2, top = e.y - e.offsetY;
if ((left + width) > body.clientWidth) left -= ((left + width) - body.clientWidth + 10); if (left < 0) left = 10; div.style.cssText = `display:flex; height:${height}px; width:${width}px; left:${left}px; top:${top}px;`; div.innerHTML = `<img src="${e.target.src}">` }, 300); } }; owo_body.onmouseout = () => { div.style.display = 'none', flag = 1, clearTimeout(owo_time); } }
}) observer.observe(document.getElementById('post-comment'), { subtree: true, childList: true }) }
|
第三步:确认文件注入
最后,请确保您的主题配置文件 (themes/anzhiyu/_config.yml
) 中,已经正确注入了您存放上述代码的自定义CSS和JS文件,并使用Inject诸如对应的CSS与JS