如何最好地将画布覆盖在图像上?
我想将画布放在页面上的图像之上。画布将是图像的精确尺寸。
但是,必须满足以下条件:
- 画布必须始终完全保持在图像顶部。因此,使用绝对定位不起作用,因为如果在图像上方插入内容,它会将图像向下移动而不移动画布。
- 图像的大小可能会根据其原始大小进行调整。因此,用画布替换图像并将其背景设置为图像是行不通的。
我有什么选择?
I would like to place a canvas on top of an image that I have on a page. The canvas will be the exact dimensions of the image.
However, the following conditions must be met:
- The canvas must remain exactly on top of the image at all times. Therefore, using absolute positioning will not work because if content is inserted above the image, it will move the image down without moving the canvas.
- The image may be resized from its original size. Therefore, replacing the image with the canvas and setting its background to the image will not work.
What options do I have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够使用
position:relative
而不是绝对来满足您的第一个要求。对于第二个,我猜你可以将图像和画布都放在一个跨度内。画布的宽度/高度将为 100%,并且会随着图像大小的调整而调整大小,因为 div 的大小会更改以适合图像。
编辑:实际上我不确定
position:relative
是否有效。但我相信,如果您使用position:absolute
并且父元素具有position:relative
,那么绝对位置将相对于父元素。You should be able to use
position:relative
instead of absolute for your first requirement.For the second I'm guessing you could put both the image and the canvas inside of a span. The canvas would have a width/height of 100% and would be resized as the image resizes because the size of the div would change to fit the image.
EDIT: actually I'm not sure
position:relative
would work. But I believe if you useposition:absolute
and the parent element hasposition:relative
, than the absolute positions will be relative to the parent.