You need to enable JavaScript to run this app. 如何实现点击按钮后折叠的参考书目区块显示在按钮下方而非并排
优惠活动
大模型
产品
解决方案
定价
更多

如何实现点击按钮后折叠的参考书目区块显示在按钮下方而非并排

问题根因
  • 你的footer采用了默认横向排列的flex布局,导致承载按钮的.footerStyling容器和承载参考书目内容的<section>默认左右并排,内容展开后会挤占按钮位置导致按钮左移,同时内容和按钮横向排列
  • .bibliographySources的padding属性写法错误,数值未带单位导致顶部内边距不生效
  • 多余的bootstrap row类会自带左右负margin,可能导致内容宽度异常
  • 按钮id属性错误带了#前缀,JS存在冗余的重复DOM查询
修正方案

1. 修正HTML结构

将参考书目容器整体移入.footerStyling容器内,保证按钮和内容属于同一个纵向排列的块级上下文:

<!-- Footer -->
<footer>
  <div class="footerStyling" id="footer">
    <a onclick="showSources()" id="bibliography" class="btn rounded btn-outline-light sourcesButton">Sources</a>
    <section>
      <div class="sourceStyling bibliographySources" id="bibliographySources">
        <p>
          2040年实现碳中和(Oleksyn, 2021)<br>
          <br>全球2050年碳中和目标(Oleksyn, 2021)<br>
          <br>展示各行业排放来源的环形图(萨斯喀彻温省, 2020)<br>
          <br>萨斯喀彻温电力公司计划2030年前通过太阳能等方式供电(Praill, 2018)<br>
          <br>加拿大温室气体排放热力图 -(萨斯喀彻温省, 2020)<br>
          <br>2018年萨斯喀彻温省二氧化碳排放量7640万吨 -(萨斯喀彻温省, 2020)<br>
          <br>2018年加拿大二氧化碳排放量7.28亿吨 -(加拿大政府, 2019)<br>
          <br>加拿大联邦车队温室气体排放来源 -(萨斯喀彻温省, 2020)<br>
          <br>221兆瓦在役风电装机容量分布在143台涡轮机,占萨斯喀彻温省发电量的3.1% -(萨斯喀彻温风电协会, 2020)<br>
          <br>
        </p>
        <h3>
          参考文献
        </h3>
        <p>
          <br>加拿大政府. (2019). 加拿大政府. 摘自温室气体排放:<br> https://www.canada.ca/en/environment-climate-change/services/environmental-indicators/greenhouse-gas-emissions.html<br>
          <br>Oleksyn, M. (2021,4月27日). SES报告认为萨斯喀彻温电力公司2040年实现电网碳中和具备可行性. 摘自阿尔伯特亲王城每日先驱报:<br> https://paherald.sk.ca/2021/04/27/ses-report-sees-carbon-neutral-grid-by-2040-possible-for-saskpower/<br>
          <br>Praill, C. (2018,6月19日). 萨斯喀彻温电力公司宣布省内首个10兆瓦太阳能项目. 摘自环球新闻:<br> https://globalnews.ca/news/4284462/saskpower-announces-10-megawatt-solar-project-first-in-sask/<br>
          <br>萨斯喀彻温省. (2020). 摘自温室气体排放:<br> https://www.saskatchewan.ca/residents/environment-public-health-and-safety/state-of-the-environment/saskatchewans-state-of-the-environment/greenhouse-gas-emissions<br>
          <br>萨斯喀彻温风电协会. (2020). 摘自萨斯喀彻温风电场位置:<br> https://www.saskwind.ca/location-of-sk-wind-farms<br>
        </p>
      </div>
    </section>
  </div>
</footer>

改动点:移除了参考书目容器的row类,修正按钮id的#前缀,将<section>整体移入.footerStyling内部。

2. 修正CSS代码

修正padding写法,调整布局适配:

.sourcesButton {
  position: relative;
}

.footerStyling {
  color: white;
  padding: 1rem;
  text-align: center; /* 让按钮居中显示 */
}

.sourceStyling {
  color: white;
  padding: 2rem;
  text-align: left; /* 参考内容左对齐,和居中的按钮区分开 */
}

.bibliographySources {
  padding: 1rem 0 0 0; /* 修正单位缺失问题,顶部保留1rem内边距 */
  clear: both;
}

footer {
  display: flex;
  background-color: #655A06;
  justify-content: center;
}

3. 优化JS代码

移除冗余的DOM查询,修正变量命名问题:

// 缓存DOM节点,避免重复查询
const bibliographySources = document.getElementById("bibliographySources");
bibliographySources.style.display = "none";

function showSources() {
  bibliographySources.style.display = bibliographySources.style.display === "none" ? "block" : "none";
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 17:45:10