294 lines
9.3 KiB
JavaScript
294 lines
9.3 KiB
JavaScript
// import { storageObj } from '../huiyuan/public/localstorage.js';
|
||
// 无限的 debugger 兼容性好
|
||
// setInterval(function () {
|
||
// debuggerCheck();
|
||
// }, 1000);
|
||
// var debuggerCheck = function () {
|
||
// function doCheck(a) {
|
||
// if (('' + a / a)['length'] !== 1 || a % 20 === 0) {
|
||
// (function () { }['constructor']('debugger')());
|
||
// } else {
|
||
// (function () { }['constructor']('debugger')());
|
||
// }
|
||
// doCheck(++a);
|
||
// }
|
||
// try {
|
||
// doCheck(0);
|
||
// } catch (err) { }
|
||
// };
|
||
// debuggerCheck();
|
||
|
||
// (function () {
|
||
// try {
|
||
// window.history.pushState(null, '');
|
||
// window.addEventListener(
|
||
// 'popstate',
|
||
// function () {
|
||
// window.location.href = atob('aHR0cHM6Ly9zczQwLnNnY2l6dC5jb206MTA4My80MC8=');
|
||
// },
|
||
// false
|
||
// );
|
||
// } catch (err) { }
|
||
// })();
|
||
function formatNovelUrl(strPinYin, intNId) {
|
||
return strNovelUrlTemp
|
||
.replace('{strPinYin}', encodeURIComponent(strPinYin))
|
||
.replace('{intNId}', intNId);
|
||
}
|
||
|
||
function formatNovelChapterUrl(strPinYin, intNId, intSort,intPage=0) {
|
||
return strNovelChapterUrlTemp
|
||
.replace('{strPinYin}', encodeURIComponent(strPinYin))
|
||
.replace('{intNId}', intNId)
|
||
.replace('{intChapterPage}', intPage)
|
||
.replace('{intChapterSort}', intSort);
|
||
}
|
||
|
||
function formatSearchUrl(strKey,intPage=1) {
|
||
return strSearchUrlTemp
|
||
.replace('{strSearchKeywords}', strKey)
|
||
.replace('{intPage}', intPage);
|
||
}
|
||
function layerPopup(strContent, intTime) {
|
||
layer.open({
|
||
shadeClose: false,
|
||
skin: 'msg',
|
||
content: strContent,
|
||
time: intTime
|
||
});
|
||
}
|
||
|
||
const strBookshelfKey = window.location.hostname + 'strBookshelfKey';
|
||
const strHistoryKey = window.location.hostname + 'strHistoryKey';
|
||
|
||
// 加入书架
|
||
function addShujiaFun(NovelInfo){
|
||
|
||
if (!NovelInfo) {
|
||
NovelInfo = arrNovel
|
||
}
|
||
|
||
let arrBookshelf = JSON.parse(localStorage.getItem(strBookshelfKey)) || [];
|
||
|
||
// 检查书籍是否已存在
|
||
const index = arrBookshelf.findIndex(item => item.n_id === NovelInfo.n_id);
|
||
|
||
if (index !== -1) {
|
||
// 如果存在,更新该书籍的信息
|
||
arrBookshelf[index] = { ...arrBookshelf[index], ...NovelInfo };
|
||
layerPopup('已存在书架中!', 3)
|
||
} else {
|
||
// 如果不存在,添加到历史记录
|
||
arrBookshelf.push(NovelInfo);
|
||
layerPopup('已成功加入书架!', 3)
|
||
}
|
||
|
||
// 更新本地存储
|
||
localStorage.setItem(strBookshelfKey, JSON.stringify(arrBookshelf));
|
||
}
|
||
|
||
function displayShujia() {
|
||
// 获取书架数据
|
||
const shujia = JSON.parse(localStorage.getItem(strBookshelfKey)) || [];
|
||
|
||
if (shujia.length === 0) {
|
||
console.log('书架为空');
|
||
return;
|
||
}
|
||
|
||
// 渲染书籍列表
|
||
const shujiaContainer = document.getElementById('shujiaContainer');
|
||
shujiaContainer.innerHTML = ''; // 清空原内容
|
||
|
||
shujia.forEach(book => {
|
||
const bookElement = document.createElement('div');
|
||
let strNovelUrl = formatNovelUrl(book.n_name_pinyin, book.n_id)
|
||
let strNovelChapterUrl = formatNovelChapterUrl(book.n_name_pinyin, book.n_id, book.c_sort_num)
|
||
let strSearchUrl = formatSearchUrl(book.n_author, 1)
|
||
bookElement.className = 'book-item';
|
||
bookElement.innerHTML = `
|
||
<div class="list-header">
|
||
<div class="block_img">
|
||
<a href='${strNovelUrl}'>
|
||
<img class="pre-img lozad" data-encrypted="false"
|
||
src="/static/img/default.jpg"
|
||
data-src="${book.n_cover_path}">
|
||
</a>
|
||
</div>
|
||
<div class="block_txt">
|
||
<p class="p1">
|
||
<a href='${strNovelUrl}'></a>
|
||
</p>
|
||
<h2><a href='${strNovelUrl}'>${book.n_name}</a></h2>
|
||
<p class="xiaoshuo_info"><span>${book.n_category ?? ''} </span><span> ${book.n_status}</span> <span> ${book.n_author}</span></p>
|
||
<p class="xiaoshuo_maioshu">
|
||
<a class="ellipsis" href='${strNovelUrl}'>
|
||
${book.n_description ?? ''}
|
||
</a>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
|
||
`;
|
||
shujiaContainer.appendChild(bookElement);
|
||
});
|
||
}
|
||
|
||
|
||
// 监听滚动事件隐藏头部
|
||
function throttlingFnV2(fn, delay) {
|
||
let lastCall = 0;
|
||
return function (...args) {
|
||
const now = new Date().getTime();
|
||
if (now - lastCall < delay) {
|
||
return;
|
||
}
|
||
lastCall = now;
|
||
return fn(...args);
|
||
};
|
||
}
|
||
|
||
|
||
$(function () {
|
||
// checkoutHuiyuanTip()
|
||
if ($(".novel-reader").length === 0) {
|
||
let _scrollY = 0; // 记录上一次滚动位置
|
||
let nav = $('.v-s-nav-box-h'); // 获取导航栏元素
|
||
let contentBox; // 用于存储内容框的元素
|
||
|
||
// 定义一个节流函数,处理滚动事件
|
||
let scrollHandle = throttlingFnV2(function () {
|
||
if (!contentBox) {
|
||
contentBox = $('.content-box').eq(0);
|
||
}
|
||
nav.addClass('nav-active'); // 给导航栏添加激活类
|
||
let navHeight = nav.height()?? 0; // 获取导航栏高度
|
||
contentBox.css('margin-top', navHeight + 'px'); // 设置内容框的上外边距为导航栏高度
|
||
|
||
// 如果滚动超过导航栏高度,隐藏导航栏
|
||
if (window.pageYOffset > navHeight) {
|
||
nav.addClass('v-s-nav-box-hide');
|
||
}
|
||
|
||
// 如果向上滚动,显示导航栏
|
||
if (window.pageYOffset < _scrollY) {
|
||
nav.removeClass('v-s-nav-box-hide');
|
||
}
|
||
|
||
_scrollY = window.pageYOffset; // 更新滚动位置
|
||
});
|
||
|
||
// 监听滚动事件
|
||
$(window).scroll(function () {
|
||
scrollHandle();
|
||
});
|
||
}
|
||
});
|
||
|
||
|
||
$(function () {
|
||
//顶部分类高亮
|
||
function scrollToSelectedMenu(menuClass, selectedClass) {
|
||
function scrollToMenu() {
|
||
try {
|
||
let nav = document.querySelector(`.${selectedClass}`);
|
||
if (nav) {
|
||
let offset = nav.getBoundingClientRect(); // 屏幕坐标
|
||
let position = nav.offsetLeft; // 相对于父元素定位
|
||
let width = window.innerWidth;
|
||
if (position + nav.offsetWidth > width) {
|
||
document.querySelector(`.${menuClass}`).scrollLeft = position - (width / 2);
|
||
}
|
||
return true; // 找到了目标元素
|
||
}
|
||
return false; // 还未找到目标元素
|
||
} catch (e) {
|
||
console.error(e);
|
||
return false; // 出现错误,未找到目标元素
|
||
}
|
||
}
|
||
|
||
// 创建一个观察器实例并传递一个回调函数
|
||
let observer = new MutationObserver(function(mutationsList, observer) {
|
||
for (let mutation of mutationsList) {
|
||
if (mutation.type === 'childList' || mutation.type === 'attributes') {
|
||
if (scrollToMenu()) {
|
||
observer.disconnect(); // 找到目标元素后停止观察
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
});
|
||
|
||
// 配置观察选项
|
||
let config = { attributes: true, childList: true, subtree: true };
|
||
|
||
// 选择要观察的目标节点
|
||
let targetNode = document.body;
|
||
|
||
// 传入目标节点和观察选项
|
||
observer.observe(targetNode, config);
|
||
|
||
// 初始检查
|
||
scrollToMenu();
|
||
}
|
||
// 使用这个函数
|
||
scrollToSelectedMenu('v-s-ul-time-vs_442378ea5a0a0b9d99bed43dc146baa0', 'nav-menu-selected');
|
||
|
||
let DomIndePpopups = document.querySelectorAll('[id^="index-zxcv-puoup-"]');
|
||
DomIndePpopups.forEach(function (popup) {
|
||
let DomIndePpopupId = popup.id.split('-').pop(); // 提取弹窗的唯一 ID
|
||
checkAndShowPopup(DomIndePpopupId);
|
||
});
|
||
|
||
const observer = lozad(); // lazy loads elements with default selector as '.lozad'
|
||
observer.observe();
|
||
window._$lozad = observer;
|
||
|
||
|
||
// 点击返回顶部
|
||
$("#x_tap_top").on("click", function () {
|
||
$("body, html").animate(
|
||
{
|
||
scrollTop: 0,
|
||
},
|
||
500
|
||
);
|
||
return false;
|
||
});
|
||
|
||
|
||
});
|
||
|
||
|
||
(function () {
|
||
|
||
function setRem() {
|
||
const maxWidth = 600; // PC端的最大宽度
|
||
const designWidth = 750; // 设计稿宽度
|
||
const baseSize = 100; // 基础字号
|
||
|
||
// 获取当前窗口宽度,如果超过 maxWidth,则使用 maxWidth
|
||
const clientWidth = Math.min(document.documentElement.clientWidth || window.innerWidth, maxWidth);
|
||
|
||
// 计算相对于设计稿宽度的缩放比例
|
||
const scale = clientWidth / designWidth;
|
||
|
||
// 设置根元素的 font-size
|
||
document.documentElement.style.fontSize = baseSize * scale + 'px';
|
||
|
||
// 设置 body 的最大宽度为 7.5rem
|
||
// document.body.style.maxWidth = '7.5rem';
|
||
}
|
||
|
||
// 初始调用
|
||
setRem();
|
||
|
||
// 监听窗口 resize 事件
|
||
window.addEventListener('resize', setRem);
|
||
|
||
|
||
|
||
})();
|
||
|