SVG实现的圆环旋转效果

快乐打工仔 分类:实例代码

本章节分享一段代码实例,它使用svg实现了圆环旋转效果。

需要的朋友可以做一下参考,代码实例如下:

<!DOCTYPE html>
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.pipipi.net/" /> 
<title>犀牛前端部落</title> 
<style type="text/css">
circle{
  -webkit-transition: stroke-dasharray .25s;
  transition: stroke-dasharray .25s;
}
</style>
<script>
window.onload = function () {
  if (window.addEventListener) {
    var range = document.querySelector("#range");
    var circle = document.querySelectorAll("circle")[1];
    if (range && circle) {
      range.addEventListener("change", function () {
        var percent = this.value / 100, perimeter = Math.PI * 2 * 170;
        circle.setAttribute('stroke-dasharray', perimeter * percent + " " + perimeter * (1 - percent));
      });
    }
  }
}
</script>
</head>
<body>
<svg width="440" height="440" viewbox="0 0 440 440">
  <circle 
    cx="220" cy="220" 
    r="170" 
    stroke-width="50" 
    stroke="#D1D3D7" 
    fill="none">
  </circle>
  <circle 
    cx="220" cy="220" 
    r="170" 
    stroke-width="50" 
    stroke="#00A5E0" 
    fill="none" 
    transform="matrix(0,-1,1,0,0,440)" 
    stroke-dasharray="0 1069">
  </circle>
</svg>
<p>拖我:<input id="range" type="range" min="0" max="100" value="0" style="width:300px;"></p>
</body>
</html>

妥当按钮即可查看环形效果。

SVG实现的圆环旋转效果

回复

我来回复
  • 暂无回复内容