fix: 增强桌面端文章目录滚动跟随功能
- 添加独立的 window scroll 事件监听,确保滚动时触发更新 - 获取 headIndex 实例并手动调用 updateCurrent 方法 - 添加 PJAX 完成后的目录更新逻辑 - 使用节流优化滚动性能(100ms 延迟)
This commit is contained in:
26
sidebar.php
26
sidebar.php
@@ -524,6 +524,7 @@ $author_desc = get_option('argon_sidebar_author_description');
|
||||
// 检查 jQuery 和 headIndex 是否都已加载
|
||||
if (typeof jQuery !== 'undefined' && typeof jQuery.fn.headIndex === 'function') {
|
||||
jQuery(document).ready(function($) {
|
||||
// 初始化 headIndex
|
||||
$(document).headIndex({
|
||||
articleWrapSelector: '#post_content',
|
||||
indexBoxSelector: '#leftbar_catalog',
|
||||
@@ -536,6 +537,31 @@ $author_desc = get_option('argon_sidebar_author_description');
|
||||
offset: 80,
|
||||
defaultExpanded: true
|
||||
});
|
||||
|
||||
// 获取 headIndex 实例
|
||||
var headIndexInstance = $(document).data('headIndex');
|
||||
|
||||
// 添加额外的滚动监听,确保目录跟随
|
||||
var scrollTimer = null;
|
||||
$(window).on('scroll.desktopCatalog', function() {
|
||||
if (scrollTimer) {
|
||||
clearTimeout(scrollTimer);
|
||||
}
|
||||
scrollTimer = setTimeout(function() {
|
||||
if (headIndexInstance && typeof headIndexInstance.updateCurrent === 'function') {
|
||||
headIndexInstance.updateCurrent();
|
||||
}
|
||||
}, 100);
|
||||
});
|
||||
|
||||
// PJAX 后重新绑定
|
||||
$(document).on('pjax:complete', function() {
|
||||
if (headIndexInstance && typeof headIndexInstance.updateCurrent === 'function') {
|
||||
setTimeout(function() {
|
||||
headIndexInstance.updateCurrent();
|
||||
}, 300);
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// 如果还没加载完成,等待后重试
|
||||
|
||||
Reference in New Issue
Block a user