原生JavaScript:模态框scrollTop函数失效问题
问题解决:模态框返回顶部按钮无法隐藏
问题根源
- 滚动判断对象错误:你把滚动事件绑定在了模态框容器
myFlix-react-case-study上,但判断显示/隐藏的条件用的是document.body.scrollTop和document.documentElement.scrollTop——模态框内部的滚动不会改变全局文档的滚动距离,所以按钮显示后,全局滚动距离没回到30px以下,就永远不会触发隐藏逻辑。 - 按钮定位冲突:按钮使用
position: fixed会相对于整个浏览器视口定位,而非模态框容器,滚动模态框时按钮位置不会跟着内容适配,体验也不符合预期。
修复方案
1. 修正JS滚动判断逻辑
获取模态框容器元素,直接用它的scrollTop值做判断:
// Logic for get to top button let getToTopButton = document.getElementById('btn-back-to-top'); const modalContainer = document.getElementById('myFlix-react-case-study'); // 监听模态框容器的滚动事件 modalContainer.addEventListener('scroll', scrollFunction); function scrollFunction() { // 用模态框自身的scrollTop判断滚动距离 if (modalContainer.scrollTop > 30) { getToTopButton.style.display = 'block'; } else { getToTopButton.style.display = 'none'; } }
2. 调整按钮定位(可选但推荐)
将按钮改为相对于模态框容器定位,确保它始终出现在模态框的右下角:
.button-up-container { position: absolute; right: 38px; bottom: 30px; width: 41px; } #btn-back-to-top { /* 去掉fixed,改用absolute继承父容器的定位 */ position: absolute; background: var(--primary-color) linear-gradient(to bottom right, var(--primary-color) 25%, var(--accent-color)); border: none; border-radius: 10px; color: var(--font-color); padding: 8px 16px; font-size: 22px; display: none; right: 0; bottom: 0; }
如果希望按钮在模态框滚动时始终固定在底部右侧,也可以用sticky定位:
#btn-back-to-top { position: sticky; bottom: 30px; right: 38px; margin-left: auto; /* 保留其他样式 */ }
完整修复后代码示例
HTML(无需修改)
<div id='myFlix-react-case-study' class="d-none modal-container "> <div class='case-study-modal'> <div class="button-up-container"> <button type="button" id="btn-back-to-top"> <i class="fas fa-arrow-up"></i> </button> </div> <div class="modal-title"> <p> Some title </p> </div> <div class="modal-content"> <p> Some content </p> </div> </div> </div>
CSS(修改定位部分)
.modal-container { position: fixed; padding: 20px; box-sizing: border-box; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); z-index: 999; overflow: scroll; display: grid !important; text-align: center; } .case-study-modal { margin: 80px auto 0px; display: inline-block; box-sizing: border-box; background: #fff; padding: 20px 30px; width: 100%; max-width: 1100px; text-align: left; border-radius: 15px; color: var(--primary-color); position: relative; /* 确保子元素absolute定位有效 */ } .button-up-container { position: absolute; right: 38px; bottom: 30px; width: 41px; } #btn-back-to-top { position: absolute; background: var(--primary-color) linear-gradient(to bottom right, var(--primary-color) 25%, var(--accent-color)); border: none; border-radius: 10px; color: var(--font-color); padding: 8px 16px; font-size: 22px; display: none; right: 0; bottom: 0; }
JS(修正判断逻辑)
// Logic for get to top button let getToTopButton = document.getElementById('btn-back-to-top'); const modalContainer = document.getElementById('myFlix-react-case-study'); modalContainer.addEventListener('scroll', scrollFunction); function scrollFunction() { if (modalContainer.scrollTop > 30) { getToTopButton.style.display = 'block'; } else { getToTopButton.style.display = 'none'; } }
内容的提问来源于stack exchange,提问作者Zhykiki
相关产品推荐
相关产品推荐

