Java随机生成长宽的矩形_JS_生成随机矩形位置/矩形大小_面向对象_原型+构造函数模式...

document.getElementById("content").style.height=document.documentElement.clientHeight+"px";

document.getElementById("rec_place").style.height=document.documentElement.clientHeight-85+"px";varmakeRec=document.getElementById("makeRec");varclearRec=document.getElementById("clearRec");varrec_num=document.getElementById("rec_num");varrec_place=document.getElementById("rec_place");vararr= newArray();//点击生成按钮

makeRec.οnclick=function(){varnum=rec_num.value;if(parseInt(num)!=num){

alert("请输入整数!")

}else if(parseInt(num)<=0){

alert("请输入大于0的整数!")

}else{for(vari=0;i

rec.createRec();

arr.push(rec);

}

}

}//点击清除画布

clearRec.οnclick=function(){varnum=rec_place.getElementsByTagName("div").length;for(vari=0;i

arr[0].ruinRec();

arr.shift();

}

}

}//var colors = ["red","blue","green","yellow","black"];

functionRectangle(index,rec_place){this.index=index;this.rec_height=Math.floor(Math.random()*101+100);this.rec_width=Math.floor(Math.random()*101+100);//this.rec_color = colors[Math.floor(Math.random()*5)];

this.rec_color= "rgb("+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+")";this.rec_left=Math.floor(Math.random()*(rec_place.offsetWidth-this.rec_width-1)+2);this.rec_top=Math.floor(Math.random()*(rec_place.offsetHeight-this.rec_height)+87);this.myRec= null;//方法一,构造函数

//this.createRec =function(){

//this.myRec = document.createElement("div");

//this.myRec.className = "mydiv";

//this.myRec.innerHTML = "我是矩形"+(this.index+1);

//this.myRec.style.height = this.rec_height+"px";

//this.myRec.style.width = this.rec_width+"px";

//this.myRec.style.border = "3px solid "+this.rec_color;

//this.myRec.style.left = this.rec_left+"px";

//this.myRec.style.top =this.rec_top+"px";

//rec_place.appendChild(this.myRec);

//}

//this.ruinRec = function(){

//rec_place.removeChild(this.myRec);

//}

}//原型+构造函数:让函数共用

Rectangle.prototype={

createRec:function(){this.myRec=document.createElement("div");this.myRec.className= "mydiv";this.myRec.innerHTML= "我是矩形"+(this.index+1);this.myRec.style.height= this.rec_height+"px";this.myRec.style.width= this.rec_width+"px";this.myRec.style.border= "3px solid"+this.rec_color;this.myRec.style.left= this.rec_left+"px";this.myRec.style.top=this.rec_top+"px";

rec_place.appendChild(this.myRec);

},

ruinRec:function(){

rec_place.removeChild(this.myRec);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值