使用javascript将一张透明图像叠加在另一张透明图像上
javascript 是否可以将透明背景图像叠加到另一个图像上?假设您有一个网站,其中包含大量产品图片、小玩意、小部件和 Thingamajigs,其中一些产品已被制造商召回,并且您希望在这些图像上叠加“请勿进入”标志(带圆圈的圆圈)斜线图标)——以透明方式,因此原始图像仍然显示出来。可以吗?目标是不必编辑图像。
我是否沿着正确的路线将透明背景图像放入不透明的 DIV 中,并使用 clientWidth 和 clientHeight 根据原始图像的大小调整透明背景图像及其容器 DIV 的大小,然后将 DIV 放置在原始图像的顶部?如果调整窗口大小并且布局动态变化,如何/何时通知叠加的 DIV 移动到原始图像的新位置?没有像 LayoutChangedEvent 这样的东西吗?是否必须使用 setInterval 重复检查原始图像的位置?
Is it possible in javascript to superimpose a transparent-background image upon another image? Let's say you have a website with a bunch of product pictures, gizmos and widgets and thingamajigs, and some of these products have been recalled by the manufacturer and upon those images you want to superimpose the Do-Not-Enter sign (circle-with-slash icon) -- in a transparent manner so the original image still shows through. Can that be done? The goal is to not have to edit the images.
Am I going down the right track to put the transparent-background image in a DIV without opacity and to size the transparent-background image and its container DIV according to the size of the original image using clientWidth and clientHeight, and then position the DIV on top of the original image? If the window is resized and the layout changes dynamically, how/when would the superimposed DIV be told to move to the new location of the original image? There's nothing like a LayoutChangedEvent is there? Would the original image's position have to be checked repeatedly using setInterval?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要 Javascript 来完成此操作,您可以使用 CSS 轻松完成此操作。
下面的例子非常简单。如果将产品图像包装在容器元素中并将该容器设置为
position:relative
,那么您就可以相对于该容器定位子元素。这样,如果调整窗口大小也没关系。查看 jsFiddle 上的工作演示: http://jsfiddle.net/VNqSd/2/
HTML
CSS
You don't need Javascript for this, you can easily accomplish this with CSS.
The example below is very simple. If you wrap the product image in a container element and set that container to
position: relative
then you are able to position child elements relative to that container. This way, if the window is resized it doesn't matter.View a working demo on jsFiddle: http://jsfiddle.net/VNqSd/2/
HTML
CSS