为z-index:1的fixed评论区设置margin,实现列表间距适配的问题
问题
页面包含可滚动的列表,底部有一个position: fixed、z-index: 1的评论区。需要实现列表底部与评论区顶部始终保持固定垂直间距,但当前遇到两个问题:
- 给列表的
.meaning类设置margin-bottom,当评论区行数增加、高度变大时,预留的边距会不足,导致列表内容被评论区遮挡 - 给评论区设置
margin-top,由于固定定位元素脱离文档流,margin-top无法影响列表布局,完全不生效
解决方案
方法:动态计算列表底部内边距(最稳妥)
固定定位元素不参与文档流布局,因此需要通过JavaScript动态获取评论区的实时高度,给列表设置「评论区高度 + 固定间距」的padding-bottom,这样无论评论区高度如何变化,列表底部都会留出足够空间,不会被遮挡。
修改后的代码
HTML
<!DOCTYPE html> <html> <body> <ol class="meaning"> <li class="meaning-word">含义1</li> <ul class="sentences"> <li class="sentence-en">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li> <li class="sentence-de">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</li> </ul> <li class="meaning-word">含义2</li> <ul class="sentences"> <li class="sentence-en">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li> <li class="sentence-de">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</li> </ul> </ol> <div class="comment"> <p>这是文本信息的第一行。</p> <!-- 可添加更多行测试高度变化 --> <!-- <p>这是文本信息的第二行。</p> --> </div> </body> </html>
CSS
.meaning { list-style-type: none; counter-reset: item; hyphens: auto; font-size: calc(0.7em + 1.5vw); /* 初始预留最小间距,后续JS会动态更新 */ padding-bottom: 60px; } .meaning > li { position: relative; } .meaning > li::before { content: counter(item); counter-increment: item; position: absolute; top: 0; text-align: center; margin-left: calc(-0.7em - 2.5vw); } .meaning-word { margin-top: 50px; } .sentences { list-style-type: none; padding-left: 0; font-size: calc(0.7em + 1.5vw); margin-top: 30px; } .sentences > li.sentence-en { color: #5d78e5; } .sentences > li.sentence-de { margin-top: 5px; } .comment { position: fixed; bottom: 0; left: 0; width: 100%; background-color: #fdf1d1; color: #f92504; font-size: calc(0.5em + 2vw); margin: 0; padding: 5px 4vw; box-sizing: border-box; z-index: 1; }
JavaScript
const meaningList = document.querySelector('.meaning'); const commentArea = document.querySelector('.comment'); const FIXED_GAP = 20; // 自定义固定间距,单位px // 设置列表底部内边距的核心函数 function updateListPadding() { const commentHeight = commentArea.offsetHeight; meaningList.style.paddingBottom = `${commentHeight + FIXED_GAP}px`; } // 初始化执行 updateListPadding(); // 监听窗口缩放(评论区高度可能随窗口变化) window.addEventListener('resize', updateListPadding); // 监听评论区内容变化(比如动态添加/删除文本) const observer = new MutationObserver(updateListPadding); observer.observe(commentArea, { childList: true, subtree: true, characterData: true });
内容的提问来源于stack exchange,提问作者Shakalakah
相关产品推荐
相关产品推荐

