简短结论:hover 菜单的鼠标跟随预览图不显示,而图片地址、DOM 结构和样式看起来都正常。用 getBoundingClientRect() 检查后发现,预览容器 #hm-cursor 的宽度是 0,所以什么都看不到。给容器加上内联样式,强制指定宽高后恢复正常。
现象
菜单行本身正常,黑色幕布和文字动效也正常,但鼠标移到某一行时,旁边没有出现预览图,控制台也没有报错。
排查过程
按”先排除数据,再排除加载,再排除结构,最后看尺寸”的顺序:
| 步骤 | 检查内容 | 结果 |
|---|---|---|
| 1 | REST API 返回的特色图地址 | 每篇都有完整的 https:// 地址,正常 |
| 2 | 用 new Image() 加载其中一张图 | 加载成功,图片本身没问题 |
| 3 | .hm-item 数量、data-image 是否有值、#hm-cursor 的 display / position / z-index | 都正常 |
| 4 | #hm-cursor 的父元素是否有 overflow / clip-path / transform 裁切 | 没有 |
| 5 | 用 getBoundingClientRect() 查看预览框的实际尺寸 | 宽度为 0 |
下面是每一步用到的控制台代码,在浏览器开发者工具的 Console 里运行。
步骤 1:确认 REST API 能拿到图片地址
fetch('/wp-json/wp/v2/posts?per_page=5&categories=CATEGORY_ID&_embed=1')
.then(r => r.json())
.then(posts => {
posts.forEach(p => {
const img = p?._embedded?.["wp:featuredmedia"]?.[0]?.source_url;
console.log(p.title.rendered, '→', img);
});
});步骤 2:确认图片本身能加载
const img = new Image();
img.crossOrigin = "anonymous";
img.onload = () => console.log("✅ 图片加载成功", img.src);
img.onerror = (e) => console.log("❌ 图片加载失败", e);
img.src = "图片 URL";步骤 3:确认 DOM 和样式状态
const items = document.querySelectorAll(".hm-item");
const cursor = document.getElementById("hm-cursor");
console.log("hm-item 数量:", items.length);
console.log("hm-cursor 存在:", !!cursor);
items.forEach((item, i) => {
console.log(`第${i+1}项 data-image:`, item.getAttribute("data-image"));
});
if (cursor) {
const style = getComputedStyle(cursor);
console.log("cursor display:", style.display);
console.log("cursor position:", style.position);
console.log("cursor z-index:", style.zIndex);
}步骤 4:确认父元素没有裁切
这段代码会把预览框强制显示出来,并逐层检查父元素:
(function() {
var cur = document.getElementById("hm-cursor");
cur.style.opacity = "1";
cur.style.transform = "translate3d(100px, 100px, 0)";
var el = cur.parentElement;
while (el) {
var s = getComputedStyle(el);
if (s.overflow !== 'visible' || s.clipPath !== 'none' || s.transform !== 'none') {
console.log("⚠️", el.tagName, el.className);
console.log(" overflow:", s.overflow);
console.log(" clip-path:", s.clipPath);
console.log(" transform:", s.transform);
}
el = el.parentElement;
}
console.log("检查完毕");
})();只显示”检查完毕”,说明父元素没有裁切。但页面上依然看不到预览框,哪怕它的背景是黑色的,应该至少能看到一个黑色方块。
关键一步:检查实际尺寸
(function() {
var cur = document.getElementById("hm-cursor");
var rect = cur.getBoundingClientRect();
console.log("cursor 实际位置:", JSON.stringify(rect.toJSON()));
console.log("cursor 子元素数量:", cur.children.length);
})();输出里 "width": 0,而高度是 202.39。
高度 202.39 约等于 220 × 0.92,这是样式里默认的 scale(.92) 缩放造成的,属于正常。所以问题只出在宽度上:宽度被压成了 0,预览框存在,却是一条看不见的细线。
解决办法
给容器加上内联样式,强制指定宽高和定位。内联样式的优先级最高,可以绕过其他样式对它的干扰:
<div id="hm-cursor" class="hm-cursor" aria-hidden="true"
style="width:320px;max-width:320px;height:220px;position:fixed;top:0;left:0;"></div>改完后预览图恢复正常。
如何确认真正的原因
内联样式能解决问题,但它只是绕过去,没有说明是哪条样式把宽度改成了 0。想查出来:
- 在页面上右键 →「检查」,打开开发者工具。
- 在 Elements 面板里选中
#hm-cursor。 - 切换到 Computed(计算样式)面板,找到
width。 - 点开
width左边的小三角,下面会列出是哪个样式文件、哪一行的规则最终决定了这个值。
找到之后,就能直接针对那条规则处理,比如换一个不冲突的类名,或者提高自己规则的优先级。
[此处补充:你实际查到的是哪条规则,以及最终采用的修复方式。如果还没有查,这一节可以先留着只保留上面四步。]
小结
元素”明明存在却看不见”时,除了检查 display、opacity 和 z-index,还可以用 getBoundingClientRect() 看它实际有没有占据空间。宽度或高度为 0,就是这类问题最常见的原因之一。