3
关注
1829
浏览

带8弧的圆弧svg顺时针和逆时针旋转

为什么被折叠? 0 个回复被折叠
阿杜林林 未验证用户 用户来自于: 广东省
2020-09-17 16:59

您可以轻松使用css动画,然后只需将该类添加到您的svg上的点击功能即可。就像这样:

var arcpoint = [-31, -66, -101, -136, -171, -206, -241, -276]; 
 
colors = ["red", "green", "blue", "orange", "yellow", "purple", "pink"]; 
 
for (var i = 0; i < colors.length; i++) { 
 
     document.querySelector("#" + colors[i]).style.strokeDashoffset = arcpoint[i]; 
 
    } 
 
    
 
    
 
$('.left').click(function(){ 
 
    $("#orbit svg").attr("class", "rotating-left"); 
 
}); 
 
$('.right').click(function(){ 
 
    $("#orbit svg").attr("class", "rotating-right"); 
 
});
svg { 
 
    height: 100px; 
 
    width: 100px; 
 
} 
 
circle { 
 
    stroke-width: 4px; 
 
    fill: transparent; 
 
} 
 
#gray{ 
 
    stroke: gray; 
 
} 
 
#red{ 
 
    stroke: red; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#green{ 
 
    stroke: green; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#blue{ 
 
    stroke: blue; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#orange{ 
 
    stroke: orange; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#yellow{ 
 
    stroke: yellow; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#purple{ 
 
    stroke: purple; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#pink{ 
 
    stroke: pink; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 

 
.rotating-right { 
 
    
 
    animation: rotating-right 2s linear infinite; 
 
} 
 
@keyframes rotating-right { 
 
    from { 
 
    transform: rotate(0deg); 
 
    } 
 
    to { 
 
    transform: rotate(360deg); 
 
    } 
 
} 
 
.rotating-left { 
 
    
 
    animation: rotating-left 2s linear infinite; 
 
} 
 
@keyframes rotating-left { 
 
    from { 
 
    transform: rotate(0deg); 
 
    } 
 
    to { 
 
    transform: rotate(-360deg); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">script> 
 
<div id="orbit" > 
 
     <svg viewBox='0 0 100 100' > 
 
      <circle cx='50' cy='50' r='45' id='gray'/> 
 
      <circle cx='50' cy='50' r='45' id='red'/> 
 
      <circle cx='50' cy='50' r='45' id='green'/> 
 
      <circle cx='50' cy='50' r='45' id='blue'/> 
 
      <circle cx='50' cy='50' r='45' id='orange'/> 
 
      <circle cx='50' cy='50' r='45' id='yellow'/> 
 
      <circle cx='50' cy='50' r='45' id='purple'/> 
 
      <circle cx='50' cy='50' r='45' id='pink'/> 
 
     svg> 
 
    div> 
 
    <button class="left">leftbutton> 
 
    <button class="right">rightbutton>

通知我用$("#orbit svg").attr("class", "rotating-right");作为svgjQuery

一百万个答案 未验证用户 用户来自于: 广东省
2020-09-16 18:24

也许你想这样的事情你不能用addClass

var colors = ["gray", "red", "green", "blue", "orange", "yellow", "purple", "pink"]; 
 
var rotateOffset = 0; 
 

 
function setColours() 
 
{ 
 
    for (var i = 0; i < colors.length; i++) { 
 
    var arcIndex = (i + rotateOffset) % colors.length; 
 
    document.querySelector("#" + colors[i]).style.strokeDashoffset = (arcIndex) * -35.3; 
 
    } 
 
} 
 

 
// Set initial colours 
 
setColours(); 
 
    
 

 
// Button handlers 
 
document.getElementById('left').addEventListener("click", function() { 
 
    rotateOffset += (colors.length - 1); 
 
    setColours(); 
 
}); 
 

 
document.getElementById('right').addEventListener("click", function() { 
 
    rotateOffset++ 
 
    setColours(); 
 
});
svg { 
 
    height: 100px; 
 
    width: 100px; 
 
} 
 
circle { 
 
    stroke-width: 4px; 
 
    fill: transparent; 
 
} 
 
#gray{ 
 
    stroke: gray; 
 
} 
 
#red{ 
 
    stroke: red; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#green{ 
 
    stroke: green; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#blue{ 
 
    stroke: blue; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#orange{ 
 
    stroke: orange; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#yellow{ 
 
    stroke: yellow; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#purple{ 
 
    stroke: purple; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
} 
 
#pink{ 
 
    stroke: pink; 
 
    stroke-dasharray: 35.5, 284; /* length of arc, circumference of circle */ 
 
}
<div id="orbit" > 
 
    <svg viewBox='0 0 100 100' > 
 
    <circle cx='50' cy='50' r='45' id='gray'/> 
 
    <circle cx='50' cy='50' r='45' id='red'/> 
 
    <circle cx='50' cy='50' r='45' id='green'/> 
 
    <circle cx='50' cy='50' r='45' id='blue'/> 
 
    <circle cx='50' cy='50' r='45' id='orange'/> 
 
    <circle cx='50' cy='50' r='45' id='yellow'/> 
 
    <circle cx='50' cy='50' r='45' id='purple'/> 
 
    <circle cx='50' cy='50' r='45' id='pink'/> 
 
    svg> 
 
div> 
 

 
<button id="left">leftbutton> 
 
<button id="right">rightbutton>

关于作者

问题动态

发布时间
2020-09-15 15:51
更新时间
2022-09-15 16:04
关注人数
3 人关注

相关问题

3点圆弧,顺时针逆时针labview求源码,,
个人工作笔记 Powered BY WeCenter V4.1.0 © 2024 粤ICP备2020123311号