圓形小球環形均勻分佈程式碼例項

antzone發表於2017-04-17

分享一段程式碼例項,它實現了小球環形圍繞的效果。

並且小球是均勻分佈的,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
.centerDiv {
  width: 600px;
  height: 600px;
  margin: 0 auto;
  margin-top: 200px;
  position: relative;
}
.dashedCircle {
  height: 300px;
  width: 300px;
  border-radius: 100%;
  border: 1px dashed #494949;
  margin: 0 auto;
}
.circle, .circleCenter {
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  color: #fff;
  background: #97e49a;
  border-radius: 100%;
  position: absolute;
}
.circleCenter {
  width: 20px;
  height: 20px;
  line-height: 20px;
}
</style>
<script type="text/javascript">
window.onload = function () {
  var dx = 280,
                  dy = 130,
                        s = 180,//半徑
                        x = Math.sin(0),
                        y = Math.cos(0),
                        dig = 2 * Math.PI / 12;
  var circle = document.querySelectorAll(".circle");
  for (var index = 0; index < 12; index++) {
    var x = Math.sin(index * dig);
    var y = Math.cos(index * dig);
    var topValue = Number(dy + y * s),
          leftValue = Number(dx + x * s);
    circle[index].style.top = topValue + "px";
    circle[index].style.left = leftValue + "px";
  }
  var circleCenterObj = document.querySelector(".circleCenter");
  circleCenterObj.style.top = Number(dy) + 10;//10是中心小圓半徑
  circleCenterObj.style.left = Number(dx) + 10;
}
</script>
</head>
<body>
  <div class="centerDiv">
    <div class="dashedCircle"></div>
    <div class="circleCenter"></div>
    <div class="circle">7</div>
    <div class="circle">6</div>
    <div class="circle">5</div>
    <div class="circle">4</div>
    <div class="circle">3</div>
    <div class="circle">2</div>
    <div class="circle">1</div>
    <div class="circle">12</div>
    <div class="circle">11</div>
    <div class="circle">10</div>
    <div class="circle">9</div>
    <div class="circle">8</div>
  </div>
</body>
</html>

相關文章