前言
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)巫溪免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
所謂文字聚合效果,原理就是將容器分為若干的小塊,然后每個(gè)小塊設(shè)置 background-poisition
,最后添加css3動(dòng)畫就可以了,去掉注釋,也就僅僅20行的代碼。
先來看看效果圖:
js代碼如下:
//c為列數(shù),r為行數(shù),把box劃分成多少個(gè)小塊 var box = document.querySelector('.boxWrap1'),c=4,r=8; //每個(gè)小塊的寬高 var w = box.offsetWidth/c,h = box.offsetHeight/r; //循環(huán)添加小塊 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樣式,并設(shè)置每個(gè)小塊的背景 _div.style.cssText = 'width:'+w+'px;height:'+h+'px;left:'+_left+'px;top:'+_top+'px; opacity:0;background-position:'+(-_left) + 'px ' + (-_top) + 'px'; //添加css動(dòng)畫時(shí)間 _div.style.transition = 'all '+ Random(1,1.8) +'s ease'; //添加css的transform動(dòng)畫 _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); }; }; //延時(shí)添加動(dòng)畫 setTimeout(function(){ box.classList.add('set'); },100); //隨機(jī)數(shù) function Random(start,end){ return Math.random()*(end-start)+start; };
完整的示例如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>利用JS實(shí)現(xiàn)文字的聚合動(dòng)畫效果</title> <style> .boxWrap1{width: 160px;height: 417px;position: relative;margin:0px auto;} .boxWrap1 img{width: 100%;} .boxWrap1 div{background: url(http://itakeo.com/wpimg/2_1.png) no-repeat;background-size: 160px auto; position: absolute; left: 0px;top: 0px; } .boxWrap1.set div{ opacity: 1!important; transform:perspective(800px) translate3d(0px,0px,0px) rotate(0deg) scale(1)!important; -moz-transform:perspective(800px) translate3d(0px,0px,0px) rotate(0deg) scale(1)!important; -webkit-transform:perspective(800px) translate3d(0px,0px,0px) rotate(0deg) scale(1)!important; } .boxSiteWrap{ width: 100%;overflow: hidden;height: auto; background: #000;padding: 140px 0} .boxSiteWrap button{cursor:pointer;display: block;font-size: 16px; width: 140px;height: 40px; background: #fff;color: #000;border:none;margin:30px auto;} </style> </head> <body> <div class="boxSiteWrap"> <div class="boxWrap1"></div> <button>重新加載</button> </div> <script> window.addEventListener('load',function(){ ;(function(){ function init(){ 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; _div.style.cssText = 'width:'+w+'px;height:'+h+'px;left:'+_left+'px;top:'+_top+'px; opacity:0;background-position:'+(-_left) + 'px ' + (-_top) + 'px'; _div.style.transition = _div.style.MozTransition = _div.style.WebkitTransition = 'all '+ Random(1,1.8) +'s ease'; _div.style.transform = _div.style.MozTransform = _div.style.WebkitTransform = '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; }; }; init(); var flag = true; document.querySelector('button').onclick = function(){ if(flag){ document.querySelector('.boxWrap1').classList.remove('set') setTimeout(function(){ document.querySelector('.boxWrap1').innerHTML = ''; init(); flag = true; },1200); flag = false; }; }; })(); }); </script> </body> </html>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
新聞標(biāo)題:利用JS實(shí)現(xiàn)文字的聚合動(dòng)畫效果
文章分享:http://www.chinadenli.net/article48/jogchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站收錄、小程序開發(fā)、微信公眾號(hào)、商城網(wǎng)站、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)