大致流程
添加侧边栏 -> 添加CSS -> 激活配置 -> 同步完工
|
演示效果
添加一言插件
编辑themes/next/layout/_custom/sidebar.swig
文件, 若无则创建.
<strong> <div id="hitokoto_wrap"> <div id="hitokoto_text"> 抬头望天,低头走路。</div> <div id="hitokoto_from">——无名</div> </div> </strong>
<script> let hitokoto_wrap = document.getElementById("hitokoto_wrap"); let hitokoto_text = document.getElementById('hitokoto_text') let hitokoto_from = document.getElementById('hitokoto_from'); async function loadHitokoto(){ fetch('https://international.v1.hitokoto.cn?min_length=12') .then(response => response.json()) .then(data => { hitokoto_text.innerText = '\xa0\xa0\xa0\xa0\xa0\xa0\xa0' + data.hitokoto; hitokoto_from.innerText = "——" + data.from + '\xa0'; }) .catch(console.error) } loadHitokoto();
hitokoto_wrap.addEventListener("click", async function(e){ loadHitokoto(); let content = (hitokoto_text.innerText + hitokoto_from.innerText).trim() navigator.clipboard.writeText(content) .then(console.log(content)) .catch(console.error) }); </script>
|
添加点击爱心特效
<script> !function(e, t, a) { function r() { for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999"); requestAnimationFrame(r) } function n() { var t = "function" == typeof e.onclick && e.onclick; hitokoto_wrap.addEventListener("click", function(e) { t && t(), o(e) }); } function c() { return "rgb(" + ~~ (255 * Math.random()) + "," + ~~ (255 * Math.random()) + "," + ~~ (255 * Math.random()) + ")" } function o(e) { var a = t.createElement("div"); a.className = "heart", s.push({ el: a, x: e.clientX - 5, y: e.clientY - 5, scale: 1, alpha: 1, color: c() }), t.body.appendChild(a) } function i(e) { var a = t.createElement("style"); a.type = "text/css"; try { a.appendChild(t.createTextNode(e)) } catch(t) { a.styleSheet.cssText = e } t.getElementsByTagName("head")[0].appendChild(a) }
var s = []; e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function(e) { setTimeout(e, 1e3 / 60) }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r() } (window, document); </script>
|
激活一言窗口
编辑themes/next/_config.yml
文件
找到custom_file_path
修改sidebar
为themes/next/layout/_custom/sidebar.swig
, 才会加载定制的侧边栏内容.
![]()
添加一言CSS
编辑themes/next/source/css/_custom/custom.styl
文件
//hitokoto
#hitokoto_wrap { margin-top: 15px; cursor: pointer; display: flex; flex-direction: column; flex-wrap: nowrap; }
#hitokoto_text { text-align: left; font-size: 0.8125em; }
#hitokoto_from { text-align: right; font-size: 0.7em; color: #999; }
|
导入一言CSS
编辑themes/next/source/css/main.styl
文件
在末尾添加@import "_custom/custom";
, 这样css才会生效.
![]()
测试效果 -> 同步配置 -> 大功告成🎉
参考文档
语句接口 | 一言开发者中心 (hitokoto.cn)
为您的Hexo博客添加Hitokoto一言功能 | Bill Yang's Blog
JS实现鼠标点击爱心&绘制多边形&每日一言功能 - 掘金 (juejin.cn)