javascript抖動元素的小例子
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>xxxxxx</title>
<style>
#control {
height: 100px;
width: 100%;
background: gray;
}
</style>
<script>
function shake(e, onComplete, distance, interval)
{
if (typeof e === "string")
{
e = document.getElementById(e);
} // end if
distance = distance || 8;
interval = interval || 800;
var originalStyle = e.style.cssText;
e.style.position = "relative";
var start = (new Date()).getTime();
animate();
function animate()
{
var now = (new Date()).getTime();
var elapsed = now - start;
var progress = elapsed / interval;
if (progress < 1)
{
var y = distance * Math.sin(Math.PI * progress * 4);
var x = distance * Math.cos(Math.PI * progress * 4);
e.style.left = x + "px";
e.style.top = y + "px";
console.log(e.style.cssText);
setTimeout(animate, Math.min(25, elapsed));
} // end if
else
{
e.style.cssText = originalStyle;
if (onComplete)
{
onComplete(e);
} // end if
} // end else
} // end animate()
} // end shake()
</script>
</head>
<body>
<div id="control" onclick="shake(this);">
</div>
</div>
</body>
</html>
相關(guān)文章
js基于setTimeout與setInterval實(shí)現(xiàn)多線程
這篇文章主要介紹了js基于setTimeout與setInterval實(shí)現(xiàn)多線程的方法,分析了多線程的原理與javascript模擬實(shí)現(xiàn)多線程的相關(guān)技巧,需要的朋友可以參考下2016-06-06
javascript 函數(shù)調(diào)用規(guī)則
構(gòu)造器函數(shù)以大寫字母開頭是一個好的習(xí)慣,這可以作為一個提醒,讓你在調(diào)用的時候不要忘記前面的new運(yùn)算符.2009-08-08
es6函數(shù)name屬性功能與用法實(shí)例分析
這篇文章主要介紹了es6函數(shù)name屬性,結(jié)合實(shí)例形式分析了es6函數(shù)name屬性基本原理、功能、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
JavaScript實(shí)現(xiàn)郵箱后綴提示功能的示例代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)郵箱后綴提示功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
微信小程序列表時間戳轉(zhuǎn)換實(shí)現(xiàn)過程解析
這篇文章主要介紹了微信小程序列表時間戳轉(zhuǎn)換實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
微信小程序基于movable-view實(shí)現(xiàn)滑動刪除效果
這篇文章主要介紹了微信小程序基于movable-view實(shí)現(xiàn)滑動刪除效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01

