初始化中...
在留言中有个小伙伴问道关于博客首页下面的页数跳转按钮是怎么做的,在浏览了一下友链中的文章后,发现这个功能确实没有太多的教程,所以想着出一篇关于这个功能的教程来满足一下留言中的小伙伴,安排!
前言
起初添加这个功能,是因为看到leonus和heo都做了这个新功能(新活儿),所以当然不能落后呀,我就马不停蹄的将它实现(抄✍️)了。
预览效果
效果如下图:


页面添加按钮元素
找到 themes\butterfly\layout\includes\pagination.pug 文件,并在文件的最后做如下代码添加:
1 2 3 4 5 6 7 8 9 10 11 12
| ......省略上面无关紧要的代码 else nav#pagination .pagination if is_home() - options.format = 'page/%d/' !=paginator(options) + if is_home() + .toPageGroup + input#toPageText(maxlength="3" oninput="value=value.replace(/[^0-9]/g,'')" onkeyup="if (this.value === '0') this.value = ''" title="跳转到指定页面") + a#toPageButton(data-pjax-state="" onclick="shine.toPage()") + i.fa-solid.fa-angles-right
|
添加按钮的功能
我们在js文件中添加一段代码(还没做过添加自定义js文件的小伙伴可以看看这篇教程Hexo博客添加自定义css和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
| var shine = { toPage: function() { console.log("执行跳转"); var e = document.querySelectorAll(".page-number") , t = parseInt(e[e.length - 1].innerHTML) , o = document.getElementById("toPageText") , n = parseInt(o.value); if (!isNaN(n) && n > 0 && "0" !== ("" + n)[0] && n <= t) { var a = (n == 1) ? "/" : "/page/" + n + "/"; document.getElementById("toPageButton").href = a } }, listenToPageInputPress() { var e = document.getElementById("toPageText") , t = document.getElementById("toPageButton"); e && (e.addEventListener("keydown", (e=>{ 13 === e.keyCode && (shine.toPage(), pjax.loadUrl(t.href)) } )), e.addEventListener("input", (function() { "" === e.value || "0" === e.value ? t.classList.remove("haveValue") : t.classList.add("haveValue"); var o = document.querySelectorAll(".page-number") , n = +o[o.length - 1].innerHTML; +document.getElementById("toPageText").value > n && (e.value = n) } ))) } } shine.listenToPageInputPress();
|
添加按钮的css样式
将下面的按钮css代码复制到你的自定义css文件中,例如custom.css等样式文件(还没做过添加自定义css文件的小伙伴可以看看这篇教程Hexo博客添加自定义css和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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| .pagination input { width: 2rem; height: 2rem; border-radius: 8px; border: var(--style-border-always); transition: all 0.3s; outline-style: none; font-size: 16px; padding-left: 12px; background: var(--heo-secondbg); color: var(--heo-fontcolor); }
.pagination .toPageGroup:hover input,.pagination .toPageGroup input:focus { border: 1px solid #425AEF; outline-style: none; width: 100px; }
.toPageGroup { display: flex !important; position: relative; margin: 0 0.3rem !important; }
a#toPageButton { display: flex; position: absolute; width: 2rem; height: 2rem; border-radius: 8px; justify-content: center; align-items: center; transition: all 0.3s; background: var(--heo-card-bg); border: var(--style-border-always); cursor: text !important; pointer-events: none; }
.toPageGroup:hover a#toPageButton, .toPageGroup:focus-within a#toPageButton { margin-top: 2.5px; width: 27px; height: 27px; margin-left: 70px; background: var(--heo-card-bg); border: 1px solid var(--heo-none); border-radius: 4px; opacity: 0.2; transition: all 0.3s !important; }
.toPageGroup:focus-within a#toPageButton.haveValue { opacity: 1; cursor: pointer; }
a#toPageButton.haveValue { opacity: 1!important; cursor: pointer!important; pointer-events: all; }
a#toPageButton.haveValue:hover { background: var(--heo-theme); color: var(--heo-white); }
@media screen and (max-width: 768px) { .toPageGroup { display: none !important; } }
|
Hexo三连
1
| hexo clean && hexo g && hexo s
|