javascript - SVG 断路器/开关合闸动画

标签 javascript html css svg css-transforms

我正在尝试设计一个断路器/开关,它可以在某些事件中关闭和打开。我使用 SVG 设计了断路器,并使用 css 动画和转换属性来为它的关闭设置动画。

使用 transform-origin: bottom 但它没有按预期工作。请帮助我以下是我的 css 代码:

.closeme {
    -webkit-animation-name: closeanimaton;
    -webkit-animation-duration: 3s; 
    -webkit-animation-iteration-count: 1;
    -webkit-animation-timing-function: ease-in-out;
    -webkit-animation-delay: -1.5s;
    -webkit-animation-direction: alternate;
    -webkit-transform-origin: bottom;
    animation-name: closeanimaton;
    animation-duration: 3s; 
    animation-iteration-count: 1;
    animation-timing-function: ease-in-out;
    animation-delay: -1.5s;
    animation-direction: alternate;
    animation-fill-mode: forwards;
    transform-origin: bottom;
    -moz-animation: none;
}

@-webkit-keyframes closeanimaton { 
    0% { -webkit-transform: rotate(0deg); }
    100% { -webkit-transform: rotate(35deg); }
}

@keyframes closeanimaton { 
    0% { transform: rotate(0deg); }
    100% { transform: rotate(35deg); }
}

这是codepen链接,我有完整的代码,请随时编辑:

https://codepen.io/anon/pen/OQexEP

最佳答案

这可能不是您寻求帮助的目的,但我发现使用 D3 来创建形状并向其添加动画要容易得多。看看我下面的片段。它可能会给您灵感,让您了解如何制作 SVG 动画。

var svg = d3.select('body').append("svg").attr("width",200).attr("height",150);
svg.style("background-color","black");



var part1 = svg.append("path").attr("d","M100,0 L100,30").attr("fill","none").attr("stroke","white");

var part2 = svg.append("path").attr("d","M100,80 L100,150").attr("fill","none").attr("stroke","white");

var moving_part = svg.append("g").attr("transform","translate(100,80) rotate(45)");

moving_part.append("path").attr("d","M0,0 L0,-50").attr("fill","none").attr("stroke","gold").attr("stroke-width",2);
moving_part.append("circle").attr("cy",-50).attr("r",5).attr("fill","gold");

moving_part.transition().delay(1000).duration(3000).attr("transform","translate(100,80) rotate(0)");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

关于javascript - SVG 断路器/开关合闸动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49123999/

相关文章:

javascript - 检测桌面浏览器中的捏合

javascript - 警报后背景变化

php - 更改 CSS :before from PHP with multiple occurences

javascript - HTML/JS 代码可以在普通浏览器 View 中运行,但无法作为 Chrome 扩展运行?

html - 如何在不调整CSS中父元素宽度的情况下调整子元素的宽度?

javascript - 如何使用 css 或 js 从子 div 中删除样式?

javascript - 如何使用 JavaScript 为 HTML 表格的第 n 个 <td> 元素着色?

javascript - 为什么 jquery 中的 focus() 不起作用?

javascript - 使用搜索栏将 .html 加载到 div 中

javascript - 如何在 javascript 中拆分数字 Linux Firefox 获取错误值,Windows 显示正确值