Pacman吞吃圆点失效求助:overflow:hidden伪元素方案无效
问题描述
Pacman本应吞吃前方的圆点,但圆点直接穿过了它。尝试使用.pacman::before伪元素并设置overflow:hidden来遮挡进入Pacman嘴部的圆点,但该方案无效。求解决思路或替代方案。

现有代码
CSS代码
/* General Reset */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Centralized the game */ body { display: flex; justify-content: center; align-items: center; width: 100%; height: 100vh; background-color: gray; } /* Game Container */ .game-container { display: flex; align-items: center; width: 70%; height: 100px; overflow: hidden; } /* Pacman */ .pacman { display: inline-block; width: 80px; height: 80px; background-image: url("../images/pacman1.png"); background-size: contain; background-repeat: no-repeat; animation: chomping 0.6s linear 1s infinite; z-index: 1; } .pacman::before { position: relative; display: block; content: ""; width: 40px; height: 80px; overflow: hidden; } @keyframes chomping { 0% { background-image: url("../images/pacman1.png"); } 16.67% { background-image: url("../images/pacman2.png"); } 33.33% { background-image: url("../images/pacman3.png"); } 50% { background-image: url("../images/pacman4.png"); } 66.67% { background-image: url("../images/pacman3.png"); } 83.33% { background-image: url("../images/pacman2.png"); } 100% { background-image: url("../images/pacman1.png"); } } /* Dots */ .dots { width: 90%; height: 30px; position: relative; } .dots::before { content: ""; width: 400%; height: 100%; background-image: radial-gradient(circle, white 30%, transparent 15%); background-size: 40px 20px; position: absolute; background-repeat: repeat-x; left: 0; top: 10%; animation: dots-moving 23.5s linear 1s infinite; } @keyframes dots-moving { 0% { transform: translateX(0); } 100% { transform: translateX(-50%); } }
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="./styles/index.css"> </head> <body> <div class="game-container"> <div class="pacman"></div> <div class="dots"></div> </div> </body> </html>
解决思路
方案一:调整DOM层级与裁剪区域
原方案无效是因为.pacman和.dots是同级元素,伪元素作为.pacman的子元素无法覆盖在.dots上方。可以通过嵌套DOM结构实现裁剪:
- 将
.dots移入.pacman内部,让Pacman作为父容器,利用overflow: hidden动态裁剪进入嘴部的圆点。 - 调整
.dots的初始位置,使其从Pacman右侧开始向左移动。 - 配合Pacman的张嘴动画,父容器的裁剪会自动实现“吞吃”效果,无需额外伪元素。
核心修改代码:
.pacman { display: inline-block; width: 80px; height: 80px; background-image: url("../images/pacman1.png"); background-size: contain; background-repeat: no-repeat; animation: chomping 0.6s linear 1s infinite; position: relative; overflow: hidden; /* 关键:裁剪内部超出的圆点 */ } .dots { position: absolute; left: 80px; /* 从Pacman右侧开始 */ top: 50%; transform: translateY(-50%); width: 100vw; height: 30px; }
方案二:添加同步遮罩层
如果不想调整DOM结构,可以给Pacman添加一个动态遮罩层,模拟嘴部开合时的遮挡:
- 创建
.pacman-mask元素,与Pacman重叠,背景色设为游戏背景色(灰色)。 - 根据Pacman的张嘴动画,用
clip-path同步调整遮罩层形状,张嘴时露出缺口,闭嘴时完全覆盖前方圆点。
核心修改代码:
.pacman { position: relative; /* 原有样式不变 */ } .pacman-mask { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: gray; animation: mask-chomp 0.6s linear 1s infinite; } @keyframes mask-chomp { 0% { clip-path: circle(50% at 50% 50%); /* 完全覆盖,对应Pacman闭嘴 */ } 50% { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%, 50% 50%); /* 露出嘴部缺口 */ } 100% { clip-path: circle(50% at 50% 50%); } }
同时在HTML中给.pacman添加子元素:
<div class="pacman"> <div class="pacman-mask"></div> </div>
方案三:拆分圆点图层
将圆点分为“已吞吃”和“未吞吃”两部分,通过动画控制右侧未吞吃圆点移动,左侧已吞吃圆点隐藏。但这种方式需要额外DOM元素和动画同步,效率不如前两种方案。
内容的提问来源于stack exchange,提问作者Edward J
相关产品推荐
相关产品推荐

