You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RMarkdown悬浮目录因隐藏区块无法滚动问题求助

解决RMarkdown toc_float隐藏区块后滚动卡住的问题

直接用style="display: none;"隐藏章节区块会让元素脱离文档流,toc_float的滚动逻辑依赖页面所有元素的位置信息,这就导致目录滚动计算出错。要保留目录里的"Secondary variables"条目,同时解决滚动问题,换个方式控制区块可见性就行:

修改步骤:

  • 移除原代码里的style="display: none;",改用自定义CSS类控制隐藏
  • 写一段CSS定义隐藏状态的样式(让元素留在文档流里,只是不显示内容)
  • 调整JS切换函数,通过添加/移除类来控制显示隐藏

完整修改后的代码:

---
title: "TEST"
output: 
  html_document:
    number_sections: true
    toc: true
    toc_float:
      collapsed: true
      smooth_scroll: true
    toc_depth: 2
    section-divs : true
---

<style>
/* 定义隐藏类:元素保留在文档流,仅隐藏内容 */
.hidden-section {
  height: 0;
  overflow: hidden;
  margin: 0;
  padding: 0;
  border: none;
}
/* 显示状态恢复默认样式 */
.hidden-section.show {
  height: auto;
  margin: 1em 0;
  padding: 0;
}
</style>

# Some categorie{-}

**Description :**  
blablabla

Click to show other variables  
<button name="button" onclick="toggleVisibility('other_var')" >All variables</button> 

## Principal variables {- .tabset}
### var1
### var2

## Secondary variables{- .tabset #other_var .hidden-section}
### va3
### var4

<script>
function toggleVisibility(id) {
  const section = document.getElementById(id);
  section.classList.toggle('show');
}
</script>

为啥这么改?

用height:0+overflow:hidden隐藏区块时,元素依然存在于文档流中,toc_float能正确读取页面的布局信息,不会出现滚动卡住的问题。同时因为没加.unlisted,"Secondary variables"会正常显示在浮动目录里。

内容的提问来源于stack exchange,提问作者Harrison Ford

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 16:52:08