修复了一个<div>未闭合的bug

优化分页和加载更多逻辑
This commit is contained in:
浪子 2025-03-26 09:14:45 +08:00
parent 13c3587762
commit 482e9a1e93
23 changed files with 112 additions and 146 deletions

View File

@ -1,5 +1,5 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('module/header.php'); ?>
<?php $this->need('header.php'); ?>
<header class="archive--header">
<h2 class="post--single__title">
<?php $this->archiveTitle(array(
@ -37,25 +37,10 @@ $memosId = is_numeric($memosId) ? intval($memosId) : null;
<?php $this->need('module/postlist.php'); ?>
<?php endif; ?>
<!-- 分页导航 -->
<?php $this->pageNav(
' ',
' ',
1,
'...',
array(
'wrapTag' => 'nav',
'wrapClass' => 'nav-links nav-links__comment',
'itemTag' => '',
'textTag' => 'span',
'itemClass' => 'page-numbers',
'currentClass' => 'page-numbers current',
'prevClass' => 'hidden',
'nextClass' => 'hidden'
)
); ?>
<?php $this->need('module/paging.php'); ?>
<?php else: ?>
<!-- 无结果 -->
<?php $this->need('module/notfound.php'); ?>
<?php endif; ?>
</div>
<?php $this->need('module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -17,7 +17,7 @@
is_single = false;
post_id = 0;
is_archive = false;
VERSION = "0.7.0";
VERSION = "0.7.1";
constructor() {
super();
this.initCopyright();
@ -46,7 +46,7 @@
}
initCopyright() {
const copyright = `<div class="site--footer__info">由<a href="https://www.typecho.org" target="_blank">Typecho</a> 驱动 <br>
Theme <a href="https://fatesinger.com/101971" target="_blank">farallon</a> by bigfa &nbsp;<br>Made with<a href="https://www.imsun.org" target="_blank"> Sun</a> / version ${this.VERSION}
Theme <a href="https://fatesinger.com/101971" target="_blank">farallon</a> by bigfa &nbsp;<br>Made with<a href="https://www.imsun.org" target="_blank">jkjoy</a> / version ${this.VERSION}
</div>`;
document.querySelector(".site--footer__content").insertAdjacentHTML("afterend", copyright);
document.querySelector(".icon--copryrights").addEventListener("click", () => {

View File

@ -5,7 +5,6 @@ document.addEventListener('DOMContentLoaded', function() {
const methodBtns = document.querySelectorAll('.donate-method-btn');
const qrImages = document.querySelectorAll('.qr-image');
let isVisible = false;
// 切换支付方式
function switchPayMethod(method) {
// 更新按钮状态
@ -15,7 +14,6 @@ document.addEventListener('DOMContentLoaded', function() {
btn.classList.add('active');
}
});
// 更新二维码显示
qrImages.forEach(img => {
img.classList.remove('active');
@ -24,21 +22,18 @@ document.addEventListener('DOMContentLoaded', function() {
}
});
}
// 点击打赏按钮切换二维码显示状态
function toggleQRCode(event) {
event.stopPropagation();
isVisible = !isVisible;
qrcodePanel.style.display = isVisible ? 'block' : 'none';
}
// 点击关闭按钮隐藏二维码
function hideQRCode(event) {
event.stopPropagation();
isVisible = false;
qrcodePanel.style.display = 'none';
}
// 点击二维码面板之外的地方隐藏二维码
function handleDocumentClick(event) {
if (isVisible && !qrcodePanel.contains(event.target) && !donateBtn.contains(event.target)) {
@ -46,12 +41,10 @@ document.addEventListener('DOMContentLoaded', function() {
qrcodePanel.style.display = 'none';
}
}
// 绑定事件监听器
donateBtn.addEventListener('click', toggleQRCode);
donateClose.addEventListener('click', hideQRCode);
document.addEventListener('click', handleDocumentClick);
// 绑定支付方式切换按钮事件
methodBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
@ -59,7 +52,6 @@ document.addEventListener('DOMContentLoaded', function() {
switchPayMethod(method);
});
});
// 初始化显示第一个支付方式
switchPayMethod('wechat');
});

View File

@ -1,13 +1,13 @@
document.addEventListener('click', function (e) {
// 检查点击的元素是否是 .post-read-more a
// 检查点击的元素是否是 .loadmore a
if (e.target.closest('.loadmore a')) {
e.preventDefault();
var btn = e.target.closest('.loadmore a');
var nextPage = btn.getAttribute('href');
var nextPage = btn.getAttribute('href');
// 防止重复点击
if (btn.classList.contains('loading')) return false;
btn.classList.add('loading');
btn.textContent = '加载中...';
btn.textContent = '加载中...';
// 发起 AJAX 请求
fetch(nextPage)
.then(response => {
@ -19,18 +19,23 @@ document.addEventListener('click', function (e) {
.then(data => {
// 创建一个临时的 DOM 元素来解析返回的 HTML
var parser = new DOMParser();
var htmlDoc = parser.parseFromString(data, 'text/html');
// 找到新的文章
var newPosts = htmlDoc.querySelectorAll('.post--item');
// 找到新的"加载更多"按钮
var newBtn = htmlDoc.querySelector('.nav-links a');
// 获取文章列表容器和加载更多按钮
var postReadMore = document.querySelector('.nav-links');
var articleList = document.querySelector('.articleList');
var htmlDoc = parser.parseFromString(data, 'text/html');
// 调试代码:检查选择器
console.log('Searching for #loadpost:', htmlDoc.querySelectorAll('#loadpost'));
console.log('Searching for .nav-links:', htmlDoc.querySelector('.nav-links'));
// 找到新的文章和按钮
var newPosts = htmlDoc.querySelectorAll('#loadpost');
var newBtn = htmlDoc.querySelector('.nav-links a');
// 更健壮的元素选择
var articleList = document.querySelector('#loadposts') ||
document.querySelector('.posts-container') ||
document.body;
var postReadMore = document.querySelector('.nav-links');
if (newPosts.length > 0) {
newPosts.forEach(post => {
articleList.insertBefore(post, postReadMore);
});
// 使用 appendChild 替代 insertBefore
articleList.appendChild(post);
});
// 新文章淡入效果
Array.from(newPosts).forEach(post => {
post.style.opacity = 0;
@ -39,7 +44,7 @@ document.addEventListener('click', function (e) {
post.style.opacity = 1;
}, 10);
});
}
}
// 更新"加载更多"按钮或移除它
if (newBtn) {
btn.setAttribute('href', newBtn.getAttribute('href'));

View File

@ -71,18 +71,6 @@ function themeFields($layout) {
$layout->addItem($summary);
$cover= new Typecho_Widget_Helper_Form_Element_Text('cover', NULL, NULL, _t('文章封面'), _t('自定义文章封面'));
$layout->addItem($cover);
// $douban= new Typecho_Widget_Helper_Form_Element_Text('douban', NULL, NULL, _t('豆瓣API'), _t('自定义页面豆瓣API'));
// $layout->addItem($douban);
//$neodb= new Typecho_Widget_Helper_Form_Element_Text('neodb', NULL, NULL, _t('NeoDB API'), _t('自定义页面NeoDB API'));
//$layout->addItem($neodb);
// $tooot= new Typecho_Widget_Helper_Form_Element_Text('tooot', NULL, NULL, _t('Mastodon API'), _t('自定义页面Mastodon API'));
// $layout->addItem($tooot);
// $memos= new Typecho_Widget_Helper_Form_Element_Text('memos', NULL, NULL, _t('Memos地址'), _t(' 自定义页面Memos地址'));
// $layout->addItem($memos);
// $memosID= new Typecho_Widget_Helper_Form_Element_Text('memosID', NULL, NULL, _t('Memos ID'), _t('自定义页面Memos ID'));
// $layout->addItem($memosID);
// $memosnum= new Typecho_Widget_Helper_Form_Element_Text('memosnum', NULL, NULL, _t('Memos数量'), _t('自定义页面Memos数量'));
// $layout->addItem($memosnum);
}
/*

View File

@ -91,40 +91,11 @@ foreach ($normalPosts as $normalPost) {
$this->push($normalPost);
}
?>
<?php $this->need('module/header.php');?>
<?php $this->need('header.php');?>
<main class="site--main">
<div class="articleList">
<?php $this->need('module/postlist.php'); ?>
<?php if ($this->options->loadmore): ?>
<?php
$this->pageNav(
' ',
' ',
1,
'...',
array(
'wrapTag' => 'nav',
'wrapClass' => 'nav-links nav-links__comment',
'itemTag' => '',
'textTag' => 'span',
'itemClass' => 'page-numbers',
'currentClass' => 'page-numbers current',
'prevClass' => 'hidden',
'nextClass' => 'hidden'
)
);
?>
<?php else:?>
<?php
$nextPage = $this->_currentPage + 1;
$totalPages = ceil($this->getTotal() / $this->parameter->pageSize);
if ($this->_currentPage < $totalPages): ?>
<div class="nav-links">
<span class="loadmore"><?php $this->pageLink('加载更多', 'next'); ?></span>
<div class="articleList">
<?php $this->need('module/postlist.php'); ?>
<?php $this->need('module/paging.php'); ?>
</div>
<?php endif; ?>
<script src="<?php $this->options->themeUrl('assets/js/loadmore.js'); ?>"></script>
<?php endif; ?>
</div>
</main>
<?php $this->need('module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -1,6 +1,7 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<div id="loadposts">
<?php while($this->next()): ?>
<article class="post--item post--item__status" itemtype="http://schema.org/Article" itemscope="itemscope">
<article id="loadpost" class="post--item post--item__status" itemtype="http://schema.org/Article" itemscope="itemscope">
<div class="content">
<header>
<img src="<?php $this->options->logoUrl() ?>" class="avatar" width="48" height="48" />
@ -14,4 +15,5 @@
</div>
</div>
</article>
<?php endwhile; ?>
<?php endwhile; ?>
</div>

31
module/paging.php Normal file
View File

@ -0,0 +1,31 @@
<?php if ($this->options->loadmore): ?>
<?php
$this->pageNav(
' ',
' ',
1,
'...',
array(
'wrapTag' => 'nav',
'wrapClass' => 'nav-links nav-links__comment',
'itemTag' => '',
'textTag' => 'span',
'itemClass' => 'page-numbers',
'currentClass' => 'page-numbers current',
'prevClass' => 'hidden',
'nextClass' => 'hidden'
)
);
?>
<?php else:?>
<?php
$nextPage = $this->_currentPage + 1;
$totalPages = ceil($this->getTotal() / $this->parameter->pageSize);
if ($this->_currentPage < $totalPages):
?>
<div class="nav-links">
<span class="loadmore"><?php $this->pageLink('加载更多', 'next'); ?></span>
</div>
<script src="<?php $this->options->themeUrl('assets/js/loadmore.js'); ?>"></script>
<?php endif; ?>
<?php endif; ?>

View File

@ -1,10 +1,10 @@
<div id="loadposts">
<?php while($this->next()): ?>
<?php
// 获取当前文章的分类
$categories = $this->categories;
$memosMid = $this->options->memos; // 获取主题设置中的说说分类 mid
$isMemos = false;
// 检查当前文章是否属于说说分类
foreach ($categories as $category) {
if ($category['mid'] == $memosMid) {
@ -12,10 +12,10 @@
break;
}
}
// 根据是否为说说分类使用不同的显示模板
if ($isMemos):
?>
<div id="loadpost">
<article class="post--item post--item__status" itemtype="http://schema.org/Article" itemscope="itemscope">
<div class="content">
<header>
@ -29,9 +29,11 @@
<?php $this->excerpt(200, '...'); ?>
</div>
</div>
</article>
</article>
</div>
<?php else: ?>
<article class="post--item">
<div id="loadpost">
<article class="post--item" id="loadpost">
<div class="content">
<h2 class="post--title">
<a href="<?php $this->permalink() ?>">
@ -79,5 +81,7 @@
</a>
<?php endif; ?>
</article>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endwhile; ?>
</div>

View File

@ -1,5 +1,5 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<div class="post--cards">
<div class="post--cards" id="loadposts">
<?php while($this->next()): ?>
<?php // 获取文章图片
$default_thumbnail = Helper::options()->themeUrl . '/assets/images/nopic.svg';
@ -13,7 +13,7 @@
$imageToDisplay = $firstImage;
}
?>
<article class="post--card">
<article class="post--card" id="loadpost" itemscope itemtype="http://schema.org/Article">
<img src="<?php echo $imageToDisplay; ?>" alt="<?php $this->title() ?>" class="cover" itemprop="image"/>
<div class="content">
<h2 class="post--title">
@ -34,4 +34,5 @@
</div>
</div>
</article>
<?php endwhile; ?>
<?php endwhile; ?>
</div>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<section class="site--main">
<header class="archive--header">
<h1 class="post--single__title"><?php $this->title() ?></h1>
@ -57,4 +57,4 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
?>
</div>
</section>
<?php $this->need('./module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<div class="site--main">
<header class="archive--header">
<h1 class="post--single__title"><?php $this->title() ?></h1>
@ -33,4 +33,4 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php endwhile; ?>
</section>
</div>
<?php $this->need('./module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<script src="<?php $this->options->themeUrl('assets/js/db.js'); ?>"></script>
<section class="site--main">
<header class="archive--header">
@ -28,4 +28,4 @@ new Douban({
</script>
</div>
</section>
<?php $this->need('./module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$this->need('./module/header.php');
$this->need('header.php');
?>
<div class="site--main site--main__gears">
@ -77,25 +77,20 @@ $this->need('./module/header.php');
*/
function parseGoodsTable($content) {
$goods = array();
// 创建DOM对象
$dom = new DOMDocument();
libxml_use_internal_errors(true); // 禁用libxml错误
$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
libxml_clear_errors();
// 查找表格
$tables = $dom->getElementsByTagName('table');
if ($tables->length > 0) {
$table = $tables->item(0); // 获取第一个表格
$rows = $table->getElementsByTagName('tr');
$rows = $table->getElementsByTagName('tr');
// 跳过表头行
for ($i = 1; $i < $rows->length; $i++) {
$row = $rows->item($i);
$cells = $row->getElementsByTagName('td');
$cells = $row->getElementsByTagName('td');
// 确保有足够的单元格
if ($cells->length >= 5) {
$item = array(
@ -113,9 +108,7 @@ function parseGoodsTable($content) {
}
}
}
return $goods;
}
$this->need('./module/footer.php');
$this->need('footer.php');
?>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<section class="site--main">
<header class="archive--header">
<h1 class="post--single__title"><?php $this->title() ?></h1>
@ -26,4 +26,4 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php endif; ?>
</article>
</section>
<?php $this->need('./module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<div class="site--main">
<header class="archive--header">
<h1 class="post--single__title"><?php $this->title() ?></h1>
@ -148,4 +148,4 @@ img {
}
</style>
</div>
<?php $this->need('./module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<div class="site--main">
<header class="archive--header">
<h1 class="post--single__title"><?php $this->title() ?></h1>
@ -16,7 +16,7 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
$memos = $this->fields->memos ? $this->fields->memos : 'https://memos.imsun.org';
$memosID = $this->fields->memosID ? $this->fields->memosID : '1';
$memosnum = $this->fields->memosnum ? $this->fields->memosnum : '20';
?>
?>
<article class="post--single">
<script src="<?php $this->options->themeUrl('assets/js/marked.min.js'); ?>"></script>
<link rel="stylesheet" href="<?php $this->options->themeUrl('assets/css/lightbox.min.css'); ?>">
@ -32,7 +32,6 @@ const limit = 10; // 每页条数
let url = '<?php echo $memos; ?>';
let memosID = '<?php echo $memosID; ?>';
let memosnum = '<?php echo $memosnum; ?>';
function loadMemos(page) {
fetch(`${url}/api/v1/memo?creatorId=${memosID}&rowStatus=NORMAL&limit=${limit}&offset=${(page - 1) * limit}`)
.then(res => res.json())
@ -64,7 +63,6 @@ function loadMemos(page) {
// 这里可以添加一些用户提示错误发生的 HTML 更新
});
}
function Format(item) {
let date = getTime(new Date(item.createdTs * 1000).toString()),
content = item.content,
@ -94,7 +92,6 @@ function Format(item) {
text: text.replace(/\[(.*?)\]\((.*?)\)/g, '[链接]' + `${imgs?'[图片]':''}`)
}
}
// 页面时间格式化
function getTime(time) {
let d = new Date(time),
@ -105,16 +102,13 @@ function getTime(time) {
if (new Date().getFullYear() == ls[0]) return ls[1] + '月' + ls[2] + '日 ' + ls[3] +':'+ ls[4]
else return ls[0] + '年' + ls[1] + '月' + ls[2] + '日 ' + ls[3] +':'+ ls[4]
}
// 初始加载第一页
loadMemos(currentPage);
// 点击“加载更多”按钮时加载下一页
document.getElementById('loadmore').addEventListener('click', function() {
currentPage++;
loadMemos(currentPage);
});
</script>
<style>
div pre code {
@ -169,4 +163,4 @@ img {
}
</style>
</div>
<?php $this->need('./module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<link rel="stylesheet" href="<?php $this->options->themeUrl('assets/css/neodb.css'); ?>">
<script src="<?php $this->options->themeUrl('assets/js/neodb.js'); ?>"></script>
<section class="site--main">
@ -15,14 +15,14 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
</header>
<div class="site--main">
<div class="neodb-container"></div>
<?php $neodb = $this->fields->neodb ? $this->fields->neodb : 'https://neodb.imsun.org'; ?>
<script>
const neodb = new NeoDB({
container: ".neodb-container",
baseAPI: "<?php echo $neodb; ?>/api",
types: ["book", "movie", "tv", "music", "game"],
});
</script>
</div>
<?php $neodb = $this->fields->neodb ? $this->fields->neodb : 'https://neodb.imsun.org'; ?>
<script>
const neodb = new NeoDB({
container: ".neodb-container",
baseAPI: "<?php echo $neodb; ?>/api",
types: ["book", "movie", "tv", "music", "game"],
});
</script>
</div>
</section>
<?php $this->need('./module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -5,7 +5,7 @@
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<section class="site--main">
<header class="archive--header">
<h1 class="post--single__title"><?php $this->title() ?></h1>
@ -30,4 +30,4 @@ if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php endif; ?>
</div>
</section>
<?php $this->need('./module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -1,5 +1,5 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('./module/header.php'); ?>
<?php $this->need('header.php'); ?>
<section class="site--main">
<header class="archive--header">
<h1 class="post--single__title"><?php $this->title() ?></h1>
@ -14,5 +14,5 @@
<?php endif; ?>
</article>
</section>
<?php $this->need('module/footer.php'); ?>
<?php $this->need('footer.php'); ?>

View File

@ -1,5 +1,5 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('module/header.php'); ?>
<?php $this->need('header.php'); ?>
<style>
#toc {font-size:14px;padding:10px 15px;background-color:var(--farallon-background-gray);border-radius:10px;margin-bottom:20px}
#toc summary{cursor:pointer}
@ -126,7 +126,7 @@
<?php $this->need('module/related.php'); ?>
<?php endif; ?>
<?php if ($this->allow('comment')): ?>
<?php $this->need('./module/comments.php'); ?>
<?php $this->need('module/comments.php'); ?>
<?php endif; ?>
<!--翻页-->
<nav class="navigation post-navigation is-active">
@ -145,4 +145,4 @@
<script src="<?php $this->options->themeUrl('assets/js/post.js'); ?>"></script>
<link rel="stylesheet" href="<?php $this->options->themeUrl('assets/css/lightbox.min.css'); ?>">
<script src="<?php $this->options->themeUrl('assets/js/lightbox-plus-jquery.min.js'); ?>"></script>
<?php $this->need('module/footer.php'); ?>
<?php $this->need('footer.php'); ?>