這是一個純粹為了趣味的特效:點一下方塊,方塊裡的所有元素開始不規則地抖動,就像被撞了一下。實作關鍵是「隨機位移 + 遞迴套用」。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jquery 窗口震動特效</title>
<script type="text/javascript" src="https://www.codefans.net/ajaxjs/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
;(function($){
var element = {};
$.fn.jshaker = function(){
element = $(this);
element.css('position', 'relative');
element.find('*').each(function(i, el){
$(el).css('position', 'relative');
});
var iFunc = function(){ $.fn.jshaker.animate($(element)); };
setTimeout(iFunc, 50);
};
$.fn.jshaker.animate = function(el){
$.fn.jshaker.shake(el);
el.find('*').each(function(i, el){
$.fn.jshaker.shake(el);
});
var iFunc = function(){ $.fn.jshaker.animate(el); };
setTimeout(iFunc, 50);
}
$.fn.jshaker.shake = function(el){
var pos = $(el).position();
if(Math.random() > 0.5){
$(el).css('top', pos['top'] + Math.random() * 20 < 10 ? (Math.random() * 20 * (-1)) : Math.random() * 20);
} else {
$(el).css('left', pos['left'] + Math.random() * 20 < 10 ? (Math.random() * 20 * (-1)) : Math.random() * 20);
}
}
})(jQuery);
</script>
<script type="text/javascript">
$(document).ready(function(){
$('.block').click(function(){
$(this).jshaker();
});
});
</script>
<style type="text/css">
BODY{font-family: "Lucida Grande", Arial, Helvetica, sans-serif;color: #666666;font-size: 12px;background: #FFFFFF;}
A{color: #0A8ECC;}
A:HOVER{text-decoration: none;color: #8FCB2F;}
H1{font-weight: normal;color: #0A8ECC;margin: 0;padding: 0;}
BODY{margin: 20px;padding: 20px;}
STRONG{color: #000000;}
.vspacer{height: 20px;}
PRE.code{padding: 7px;background: #777777;color: #F0F0F0;width: 400px;overflow: auto;}
#content-area{border: 3px solid #CCCCCC;background: #F0F0F0;padding: 10px;width: 500px;}
P.script-link{clear: both;padding: 10px 0;border-top: 1px dotted #CCCCCC;}
#content .block {float: left;border: 1px solid #CCCCCC;background: #F0F0F0;padding: 10px;margin: 10px;width: 300px;}
</style>
</head>
<body>
<div id="page">
<h1>jquery 窗 口 震 動 特 效</h1>
<div id="content">
<div class="block">
<p>點擊本框內,可實現震動</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>
Item 3
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
<li>Sub Item 3</li>
<li>Sub Item 4</li>
<li>Sub Item 5</li>
</ul>
</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
<div class="block">
<form action="#">
<p><label for="inp1">Text Field 1:</label><input type="text" name="inp1" id="inp1" value="" /></p>
<p><label for="inp2">Text Field 2:</label><input type="text" name="inp2" id="inp2" value="" /></p>
<p><button type="submit">Submit</button></p>
</form>
</div>
</div>
<p class="script-link">
</p>
</div>
</body>
</html>
jshaker():入口函式。先把目標元素及其所有子元素的 position 設為 relative——這步是必須的,因為接下來要用 top/left 做位移,靜態定位的元素會忽略這兩個屬性。設好之後延遲 50ms 啟動動畫。
jshaker.animate():對元素本身與所有後代做一次 shake,然後用 setTimeout 在 50ms 後再次呼叫自己,形成無限循環的抖動。
jshaker.shake():真正產生位移的地方。擲一次骰子(Math.random() > 0.5)決定這次是動垂直還是水平方向,然後在該軸上加一個隨機偏移量。
這段位移的計算其實藏著一個邏輯瑕疵:
pos['top'] + Math.random() * 20 < 10 ? (Math.random() * 20 * (-1)) : Math.random() * 20
由於運算子優先序,< 10 的比較是先作用於「pos['top'] + Math.random() * 20」的結果,而不是作用於 random 的部分。也就是說,判斷的是「目前位置是否小於 10」,而非「隨機值是否小於 10」。加上位移是疊加在當前位置上而非固定基準,元素會逐漸累積偏移而飄走。
若想要「在原地來回抖動、不飄移」的效果,應該先記錄初始位置,每次都從基準重新計算:
$.fn.jshaker.shake = function(el){
var $el = $(el);
var base = $el.data("shakeBase") || $el.position();
$el.data("shakeBase", base);
var offset = function(){ return (Math.random() * 20) - 10; };
$el.css({
top: base.top + offset(),
left: base.left + offset()
});
};
另外提醒:這個特效是無限循環的,一旦啟動不會自己停。實用化的版本應該加上停止方法與計時器參照,例如把 setTimeout 的 id 存起來,提供 $.fn.jshaker.stop() 呼叫 clearTimeout。震動類特效也建議尊重 prefers-reduced-motion 設定,避免影響對動態敏感的使用者。