HTML DOM Style objectPosition 属性

htmldomweb developmentfront end technology更新于 2024/5/23 16:12:17

HTML DOM Style objectPosition 属性返回并修改图片或视频元素在 HTML 文档中其自身内容框中的定位方式。

语法

以下是语法 −

1. 返回 objectPosition

object.objectPosition

2. 修改 objectPosition

object.objectPosition = “value”

此处,值可以为 -

值说明
Initial将此属性值设置为其默认值。
Inherit从其父元素继承此属性值。
Position表示图像或视频元素在其内容框内的位置。

让我们看一个 HTML DOM Style objectPosition 属性的示例 −

示例

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
      background-color: #8BC6EC;
      background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%);
      text-align: center;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
   }
   .img-class {
      width: 200px;
      height: 250px;
      object-fit: cover;
   }
</style>
<body>
<h1 style="text-align:center">DOM Style objectPosition Property Demo</h1>
<img alt="Learn Java Swing" src="https://www.cainiaomax.com/swing/images/swing-mini-logo.jpg" class="img-class" width='300' height='200'>
<button class="btn" onclick="set()">Set objectPosition</button>
<script>
   function set() {
      document.querySelector('.img-class').style.objectPosition = "100% 100%";
   }
</script>
</body>
</html>

输出

点击"设置对象位置"按钮设置图像元素上的对象位置属性 −


相关文章


有用资源