使用javascript将一张透明图像叠加在另一张透明图像上

发布于 2024-11-02 11:13:03 字数 388 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我家小可爱 2024-11-09 11:13:03

您不需要 Javascript 来完成此操作,您可以使用 CSS 轻松完成此操作。

下面的例子非常简单。如果将产品图像包装在容器元素中并将该容器设置为 position:relative,那么您就可以相对于该容器定位子元素。这样,如果调整窗口大小也没关系。

查看 jsFiddle 上的工作演示: http://jsfiddle.net/VNqSd/2/

HTML

<div class="container">
  <img src="http://nontalk.s3.amazonaws.com/product.png" 
       width="100" height="100" />
  <img src="http://nontalk.s3.amazonaws.com/overlay.png" 
       class="overlay" width="100" height="100" />
</div>

CSS

.container {
  position: relative;   
}
.overlay {
  position: absolute;   
  left:0;
  top: 0;
  z-index: 999;
}

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

<div class="container">
  <img src="http://nontalk.s3.amazonaws.com/product.png" 
       width="100" height="100" />
  <img src="http://nontalk.s3.amazonaws.com/overlay.png" 
       class="overlay" width="100" height="100" />
</div>

CSS

.container {
  position: relative;   
}
.overlay {
  position: absolute;   
  left:0;
  top: 0;
  z-index: 999;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文