利用jQuery的動(dòng)畫函數(shù)animate實(shí)現(xiàn)豌豆發(fā)射效果
先來看看效果圖
豌豆射手,草坪還有子彈都是現(xiàn)成的圖片,
1. jQuery是庫還是框架?
jQuery可以說是現(xiàn)在最流行的一個(gè)js類庫,而非框架。
之前在知乎上看到有人說了這樣一句話:
You call library. Framework calls you.
我深以為然,字面意思大概就是你可以無約束地使用類庫,卻需要在各種限制條件下使用一個(gè)框架。
我私以為,js 庫指的是直接和document文檔元素交互的一個(gè)API,你可以直接引用庫,讓它為你服務(wù)。而框架是偏向于架構(gòu)的層次,你如果想要使用框架,就必須按照它的規(guī)則來。比如angular.js,它就給你提供方法的同時(shí)還約束了dom文檔結(jié)構(gòu)。
拿Java的三大框架來說,也是如此,你要想使用Spring
,就得按照它的步驟來,就好像一個(gè)房子,鋼筋水泥已經(jīng)弄好,你只需要進(jìn)去裝修就OK了。而庫,就有點(diǎn)類似于StringUtils
的韻味,除了它暴露出來的接口,其他你都無需關(guān)心,直接調(diào)用就行了。
2. jQuery的animate函數(shù)
animate()
函數(shù)用于執(zhí)行一個(gè)基于css屬性的自定義動(dòng)畫
基本用法:
$('#id').animate(css,time,callback);
css : 你需要最終實(shí)現(xiàn)的樣式列表
time: 過渡的時(shí)間
callback: 回調(diào)函數(shù)
animate
函數(shù)的作用主要就是實(shí)現(xiàn)一些css樣式的過渡效果。
3.引入 jQuery
比如,現(xiàn)在我有一個(gè)div盒子。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <style type="text/css"> #box { width: 300px; height: 300px; background:greenyellow; } </style> </head> <body> <div id='box'></div> </body> <script> </script> </html>
在上面的代碼中,我們引入了百度提供的jQuery文件。
那么如何快速判斷是否引入成功了呢?提供以下幾個(gè)方法:
1.console.log($);
效果:
這說明引入成功了。
2.直接用瀏覽器驗(yàn)證
打開你的頁面,按一下F12,出現(xiàn)這樣的控制臺(tái),這是瀏覽器自帶的(我這里使用的是谷歌瀏覽器)。
輸入$
回車!
誒,這樣是不是也可以呢?
4. onmouseover事件
我們來給div
盒子添加一個(gè)鼠標(biāo)劃上去的事件。
$('#box').on('mouseover',function(){ alert(); });
劃上去:
嗯,最起碼,這說明我們到目前為止的代碼都是正確的,我初學(xué)js的時(shí)候就喜歡這樣,讓我感覺每一行代碼都寫得很放心。
5.用animate函數(shù)改變盒子寬度和高度
我們把alert
去掉,加上下面的代碼:
$('#box').on('mouseover',function(){ $('#box').animate({width:600},500); });
這表示當(dāng)我把鼠標(biāo)畫上去的時(shí)候,就改變寬度為600px,過渡時(shí)間為500毫秒。
如果我們希望在寬度加倍后,令高度也加倍,又該如何做呢?
對(duì)了,用回調(diào)函數(shù),當(dāng)?shù)谝粋€(gè)動(dòng)畫執(zhí)行完畢,就繼續(xù)執(zhí)行下一個(gè)動(dòng)畫:
$('#box').on('mouseover',function(){ $('#box').animate({width:600},500,function(){ $('#box').animate({height:600},500); }); });
這樣就有了一個(gè)先后順序。
本文簡(jiǎn)單地介紹了一下jQuery animate函數(shù)的使用。
6. 附錄
最后,附上一開始案例的代碼,除了animate
函數(shù),還用到了js的定時(shí)器setInterval
方法:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script type="text/javascript" src="jquery-1.11.2.min.js"></script> <style type="text/css"> body { background: url(background1.jpg) no-repeat; position: fixed; } ul li { list-style: none; } .wrap { position: relative; left: 170px; top: 65px; } .plants1 { display: inline-block; position: relative; left:35px; } .plants1 .plant { position: relative; margin-bottom:20px; } .plants1 .plant .PB00 { position: absolute; top:-2px; left:15px; } .plants2 { display: inline-block; position: relative; left:2px; } .plants2 .plant { position: relative; margin-bottom:20px; } .plants2 .plant .PB00 { position: absolute; top:-2px; left:15px; } .plants3 { display: inline-block; position: relative; left:-40px; } .plants3 .plant { position: relative; margin-bottom:20px; } .plants3 .plant .PB00 { position: absolute; top:-2px; left:15px; } </style> </head> <body> <div class='wrap'> <ul class='plants1'> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> </ul> <ul class='plants2'> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> </ul> <ul class='plants3'> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> <li class='plant'> <img class='Peashooter' src="img/Peashooter.gif" /> <img class='PB00' src="img/PB00.gif" /> </li> </ul> </div> </body> <script type="text/javascript"> function randomNum(num){ return Math.floor(Math.random()*(num+1)); }; setInterval(function(){ var $this = $('.PB00').eq(randomNum(17)); $this.animate({'margin-left' : 1000},2000,function(){ $this.css({'margin-left' : 0}); }); },10); </script> </html>
總結(jié)
以上就是這篇文章的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)和工作能有所幫助。如果有疑問可以留言交流,謝謝大家對(duì)腳本之家的支持。
- JQuery animate動(dòng)畫應(yīng)用示例
- jQuery封裝animate.css的實(shí)例
- jquery animate動(dòng)畫持續(xù)運(yùn)動(dòng)的實(shí)例
- jQuery中animate()的使用方法及解決$(”body“).animate({“scrollTop”:top})不被Firefox支持的問題
- jQuery animate()實(shí)現(xiàn)背景色漸變效果的處理方法【使用jQuery.color.js插件】
- 淺談原生JS實(shí)現(xiàn)jQuery的animate()動(dòng)畫示例
- jQuery實(shí)現(xiàn)立體式數(shù)字動(dòng)態(tài)增加(animate方法)
- jQuery中animate的幾種用法與注意事項(xiàng)
- jQuery使用animate實(shí)現(xiàn)ul列表項(xiàng)相互飄動(dòng)效果示例
- 原生js實(shí)現(xiàn)jquery函數(shù)animate()動(dòng)畫效果的簡(jiǎn)單實(shí)例
- 詳解jQuery的animate動(dòng)畫方法及動(dòng)畫排隊(duì)問題解決
相關(guān)文章
EasyUI學(xué)習(xí)之DataGird分頁顯示數(shù)據(jù)
這篇文章主要介紹了EasyUI學(xué)習(xí)之DataGird分頁顯示數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12jquery zTree異步加載簡(jiǎn)單實(shí)例分享
Ztree是一個(gè)使用jQuery實(shí)現(xiàn)的JSP頁面的各種功能樹,本文介紹一個(gè)異步獲取數(shù)據(jù)到下拉樹的實(shí)現(xiàn)方式,感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)ztree有所幫助2013-02-02Query常用DIV操作獲取和設(shè)置長度寬度的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猀uery常用DIV操作獲取和設(shè)置長度寬度的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09jQuery中clone()函數(shù)實(shí)現(xiàn)表單中增加和減少輸入項(xiàng)
這篇文章給大家介紹了jQuery中clone()函數(shù)實(shí)現(xiàn)表單中增加和減少輸入項(xiàng)的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-05-05基于Jquery的表格隔行換色,移動(dòng)換色,點(diǎn)擊換色插件
希望能和大家一起交流學(xué)習(xí)。先放上去一個(gè)上周學(xué)習(xí)的一個(gè)jquery插件,基于Jquery的表格隔行換色,移動(dòng)換色,點(diǎn)擊換色插件。2010-12-12jquery.AutoComplete.js中文修正版(支持firefox)
jquery.AutoComplete.js中文修正版(支持firefox),注意是修正了輸入中文的一些bug,需要的朋友可以測(cè)試下。2010-04-04利用jQuery 實(shí)現(xiàn)GridView異步排序、分頁的代碼
經(jīng)常會(huì)用到j(luò)query.ui.tabs標(biāo)簽,如我們可以把備份管理放在一個(gè)頁面上,而該頁面有兩個(gè)tab分別為備份和還原,但這樣會(huì)現(xiàn)在這個(gè)頁面臃腫2010-02-02