HTML DOM Style transitionDuration 属性

htmldomweb developmentfront end technology更新于 2024/5/23 22:36:17

HTML DOM Style transitionDuration 属性用于返回并修改 HTML 文档中元素过渡效果的持续时间(以秒 (s) 或毫秒 (ms) 为单位)。

语法

以下是语法 −

1. 返回 transitionDuration

object.transitionDuration

2. 修改 transitionDuration

object.transitionDuration = “value”

此处值可以为 -

值说明
initial将此属性值设置为默认值。
inherit从其父元素继承此属性值。
time表示过渡效果的持续时间,以秒(s)或毫秒(ms)为单位。

示例

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

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem 0;
   }
   .circle {
      height: 100px;
      width: 100px;
      background-color: #db133a;
   }
   .circle:hover {
      height: 200px;
      width: 200px;
      border-radius: 50%;
      transition: all 1s;
   }
   .show {
      font-size: 1.2rem;
      margin: 1rem 0;
   }
</style>
<body>
<h1>DOM Style transitionDuration Property Demo</h1>
<div class='circle'></div>
<button onclick="set()" class="btn">Change Transition</button>
<div class="show">Now, hover on the square</div>
<script>
   function set() {
      document.querySelector('.circle').style.transitionDuration = "5s";
   }
</script>
</body>
</html>

输出

点击"设置过渡"按钮,然后将鼠标悬停在"红色"方块上即可查看 transitionDuration 效果。


相关文章


有用资源