如何使用 JavaScript 設定過渡效果的 CSS 屬性?


使用 JavaScript 中的 transitionProperty  來設定 CSS 屬性。你可以嘗試執行以下程式碼,使用 JavaScript 返回過渡效果的 CSS 屬性 −

示例

<!DOCTYPE html>
<html>
   <head>
      <style>
         #div1 {
            position: relative;
            margin: 10px;
            height: 300px;
            width: 400px;
            padding: 20px;
            border: 2px solid blue;
         }
         #div2 {
            padding: 80px;
            position: absolute;
            border: 2px solid BLUE;
            background-color: yellow;
            transform: rotateY(45deg);
            transition: all 3s;
         }
         #div2:hover {
            background-color: orange;
            width: 50px;
            height: 50px;
            padding: 100px;
            border-radius: 50px;
         }
      </style>
   </head>

   <body>
      <p>Hover over DIV2</p>
      <button onclick = "display()"> Set </button>
      <div id = "div1">DIV1
         <div id="div2">DIV2</div>
      </div>
      <script>
         function display() {
            document.getElementById("div2").style.transitionProperty = "width,height";
         }
      </script>
   </body>
</html>

更新時間: 23-6-2020

125 次瀏覽

開啟你的 職業生涯

完成課程後即可獲得認證

開始
廣告