JS寫滑稽笑臉運(yùn)動效果
更新時間:2020年05月28日 11:43:56 作者:mariner_zp
這篇文章主要介紹了JS寫滑稽笑臉運(yùn)動效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
效果演示:

(就這玩意兒,差點寫崩了...)
代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>滑稽笑臉運(yùn)動</title>
<meta name="author" content="marinerzp">
<style>
*{padding: 0;margin: 0;}
html,body{
width: 100%;
height: 100%;
}
#main{
width: 100px;
height: 100px;
border-radius: 50%;
background:url(images/1.jpg) 0 0/100px 100px;
position: absolute;
left: 0;
top: 0;
z-index: 3;
}
.show{
width: 50px;
height: 50px;
border-radius: 50%;
background-color: rgb(239, 187, 101);
position: absolute;
animation: disappear 1.2s ;
animation-fill-mode: forwards;
}
@keyframes disappear{
0%{
opacity: 1;
transform:scale(1);
}
100%{
opacity: 0;
transform:scale(0);
}
}
</style>
</head>
<body>
<div id="main">
</div>
<script>
let Omain=document.querySelector('#main');
let MaxLeft=window.innerWidth-Omain.offsetWidth;
let MaxTop=window.innerHeight-Omain.offsetHeight;
window.οnresize=function(){//監(jiān)聽窗口大小改變事件
MaxLeft=window.innerWidth-Omain.offsetWidth;
MaxTop=window.innerHeight-Omain.offsetHeight;
};
/*
水平方向上:以向右為正方向
豎直方向上:以向下為正方向
*/
let Vx=6;//3px/s
let Vy=9;//4px/s
let X=0,Y=0;
~~function move(){
X+=Vx;
Y+=Vy;
if (Y>=MaxTop) {
Y=MaxTop;
Vy=-Vy;
}
if (Y<=0) {
Y=0;
Vy=-Vy;
}
if (X>=MaxLeft) {
X=MaxLeft;
Vx=-Vx;
}
if (X<=0) {
X=0;
Vx=-Vx;
}
Omain.style.left=`${X}px`;
Omain.style.top=`${Y}px`;
createTail(X,Y);//生成拖尾
requestAnimationFrame(move);
}();
function createTail(X,Y){
let node=document.createElement('p');
node.classList.add('show');
node.style.cssText=`left:${X+20}px;top:${Y+20}px`;
document.body.appendChild(node);
setTimeout(()=>{
document.body.removeChild(node);
node=null;
},1200);
}
</script>
</body>
</html>
總結(jié)
到此這篇關(guān)于JS寫滑稽笑臉運(yùn)動效果的文章就介紹到這了,更多相關(guān)js 滑稽笑臉內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你用Uniapp實現(xiàn)微信小程序的GPS定位打卡
地圖組件用于展示地圖,而定位API只是獲取坐標(biāo),請勿混淆兩者,下面這篇文章主要給大家介紹了關(guān)于如何使用Uniapp實現(xiàn)微信小程序的GPS定位打卡的相關(guān)資料,需要的朋友可以參考下2022-11-11
Asp.Net alert彈出提示信息的幾種方法總結(jié)
本篇文章主要是對Asp.Net alert彈出提示信息的幾種方法進(jìn)行了總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
Javascript Tab 導(dǎo)航插件 (23個)
實現(xiàn)tab頁很多方法,有一些是用純CSS實現(xiàn),其他大多數(shù)是基于jquery、mootools或者其他js框架實現(xiàn),既然有這么多可以拿來即用的插件,又何苦重復(fù)造輪子。2009-06-06
Javascript中prototype屬性實現(xiàn)給內(nèi)置對象添加新的方法
這篇文章主要介紹了Javascript中prototype屬性實現(xiàn)給內(nèi)置對象添加新的方法,涉及javascript中prototype屬性的使用技巧,需要的朋友可以參考下2015-05-05
javascript實現(xiàn)fetch請求返回的統(tǒng)一攔截
這篇文章主要介紹了javascript實現(xiàn)fetch請求返回的統(tǒng)一攔截,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Bootstrap基本樣式學(xué)習(xí)筆記之按鈕(4)
篇文章主要介紹了Bootstrap學(xué)習(xí)筆記之按鈕基本樣式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12

