也許你的問題是與"swing"緩和功能的jQuery默認情況下使用。
你可能想嘗試"linear"寬鬆函數:
$(document).ready(function() {
function pulsate() {
$("#pulsating-block").
animate({opacity: 0.2}, 3000, 'linear').
animate({opacity: 1}, 3000, 'linear', pulsate);
}
pulsate();
});
我還編寫了一個小插件搏動包括"bounce"緩和功能,可能是根據自己的喜好。我應該注意到插件使用連續計算來執行動畫,而不是兩個單獨的淡入/淡出動畫,所以它可能有助於解決「暫停」問題。 (說實話,我還沒有看到你所談論的停頓。)
工作演示
http://jsbin.com/isicu(通過http://jsbin.com/isicu/edit編輯)
全部來源
的JavaScript
(function ($) {
$.fn.pulsate = function (properties, duration, type, speed, callback) {
type = type || 'Swing'
speed = speed || 'Normal';
this.animate(properties, duration, 'pulsate' + type + speed, callback);
};
function createPulsateLinear (speed) {
speed *= 2;
return function (p, n) {
return (Math.asin(Math.sin(Math.PI * n/speed)) + Math.PI/2)/Math.PI;
}
}
function createPulsateSwing (speed) {
return function (p, n) {
return (1 + Math.sin(n/speed))/2;
}
}
function createPulsateBounce (speed) {
speed *= 2;
return function (p, n) {
return (
((Math.asin(Math.sin(Math.PI * n/speed)) + Math.PI/2)/Math.PI) *
(Math.sin(Math.PI * n/speed) + 1)/-2 + 1
);
}
}
var speeds = {
fast: 100,
normal: 200,
slow: 400
}
$.extend(jQuery.easing, {
pulsateLinearFast: createPulsateLinear(speeds.fast),
pulsateLinearNormal: createPulsateLinear(speeds.normal),
pulsateLinearSlow: createPulsateLinear(speeds.slow),
pulsateSwingFast: createPulsateSwing(speeds.fast),
pulsateSwingNormal: createPulsateSwing(speeds.normal),
pulsateSwingSlow: createPulsateSwing(speeds.slow),
pulsateBounceFast: createPulsateBounce(speeds.fast),
pulsateBounceNormal: createPulsateBounce(speeds.normal),
pulsateBounceSlow: createPulsateBounce(speeds.slow)
});
})(jQuery);
$(document).ready(function() {
var
pulsatingBlocks = $('.pulsating-block'),
forever = 5 * 24 * 60 * 60 * 1000; // 5 days! (Which is forever in Internet time)
pulsatingBlocks.filter('.opacity').each(function() {
$(this).pulsate({opacity: 0.2}, forever, this.className.split(' ')[0], 'Slow');
});
pulsatingBlocks.filter('.top').each(function() {
$(this).pulsate({top: 100}, forever, this.className.split(' ')[0], 'Slow');
});
});
HTML
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
<meta charset=utf-8 />
<title>Pulsate</title>
<style>
div { clear: left; margin-bottom: 2em; }
.pulsating-block {
width: 6em; height: 4em;
margin: 0.5em; margin-right: 10em;
float: left; clear: none; position: relative;
background: lightblue;
text-align: center;
padding-top: 2em;
font: bold 1em sans-serif;
}
</style>
</head>
<body>
<div>
<div class="Linear opacity pulsating-block">linear</div>
<div class="Swing opacity pulsating-block">swing</div>
<div class="Bounce opacity pulsating-block">bounce</div>
</div>
<div>
<div class="Linear top pulsating-block"></div>
<div class="Swing top pulsating-block"></div>
<div class="Bounce top pulsating-block"></div>
</div>
</body>
</html>
我試過這段代碼,並沒有在動畫中看到停頓。只有我嗎?你在慢速機器還是舊瀏覽器上運行? – brianpeiris 2010-01-29 20:18:52
我在四核G5上運行,所以我不認爲它是一個速度問題:-),並且我已經在Firefox和Safari(最新版本)中嘗試了這一點。我也使用jQuery的v1.4.1,這是最新的版本。儘量縮短時間,這可能有助於看到我在說什麼。 下面是它是如何出現在我身上的: \\\\\ ///// ---- \\\\\\ ///// ---- (「\」=淡出, 「/」淡入,「 - 」=不透明度不變) – 2010-01-29 21:12:30