Files
blog/public/js/main.js
T
2024-08-03 16:56:17 +08:00

137 lines
4.1 KiB
JavaScript

// 判断是否为移动端
function isMobile() {
if (window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
return true; // 移动端
} else {
return false; // PC端
}
}
// 文章图片灯箱
window.ViewImage && ViewImage.init('.post-content img');
// 激活返回顶部插件
$(function () {
$.scrollUp({
animation: 'fade',
scrollImg: true
});
});
// loader定时器
$(document).ready(function () {
setTimeout(function () {
$("#loader").addClass("hide");
}, 30000);
});
// 点击进入按钮
$("#closeloader").click(function () {
$("#loader").addClass("hide");
});
// nav栏样式调整
$('#libabys').first().attr("style", "margin-left: -5px");
$('#libabys').eq(1).attr("style", "margin-left: -5px");
// 友链列表版样式调整
$('.itp-index:odd').attr("style", "float: right;width:48%;");
//文章图片懒加载
lazyLoadInit({
coverColor: "white",
coverDiv: "<div style='display: flex; justify-content: center; align-items: center; height: 200px; color: #000000;'><span>优世界·一个记录生活点滴的小站</span></div>",
offsetBottom: 0,
offsetTopm: 0,
showTime: 1100
});
async function fetchCommentsAndCreateBarrager(path, options) {
try {
const response = await fetch('https://waline.usj.cc/api/comment?path=' + encodeURIComponent(path));
const data = await response.json();
if (!data.data || !data.data.data) {
console.error('评论数据获取失败');
return;
}
const commentsData = data.data.data;
const looperTime = options.looper_time || 3000;
const flattenedComments = flattenComments(commentsData);
const totalComments = flattenedComments.length;
let currentIndex = 0;
// 初始化弹幕插件
let barrage = new Barrage.default({
el: 'body',
backColor:"#000000b5",
fontSize:"14px",
isLeft:true,
speed:0,
height:"33px"
});
if (totalComments !== 0) {
createBarrager();
}
function createBarrager() {
const currentComment = flattenedComments[currentIndex];
const barragerItem = {
headImg: currentComment.avatar + "?s=33" || options.defaultAvatar,
content: stripHTML(currentComment.orig) || options.defaultInfo,
href: currentComment.link || options.defaultLink,
speed: options.speed || 10
};
// 发送弹幕
barrage.start(barragerItem.content, {
speed: barragerItem.speed,
headImg: barragerItem.headImg,
speed:35
});
currentIndex++;
if (currentIndex < totalComments) {
setTimeout(createBarrager, looperTime);
}
}
function stripHTML(html) {
return html.replace(/<\/?[^>]+(>|$)/g, "");
}
function flattenComments(comments) {
let result = [];
comments.forEach(function(comment) {
result.push(comment);
if (comment.children && comment.children.length > 0) {
result = result.concat(flattenComments(comment.children));
}
});
return result;
}
} catch (error) {
console.error('无法获取评论数据', error);
}
}
// 弹幕函数
function loadBarrager() {
// 获取当前页面的路径
var currentPath = "{{ .RelPermalink }}";
// 只有当 URL 包含 "post" 时才执行弹幕加载逻辑
if (currentPath.includes("post") || currentPath.includes("says")) {
// 调用封装的函数
fetchCommentsAndCreateBarrager(currentPath, {
looper_time: 3000, // 每条弹幕的发送间隔,单位毫秒
defaultAvatar: 'static/default-avatar.png', // 默认头像
defaultInfo: 'No comment', // 默认评论内容
defaultLink: '#', // 默认用户链接
speed: 10, // 弹幕速度,单位秒
});
}
}