Document.mozSetImageElement() - Web APIs 编辑

Non-standard

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

The Document.mozSetImageElement() method changes the element being used as the CSS background for a background with a given background element ID.

Syntax

document.mozSetImageElement(imageElementId, imageElement);

Parameters

  • imageElementId is a string indicating the name of an element that has been specified as a background image using the -moz-element() CSS function.
  • imageElement is the new element to use as the background corresponding to that image element string. Specify null to remove the background element.

Example

This example changes the background of a <div> block each time the block is clicked by the user.

View this example live.

<style type="text/css">
  #mybox {
    background-image: -moz-element(#canvasbg);
    text-align: center;
    width: 400px;
    height: 400px;
    cursor: pointer;
  }
</style>

The CSS defined by the <style> block above is used by our <div> to use an element with the id "canvasbg" as its background.

var c = 0x00;
function clicked() {
  var canvas = document.createElement("canvas");
  canvas.setAttribute("width", 100);
  canvas.setAttribute("height", 100);

  var ctx = canvas.getContext('2d');
  ctx.fillStyle = "#" + c.toString(16) + "0000";
  ctx.fillRect(25, 25, 75, 75);

  c += 0x11;
  if (c > 0xff) {
    c = 0x00;
  }

  document.mozSetImageElement("canvasbg", canvas);
}

The code here is called each time the user clicks the <div> element. It creates a new <canvas> with the width and height set to 100 pixels, then draws into it a 50 by 50 pixel square. Each time the function is called, the square is a different color (its red component is increased each time), so each time the user clicks the element, the background is filled with a brighter and brighter pattern of red tiles.

Once the canvas is drawn, document.mozSetImageElement() is called to set the background for any CSS using the ID "canvasbg" as its background element ID to be our new canvas.

Specifications

Not part of any specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:32 次

字数:4012

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文