由于这个也是自定义对象写的代码,之前的博客中也有自定义对象方法,有详细的注释,这个就不再写注释了。
js代码:
<script type="text/javascript">
var Box;
var Yuan;
function box() {
this.width = "500";
this.height = "500";
this.margin = "100px auto";
this._box = null;
this.creatbox = function() {
if(this._box == null) {
this._box = document.createElement("div");
this._box.className="divbg";
this._box.style.width = this.width + "px";
this._box.style.height = this.height + "px";
this._box.style.margin = this.margin;
document.body.appendChild(this._box);
}
}
}
function yuan() {
this.borderradius = "50%";
this.float = "left";
this.creatyuan = function(count) {
document.getElementsByClassName("divbg")[0].innerHTML="";
var backcolor = "rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")";
var num = Math.floor(Math.random() * (count * count));
for(i = 0; i < count * count; i++) {
this._yuan = document.createElement("div");
this._yuan.style.width = 500 / count + "px";
this._yuan.style.height = 500 / count + "px";
this._yuan.style.backgroundColor = backcolor;
this._yuan.style.borderRadius = this.borderradius;
this._yuan.style.float = this.float;
if(i == num) {
this._yuan.style.opacity = 0.5;
this._yuan.addEventListener("click", clickblock);
}
Box._box.appendChild(this._yuan);
}
}
}
function clickblock() {
countnum++;
Yuan.creatyuan(countnum);
}
var countnum = 3;
window.onload = function() {
Box = new box();
Box.creatbox();
Yuan = new yuan();
Yuan.creatyuan(countnum);
}
</script>