JavaScript / jQueryを介してテキストを拡大縮小および移動しています。jQuerys animate() はフェードインとフェードアウトする必要があり、繰り返す必要があり、より多くの要素を使用する必要があるため、使用できません (最終結果: 背景から「飛んで」、さまざまな方向に移動してフェードアウトします)。
私の問題: スムーズに実行されておらず、かなりの CPU 使用率を引き起こしています。以下は、簡略化されたバージョンです。
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript">
var steps = 300; // steps from start to finish
// the final css values
var endValueTop = 300;
var endValueLeft = 300;
var endValueFontSize = 100;
// the current css values (get encreased in doAnimation())
var currentValueTop = 100;
var currentValueLeft = 100;
var currentValueFontSize = 0;
// the added values in each step
var stepsTop = (endValueTop - currentValueTop) / steps;
var stepsLeft = (endValueLeft - currentValueLeft) / steps;
var stepsFontSize = (endValueFontSize - currentValueFontSize) / steps;
function doAnimation() {
// increase current values
currentValueTop += stepsTop;
currentValueLeft += stepsLeft;
currentValueFontSize += stepsFontSize;
// apply them
$("#hello").css({
top: currentValueTop,
left: currentValueLeft,
fontSize: currentValueFontSize
});
// restart if there are steps left
if (steps--) {
window.setTimeout(function(){
doAnimation();
}, 50);
}
}
// start the first time
doAnimation();
</script>
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
body { position: relative; }
p#hello { position: absolute; top: 100px; left: 100px; font-size: 0px; }
</style>
</head>
<body>
<p id="hello">Hello World</p>
</body>
</html>
JS BINでの実行例。
助言がありますか?おまけ:CPU負荷を減らす方法は?どうもありがとう!
シュテファン