前端JS

JS*千字墙效果

最近在学习js的继承,类过程中,突发奇想,信手拈来,本着以class类,prototype原型,构造函数及对象的思想为中心,学习中封装了一个随机动态地中华千字文的效果;

效果如下:
在这里插入图片描述

htmlCode:

在这里插入图片描述

jsCode

/**
function Box(parent,options) {
options = options||{}
this.backgroundColor = options.backgroundColor || ‘red’;
this.borderRadius = options.borderRadius || ‘100%’;
this.width = options.width || 100;
this.height = options.height || 100;
this.x = options.x || 0;
this.y = options.y || 0;
this.div1 = document.createElement(‘div’);
this.div1.innerHTML = getRandomText();

   parent.appendChild(this.div1);

   this.parent = parent;

  this.init();

}

Box.prototype.init = function(){
var div = this.div1;
div.style.backgroundColor = this.backgroundColor;
div.style.borderRadius = this.borderRadius;
div.style.width = this.width + ‘px’;
div.style.height = this.height + ‘px’;
div.style.left = this.x + “px”;
div.style.top = this.y + “px”;
div.style.position = ‘absolute’;
}
Box.prototype.random = function(){
var x = Tools.getRandom(0,this.parent.offsetWidth/this.width - 2)*this.width;
var y = Tools.getRandom(0,this.parent.offsetHeight/this.height - 2)*this.height;
this.div1.style.left = x + ‘px’;
this.div1.style.top = y + ‘px’
}

//随机生成文字
function getRandomText(){
eval( “var word=” + ‘"\u’ + (Math.round(Math.random() * 20901) + 19968).toString(16)+’"’)
return word
}

/**
var container = document.getElementById(‘container’);

var arr = []
for(var i = 0;i<100;i++){
var r = Tools.getRandom(0,255);
var g = Tools.getRandom(0,255);
var b = Tools.getRandom(0,255);
var box = new Box(container,{
backgroundColor:‘rgb(’+r+’,’+g+’,’+b+’)’,
borderRadius:‘100%’,
display: ‘flex’,
justifyContent: ‘center’,
alignItems: ‘center’,

})
arr.push(box);
}

setInterval(randomBox,2000);

randomBox();
function randomBox(){
for(var i=0;i<arr.length;i++){
var box = arr[i];
box.random()
}
}

/**
var Tools = {
getRandom:function(min,max){
return Math.floor(Math.random()*(max-min+1))+min;
}
}

cssCode

html,body{
padding:0;
margin:0;
width:100%;
height: 100%;
}

#container{
width: 100%;
height: 100%;
background-color:rgba(0,0,0,0.5);
position: relative;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
font-size: 60px;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值