js隨機運動小球效果

admin發表於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">
html, body {
  height: 100%;
  width: 100%;
  margin: 0 0;
  padding: 0 0;
}
#point_contianer {
  width: 80%;
  height: 60%;
  position: absolute;
  top: 10%;
  left: 10%;
}
</style>
</head>
<body id="point_contianer">
<script>
var container_size = {
  width: window.innerWidth,
  height: window.innerHeight
}
 
function setCss(obj, arr) {
  for (var i in arr) {
    obj.style[i] = arr[i];
  }
}
window.onload = function() {
  var frame_func = window.requestAnimationFrame
    || window.webkitRequestAnimationFrame
    || window.mozRequestAnimationFrame
    || window.oRequestAnimationFrame
    || window.msRequestAnimationFrame || function (func) {
    window.setTimeout(func, 1000 / 60);
  }
 
  var list = [];
  var point_size = {
    width: container_size.width * 0.8,
    height: container_size.height * 0.6,
  }
 
  var list_color = ["#f2d403", "#00b7ee", "#75c1f6", "#4eacf4"];
  for (var i = 0; i < 13; i++) {
    var div = document.createElement("div");
    var width = (Math.floor(Math.random() * 10) + 1) * 5;
    var color_idx = Math.floor(Math.random() * list_color.length);
    var top = Math.floor(Math.random() * point_size.width);
    var left = Math.floor(Math.random() * point_size.height);
    setCss(div, {
      "position": "absolute",
      "top": top,
      "left": left,
      "background": list_color[color_idx],
      "width": width + "px",
      "height": width + "px",
      "border-radius": "50%",
      "opacity": "0.7"
    });
 
    var item = {
      x: Math.floor(Math.random() * 5 + 1) * (Math.random() * 10 > 5 ? 1 : -1),
      y: Math.floor(Math.random() * 5 + 1) * (Math.random() * 10 > 5 ? 1 : -1),
      top: top,
      left: left,
      content: div
    }
    list.push(item);
    //                                        $("#point_contianer").append(div);
    document.getElementById("point_contianer").appendChild(div);
  }
 
  function run() {
    for (var i = 0; i < 13; i++) {
      var x = list[i].x + list[i].left;
      var y = list[i].y + list[i].top;
 
      //                                                list[i].content.css("left",x + "px");
      //                                                list[i].content.css("top",y + "px");
      setCss(list[i].content, {
        "left": x + "px",
        "top": y + "px"
      })
      list[i].top = y;
      list[i].left = x;
      (x < 0 || x > point_size.width) && (list[i].x = -list[i].x);
      (y < 0 || y > point_size.height) && (list[i].y = -list[i].y);
 
    }
    frame_func(run);
  }
  frame_func(run);
}
</script>
</body>
</html>

相關文章