首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用8弧顺时针和逆时针旋转svg。

用8弧顺时针和逆时针旋转svg。
EN

Stack Overflow用户
提问于 2017-12-27 14:58:29
回答 2查看 396关注 0票数 3

请帮帮我。

我在这个图像图像中显示了这个svg,它的代码如下:

代码语言:javascript
复制
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];
}
代码语言:javascript
复制
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 */
}
代码语言:javascript
复制
<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>

现在我想通过点击一个按钮来旋转所有弧的顺时针和逆时针方向。问题是,我的思想是如何使功能和循环改变颜色和旋转顺时针和逆时针方向。

任何帮助都将不胜感激。提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-28 06:12:20

也许你想要这样的东西?

代码语言:javascript
复制
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();
});
代码语言:javascript
复制
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 */
}
代码语言:javascript
复制
<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">left</button>
<button id="right">right</button>

票数 1
EN

Stack Overflow用户

发布于 2017-12-27 15:12:47

您可以轻松地使用css动画,然后在单击函数中将类添加到svg中。如下所示:

代码语言:javascript
复制
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");
});  
代码语言:javascript
复制
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);
  }
}
代码语言:javascript
复制
<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">left</button>
    <button class="right">right</button>

注意,我使用了$("#orbit svg").attr("class", "rotating-right");,因为您不能在svgjQuery上使用jQuery

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47994278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档