前端前端
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
  • css
  • flex
  • Vue组件样式修改
  • 水位
  • 自定义属性

水位

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>动态水位效果</title>
    <style>
      .container {
        width: 60px;
        height: 70px;
        position: relative;
        border: 1px solid #000;
        background-color: #f0f0f0;
        overflow: hidden; /* 防止水溢出容器 */
      }

      .water {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        background-color: green;
        height: 0%; /* 初始水位为0% */
        animation: waterAnimation 4s ease-in-out infinite;
      }

      /* 定义水位动画 */
      @keyframes waterAnimation {
        0% {
          height: 0;
        }
        50% {
          height: var(--water-height);
        }
        100% {
          height: 0;
        }
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="water" id="water"></div>
    </div>

    <script>
      // 获取水位元素
      const waterElement = document.getElementById('water');

      // 设置水位高度的 CSS 变量
      function updateWaterLevel(percent) {
          // 设置 --water-height 变量,用于控制水位高度
          waterElement.style.setProperty('--water-height', `${percent * 100}%`);
      }

      // 初始化水位
      updateWaterLevel(0.3);  // 假设初始水位为30%
    </script>
  </body>
</html>

show code

Edit this page
最近更新:: 2025/4/24 10:47
Contributors: wu.hui
Prev
Vue组件样式修改
Next
自定义属性