效果如图
代码如下
// 1. 创建容器
const beianBox = document.createElement('div');
// 2. 设置样式:固定底部 + 固定宽度 + 居中
beianBox.style.position = 'fixed'; // 固定定位
beianBox.style.bottom = '0'; // 固定在底部
beianBox.style.width = '200px'; // 【核心】固定宽度为 200px
// 【核心】水平居中技巧:先推到屏幕中间,再往回拉一半宽度
beianBox.style.left = '50%';
beianBox.style.transform = 'translateX(-50%)';
beianBox.style.textAlign = 'center'; // 文字在 200px 的盒子内也居中
beianBox.style.padding = '10px 0'; // 上下内边距
beianBox.style.backgroundColor = '#fff';// 背景色(防止内容穿透)
beianBox.style.zIndex = '9999'; // 层级置顶
// 可选:加个圆角和阴影,让它看起来像一个悬浮的小卡片
beianBox.style.borderRadius = '5px 5px 0 0';
beianBox.style.boxShadow = '0 -2px 10px rgba(0,0,0,0.1)';
// 3. 设置内容
beianBox.innerHTML = `
<a href="https://beian.miit.gov.cn/" target="_blank" style="color: #333; text-decoration: none; font-size: 12px;">
京ICP备XXXXXXXX号
</a>
`;
// 4. 添加到页面
document.body.appendChild(beianBox);
// 5. 【提示】关于 body 的 padding-bottom
// 因为现在宽度只有200px,挡住内容的概率降低了。
// 但如果你依然希望页面底部留白,可以保留下面这行;
// 如果觉得不需要留白,可以注释掉。
document.body.style.paddingBottom = '60px';
Memos Blog