在 HTML 中将对象定位在图像上CSS
我正在用 HTML、CSS + Javascript 制作一款找茬游戏,但我面临的问题之一是,在不同的分辨率下,标记圆圈和圆圈会出现变化。 hitbox(基本上只是链接到在标记圈中淡出的 javascript 函数的图像)都位于错误的位置。
我在 CSS 中使用绝对定位和百分比值。标记和命中框覆盖的主图像占页面宽度和高度的 50%。
谢谢,
尼尔。
编辑:添加有关主图像的信息
I'm making a spot-the-difference game in HTML, CSS + Javascript, but one of the problems I'm facing is that on a different resolution, the marker circles & hitboxes (basically just an image that links to a javascript function which fades in the marker circle) are all in the wrong places.
I'm using absolute positioning in CSS with percentage values. The main image that the markers and hitboxes get overlaid on is 50% width and height of the page.
Thanks,
Niall.
Edit: added info about the main image
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:您的编辑:
...大大改变了问题。 :-) 我下面关于使用像素值进行叠加的回答仍然有效;但是计算它们的位置变成了一场噩梦,因为您在设计时无法知道图像的实际宽度是多少。这意味着您必须在运行时(例如,通过 JavaScript)找出图像的实际宽度/高度,然后调整用于绝对定位其他所有内容的像素值以解决该问题。
如果您可以避免它(例如,仅提供一些标准尺寸),我建议避免它。如果不能,我建议使用库(例如 jQuery、原型,YUI,关闭,或 任何其他几个)用于发现图像的实际计算宽度和高度。
原始答案:
那么这就是问题所在。如果您想准确地将某些内容放置在图像顶部,因为图像中的像素将被固定,您将需要使用像素值而不是百分比,如下所示(实时示例):
CSS:
HTML:
Update: Your edit:
...substantially changes the question. :-) My answer below about using pixel values for the overlays remains valid; but calculating their positions becomes a nightmare because you can't know at design time what the actual width of the image will be. This means you have to find out what the actual width/height of the image is at runtime (e.g., via JavaScript) and then adjust the pixel values you use for absolutely positioning everything else to account for that.
If you can avoid it (for instance, by just offering a few standard sizes), I'd recommend avoiding it. If you can't, I'd recommend using a library (like jQuery, Prototype, YUI, Closure, or any of several others) for discovering the actual computed width and height of the image.
Original answer:
That would be the problem, then. If you want to accurately place something on top of an image, as the pixels in the image will be fixed, you'll want to use pixel values rather than percentages, like this (live example):
CSS:
HTML: