Files
blog/public/js/main.js
T

120 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-07-28 08:31:54 +08:00
// 判断是否为移动端
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端
}
}
// 文章图片灯箱
2024-07-28 13:32:03 +08:00
window.ViewImage && ViewImage.init('.post-content img');
2024-07-28 08:31:54 +08:00
// 激活返回顶部插件
$(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
});
2024-07-28 21:46:00 +08:00
2024-07-30 09:35:12 +08:00
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,
2024-07-31 09:54:29 +08:00
height:"33px"
2024-07-30 09:35:12 +08:00
});
if (totalComments !== 0) {
2024-07-28 21:46:00 +08:00
createBarrager();
2024-07-28 08:31:54 +08:00
}
function createBarrager() {
2024-07-30 09:35:12 +08:00
const currentComment = flattenedComments[currentIndex];
const barragerItem = {
2024-07-31 09:54:29 +08:00
headImg: currentComment.avatar + "?s=33" || options.defaultAvatar,
2024-07-30 09:35:12 +08:00
content: stripHTML(currentComment.orig) || options.defaultInfo,
href: currentComment.link || options.defaultLink,
speed: options.speed || 10
2024-07-28 08:31:54 +08:00
};
2024-07-30 09:35:12 +08:00
// 发送弹幕
barrage.start(barragerItem.content, {
speed: barragerItem.speed,
headImg: barragerItem.headImg,
2024-07-31 09:54:29 +08:00
speed:35
2024-07-30 09:35:12 +08:00
});
currentIndex++;
if (currentIndex < totalComments) {
setTimeout(createBarrager, looperTime);
2024-07-28 08:31:54 +08:00
}
2024-07-31 16:20:41 +08:00
2024-07-28 08:31:54 +08:00
}
2024-07-28 21:46:00 +08:00
function stripHTML(html) {
return html.replace(/<\/?[^>]+(>|$)/g, "");
}
function flattenComments(comments) {
2024-07-30 09:35:12 +08:00
let result = [];
2024-07-28 21:46:00 +08:00
comments.forEach(function(comment) {
result.push(comment);
if (comment.children && comment.children.length > 0) {
result = result.concat(flattenComments(comment.children));
}
});
return result;
}
2024-07-30 09:35:12 +08:00
} catch (error) {
console.error('无法获取评论数据', error);
}
2024-07-28 08:31:54 +08:00
}
2024-07-30 09:35:12 +08:00