【问题标题】:I need to make like this gif animation in css, I stated with code but I cant reversed the animation我需要在css中制作这样的gif动画,我用代码说明但我无法反转动画
【发布时间】:2022-02-15 22:50:03
【问题描述】:

我正在尝试使用 CSS 或 SVG 为进度条模拟此动画 有什么方法可以制作这个动画?

我将字符串添加为嵌入式代码,我尝试使用 SVG 而不是 CSS,此图像取自设计师发送的视频

我为尝试应用动画而编写的当前代码示例

var canvas = document.getElementById("canvas"),
    context = canvas.getContext("2d"),
    centerX = canvas.width / 2,
    centerY = canvas.height / 2,
    rad = (Math.PI * 2) / 50,
    speed = 1;
function blueCircle(speed) {
    context.save();
    context.beginPath();
    context.strokeStyle = "#ffffff";
    context.lineWidth = 4;
    context.arc(
        centerX,
        centerY,
        40,
        -Math.PI / 2,
        -Math.PI / 2 + speed * rad,
        false
    );
    context.stroke();
    context.restore();
}
function reblueCircle(speed) {
    context.save();
    context.beginPath();
    context.strokeStyle = "#ffffff69";
    context.lineWidth = 4;
    context.arc(
        centerX,
        centerY,
        40,
        -Math.PI / 2,
        -Math.PI / 2 + speed * rad,
        false
    );
    context.stroke();
    context.restore();
}
function whiteCircle() {
    context.save();
    context.beginPath();
    context.strokeStyle = "#ffffff69";
    context.lineWidth = 4;
    context.arc(centerX, centerY, 40, 0, Math.PI * 2, false);
    context.stroke();
    context.closePath();
    context.restore();
}
var res = false;
(function drawFrame() {
    window.requestAnimationFrame(drawFrame, canvas);
    context.clearRect(0, 0, canvas.width, canvas.height);
    whiteCircle();
    blueCircle(speed);
    if (res) {
        speed -= 0.1;
        reblueCircle(speed);
    }
    if (speed > 50 && !res) {
        res = true;
    } else if (speed < 50 && !res) {
        speed += 0.1;
    } else if (speed > 50 && !res) {
        res = true;
    }
})();
body {
    background: #000;
}
&lt;canvas id="canvas" width="84" height="84"&gt;&lt;/canvas&gt;

【问题讨论】:

    标签: javascript css svg


    【解决方案1】:

    编辑:我解决了你的实际要求,没有 JS,只有 CSS:

    ::root {
      --val: 0;
    }
    
    svg {
      transform: rotate(-90deg);
    }
    
    .percent {
      animation: load 3s infinite;
      stroke-dasharray: 100;
    }
    
    
    @keyframes load {
      0% {
        stroke-dashoffset: 0;
      }
      20% {
        stroke-dashoffset: -100;
      }
      100% {
        stroke-dashoffset: -200;
      }
    }
    <svg width="120" height="120" viewBox="0 0 120 120">
        <circle cx="60" cy="60" r="54" fill="none" stroke="#e6e6e6" stroke-width="12" />
        <circle class="percent" cx="60" cy="60" r="54" fill="none" stroke="#f77a52" stroke-width="12" pathLength="100" />
    </svg>

    旧答案:

    可以从this question 改编解决方案,如下所示:

    let increasing = true
    let progress = 0
    
    function updateProgress() {
      progress = increasing ? progress + 1 : progress - 1
      if(progress > 100) {
        progress = 100
        increasing = false
      }
      if(progress < 0) {
        progress = 0
        increasing = true
      }
      
      document.querySelector('svg circle.percent').style.setProperty('--value', progress);
      setTimeout(updateProgress, 50)
    }
    
    updateProgress()
    ::root {
      --val: 0;
    }
    
    svg {
      transform: rotate(-90deg);
    }
    
    .percent {
      stroke-dasharray: 100;
      stroke-dashoffset: calc(100 - var(--value, 0));
      transition: all 0.1s;
    }
    <svg width="120" height="120" viewBox="0 0 120 120">
        <circle cx="60" cy="60" r="54" fill="none" stroke="#e6e6e6" stroke-width="12" />
        <circle class="percent" cx="60" cy="60" r="54" fill="none" stroke="#f77a52" stroke-width="12" pathLength="100" />
    </svg>

    但我必须说,如果你自己实现了画布解决方案,做得很好 - 为什么不直接使用它呢?

    【讨论】:

    • 我们需要像gif一样的动画,您可以查看上图。
    • @ZeyadHabbab SO 不是免费的代码编写服务
    • 当然不是,@Andreas!我更新了答案,因为我实际上想知道如何解决它。祝你好运@OP
    • Sebastian 说的我能理解,其实我昨天看到了Zeyad Habbab 发的同一个问题,问题描述比我现在看到的要少,但是当我准备提交答案时,我发现他删除了问题
    • 今天看到同样的问题其实挺开心的,虽然说明问题还没有解决,但至少昨天的时间没有浪费,而是看了更详细的描述关于这个问题,我意识到我的答案可能不是他想要的,当然如果我花一点时间修改我的代码我可以回答它,但是已经有人回答了这个问题,所以我放弃了它
    猜你喜欢
    • 1970-01-01
    • 2012-10-23
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多