JS+CSS實(shí)現(xiàn)過渡特效
最近在玩一個(gè)叫Baba is you的游戲,很羨慕里面的一個(gè)轉(zhuǎn)場(chǎng)特效,所以試著做了一下。主要使用了JS和CSS,特效主要是用CSS實(shí)現(xiàn)的。
HTML代碼
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>開始導(dǎo)航</title> <link rel="stylesheet" href="style.css" > </head> <body> <div class="text"> <p><a id="bottom">點(diǎn)擊進(jìn)入</a></p> </div> </body> <script type="text/javascript"> //執(zhí)行代碼 window.onload=function(){ var bottom=document.getElementById("bottom"); bottom.onclick=function(){ action(); } } //獲取網(wǎng)頁長(zhǎng)寬 var windowWidth=window.screen.width; var windowHeight=window.screen.height; function createSnow(){ topblack(); leftblack(); bottomblack(); rightblack(); function topblack(){ //隨機(jī)創(chuàng)造1個(gè)div圓球 var left_random=Math.random()*windowWidth; var top_random=Math.random()*50; var div=document.createElement('div'); div.className='snow'; //定義縮放轉(zhuǎn)換 div.style.transform='scale('+(Math.random()*3)+')' //定義隨機(jī)位置,在頂部50像素之內(nèi) div.style.left=left_random+'px'; div.style.top=top_random+'px'; //放在html外面,先用overflow:hidden隱藏掉 div.style.marginTop="-250px"; document.body.appendChild(div); } function leftblack(){ var left_random=Math.random()*50; var top_random=Math.random()* windowHeight; var div=document.createElement('div'); div.className='snow'; div.style.transform='scale('+(Math.random()*2)+')' div.style.left= left_random+'px'; div.style.top=top_random+'px'; div.style.marginLeft="-250px"; document.body.appendChild(div); } function bottomblack(){ var left_random=Math.random()*windowWidth; var bottom_random=Math.random()*50; var div=document.createElement('div'); div.className='snow'; div.style.transform='scale('+(Math.random()*2)+')' div.style.left=left_random+'px'; div.style.bottom=bottom_random+'px'; div.style.marginBottom="-250px"; document.body.appendChild(div); } function rightblack(){ var right_random=Math.random()*50; var top_random=Math.random()* windowHeight; var div=document.createElement('div'); div.className='snow'; div.style.transform='scale('+(Math.random()*2)+')' div.style.right=right_random+'px'; div.style.top=top_random+'px'; div.style.marginRight="-250px"; document.body.appendChild(div); } } function setblack(){ //各自創(chuàng)造100個(gè)圓球隨機(jī)放在HTML頂部、底部、左右邊,各自隱藏。 for(var i=0;i<100;i++){ createSnow() } } //清除使用過后的云層與文字 function clearsnow(){ var snow=document.querySelectorAll(".snow"); var font=document.querySelector(".Fontarea"); for(var i=0;i<snow.length;i++){ document.body.removeChild(snow[i]); } document.body.removeChild(font); } //只是一個(gè)習(xí)慣,定義一個(gè)創(chuàng)建div的模板函數(shù)。你們可以用自己的方式。 function font(oCss){ var oBox=document.createElement("p"); oCss.parent.appendChild(oBox); oBox.innerHTML=oCss.p; oBox.className=oCss.c; return oBox; } function create(oCss){ var oBox=document.createElement("div"); oCss.parent.appendChild(oBox); oBox.style.width=oCss.w+"px"; oBox.style.height=oCss.h+"px"; oBox.style.position=oCss.p; oBox.style.left=oCss.l+"px"; oBox.style.top=oCss.t+"px"; oBox.style.backgroundSize="100%"; return oBox; } //創(chuàng)建浮現(xiàn)的文字 function winthegame(){ var Fontarea=create({ "w":500, "h":600, "p":"absolute", "parent":document.body, "l":"400", "t":"0"}); Fontarea.style.marginTop="200px"; Fontarea.className="Fontarea"; Fontarea.style.zIndex="31"; var titlep=font({ "parent":Fontarea,"p":"CONGRATULATION!","c":"font7"}); } //執(zhí)行創(chuàng)建云層與文字,封裝起來是因?yàn)椋绻淖殖霈F(xiàn)多個(gè)不同的,就用不同的函數(shù)封裝不同的場(chǎng)合。 function wintime(){ winthegame(); setblack(); } //執(zhí)行創(chuàng)建與清除,用setTimeout()來延遲清除。 function action(){ wintime(); setTimeout(clearsnow,5000); } </script> </html>
css代碼
body{ background-size: 100%; overflow: hidden; background-color: #000; } .text{ color: white; text-align: center; text-transform: uppercase; margin: 300px 0; font-size: 22px; } .text a{color:white; text-decoration:none; cursor: pointer; } .snow{ background: #15181f; position: absolute; width: 100px; height: 100px; border-radius: 50%; z-index: 30; animation: bganimation 5s 1; } .font7{ color:white; text-align: center; font-size: 60px; } .Fontarea{ opacity:0; animation: beganfont 4s 1; } @keyframes bganimation { 0%{ width: 100px; height: 100px; } 50%{ width: 500px; height: 500px; } 100%{ width: 100px; height: 100px; } } @keyframes beganfont { 0%{ opacity:0; } 50%{ opacity:1; } 100%{ opacity:0; } }
這是效果圖,點(diǎn)擊文字會(huì)執(zhí)行效果一次。
效果JS的解析都寫在注釋里了,CSS就是使用@keyframes來實(shí)現(xiàn)效果,也不是什么難懂的。
這種效果對(duì)于用于展示開場(chǎng)應(yīng)該足夠了,主要可以用來炫耀之類的,JS的代碼或許比較粗糙,是從某個(gè)朋友的雪花特效那copy來改的。主要是用來做一個(gè)期末項(xiàng)目的,這個(gè)項(xiàng)目某些東西我以后也會(huì)慢慢總結(jié)的。
那么,就這樣,可能我寫的特效會(huì)跟別人的撞車,請(qǐng)多多包涵。如果感覺不是什么高大上的東西,也請(qǐng)多多包涵。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 監(jiān)控微信瀏覽器且自帶返回按鈕時(shí)間
這篇文章主要介紹了JavaScript 監(jiān)控微信瀏覽器且自帶返回按鈕時(shí)間的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11JavaScript基礎(chǔ)篇(3)之Object、Function等引用類型
這篇文章主要介紹了JavaScript基礎(chǔ)篇(3)之Object、Function等引用類型的相關(guān)資料,需要的朋友可以參考下2015-11-11javascript中內(nèi)置對(duì)象Math的介紹及用法案例
Math對(duì)象是一個(gè)內(nèi)置對(duì)象,具有數(shù)學(xué)常數(shù)和函數(shù)的屬性和方法,不是一個(gè)函數(shù)對(duì)象,下面這篇文章主要給大家介紹了關(guān)于javascript中內(nèi)置對(duì)象Math的介紹及用法案例的相關(guān)資料,需要的朋友可以參考下2022-03-03JavaScript Array Flatten 與遞歸使用介紹
用 JavaScript 將 [1,2,3,[4,5, [6,7]], [[[8]]]] 這樣一個(gè) Array 變成 [1,2,3,4,5, 6,7,8] 呢?傳說中的 Array Flatten2011-10-10詳解js創(chuàng)建對(duì)象的幾種方式和對(duì)象方法
這篇文章主要介紹了詳解js創(chuàng)建對(duì)象的幾種方式和對(duì)象方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03js通過var定義全局變量與在window對(duì)象上直接定義屬性的區(qū)別說明
這篇文章主要介紹了js通過var定義全局變量與在window對(duì)象上直接定義屬性的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09