随机跳转文章(只能跳到首页的文章)

This commit is contained in:
liuzhihang 2022-10-18 01:02:03 +08:00
parent b647c635df
commit c75a1eb957
5 changed files with 76 additions and 7 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -149,10 +149,6 @@
<!-- todo 不知道是否可以抽到 right-menu.html 中,做到后台可配置 -->
<script th:src="@{/assets/zhheo/rightmenu.js}"></script>
<!-- todo 随机跳转功能,应该是随机友链和随机文章,应该也可以抽走 -->
<script th:src="@{/assets/zhheo/random.js}"></script>
<script data-pjax th:src="@{/assets/zhheo/commentBarrage.js}"></script>
<!-- https://raphamorim.io/waterfall.js/ 应该是这个 还有相关的 js 代码 是否可以调整-->
<script src="https://cdn.zhheo.com/public/waterfall/waterfall.min.js"></script>

View File

@ -185,7 +185,7 @@
</div>
</div>
<!-- 随机前往一篇文章 -->
<a id="banner-hover" onclick="toRandomPost()"><span class="bannerText">随便逛逛</span><i
<a id="banner-hover" onclick="randomPost()"><span class="bannerText">随便逛逛</span><i
class="fas fa-arrow-right banner-righticon"></i></a></div>
<!-- category -->
@ -203,5 +203,27 @@
<span class="categoryButtonText">实用教程</span><i class="fas fa-book"></i></a>
</div>
</div>
<script th:inline="javascript">
function randomPost() {
// 随机跳转首页的一篇文章
// 后续改成跳转全站的文章,可以从 sitemap 中获取所有文章
let posts = [[${posts}]];
let datum = posts.total < posts.size ? posts.total : posts.size;
let number = Math.floor(Math.random() * datum);
console.log(number);
let post = posts.items[number];
// 当前窗口打开
window.location.href = post.status.permalink;
// window.open(post.status.permalink);
}
</script>
</div>
</html>