利用JS實(shí)現(xiàn)文字的聚合動(dòng)畫效果
前言
所謂文字聚合效果,原理就是將容器分為若干的小塊,然后每個(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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
js實(shí)現(xiàn)自定義滾動(dòng)條的示例
這篇文章主要介紹了js實(shí)現(xiàn)自定義滾動(dòng)條的示例,幫助大家制作JS特效,美化自身網(wǎng)頁,感興趣的朋友可以了解下2020-10-10原生js 封裝get ,post, delete 請(qǐng)求的實(shí)例
下面小編就為大家?guī)硪黄鷍s 封裝get ,post, delete 請(qǐng)求的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08uniapp小程序項(xiàng)目獲取位置經(jīng)緯度信息
在實(shí)際項(xiàng)目中很多時(shí)候我們需要獲取設(shè)備的位置信息,去展示給客戶,或者以位置信息為參數(shù),繼續(xù)向服務(wù)器獲取一些數(shù)據(jù),這篇文章主要介紹了uni-app如何獲取位置信息(經(jīng)緯度),需要的朋友可以參考下2022-11-11js實(shí)現(xiàn)iframe自動(dòng)自適應(yīng)高度的方法
這篇文章主要介紹了js實(shí)現(xiàn)iframe自動(dòng)自適應(yīng)高度的方法,涉及javascript操作iframe框架的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02代碼精簡(jiǎn)的可以實(shí)現(xiàn)元素圓角的js函數(shù)
代碼精簡(jiǎn)的可以實(shí)現(xiàn)元素圓角的js函數(shù)...2007-07-07javascript實(shí)現(xiàn)類似java中g(shù)etClass()得到對(duì)象類名的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)類似java中g(shù)etClass()得到對(duì)象類名的方法,實(shí)例分析了javascript實(shí)現(xiàn)java中g(shù)etClass方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07JS導(dǎo)出PDF插件的方法(支持中文、圖片使用路徑)
下面小編就為大家?guī)硪黄狫S導(dǎo)出PDF插件的方法(支持中文、圖片使用路徑)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07JavaScript實(shí)現(xiàn)頁面5秒后自動(dòng)跳轉(zhuǎn)的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)頁面5秒后自動(dòng)跳轉(zhuǎn)的方法,涉及javascript遞歸調(diào)用與計(jì)時(shí)函數(shù)setTimeout的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04JavaScript中this關(guān)鍵字用法實(shí)例分析
這篇文章主要介紹了JavaScript中this關(guān)鍵字用法,結(jié)合實(shí)例形式總結(jié)分析了javascript中this關(guān)鍵字在不同條件下的指向問題與相關(guān)操作技巧,需要的朋友可以參考下2018-08-08js實(shí)現(xiàn)json數(shù)據(jù)行到列的轉(zhuǎn)換的實(shí)例代碼
為了實(shí)現(xiàn)這樣的數(shù)據(jù)顯示出來三個(gè)序列,分別為鄭州、新鄉(xiāng)、安陽的電量,就需要自己實(shí)現(xiàn)對(duì)這樣數(shù)據(jù)的轉(zhuǎn)換,轉(zhuǎn)換成如下的形式:2013-08-08