文字聚合效果,原理就是将容器分为若干的小块,然后每个小块设置 background-poisition ,最后添加css3动画就可以了,去掉注释,也就仅仅20行的代码。
源码如下:
//c为列数,r为行数,把box划分成多少个小块 var box = document.querySelector('.boxWrap1'),c=4,r=8; //每个小块的宽高 var w = box.offsetWidth/c,h = box.offsetHeight/r; //循环添加小块 for(var i = 0;i < r;i++){ for(var j = 0;j < c;j++) { var _div=document.createElement('div'); var _left = j * w,_top = i * h; //添加css样式,并设置每个小块的背景 _div.style.cssText = 'width:'+w+'px;height:'+h+'px;left:'+_left+'px;top:'+_top+'px; opacity:0;background-position:'+(-_left) + 'px ' + (-_top) + 'px'; //添加css动画时间 _div.style.transition = 'all '+ Random(1,1.8) +'s ease'; //添加css的transform动画 _div.style.transform = 'perspective(800px) translate3d('+Random(-200,200)+'px, '+Random(-200,200)+'px,300px) rotate('+Random(-90,90)+'deg) scale('+ Random(0,2) +')' //添加 box.appendChild(_div); }; }; //延时添加动画 setTimeout(function(){ box.classList.add('set'); },100); //随机数 function Random(start,end){ return Math.random()*(end-start)+start; };01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
效果演示:
猴赛雷
😛 你好
😮 😮 😮 😮 😮 x呃呃呃额额
123123 👿 👿 👿 666 强
666太厉害咯
😆 6666666666666厉害
爱的
阿萨德
萨达
牛逼
牛逼啊