ランダムな不透明度の変更(部分的に成功)に関連付けられた、divのランダムな動き(成功)を作成しようとしています。2 つの別々のスクリプトを 1 つにまとめることで、次のようにまとめました。
不透明度は、div の各移動後にのみ変更されます。最終的には、動きとは独立して不透明度を機能させたいと思います。どんな助けでも大歓迎です!
私はそれをjsFiddle hereに持っています、または:
HTML:
<div id="container">
<div class="a"></div>
</div>
CSS:
div#container {height:500px;width:100%;}
div.a {
width: 284px;
height:129px;
background:url(http://www.themasterbetas.com/wp-content/uploads/2013/08/aliens.png);
position:fixed;
}
jQuery:
$(document).ready(function() {
animateDiv();
runIt();
});
function makeNewPosition($container) {
// Get viewport dimensions (remove the dimension of the div)
$container = ($container || $(window));
var h = $container.height() - 50;
var w = $container.width() - 50;
var nh = Math.floor(Math.random() * h);
var nw = Math.floor(Math.random() * w);
return [nh, nw];
}
function animateDiv() {
var $target = $('.a');
var newq = makeNewPosition($target.parent());
var oldq = $target.offset();
var speed = calcSpeed([oldq.top, oldq.left], newq);
$('.a').animate({
top: newq[0],
left: newq[1]
}, speed, function() {
animateDiv();
});
}
function calcSpeed(prev, next) {
var x = Math.abs(prev[1] - next[1]);
var y = Math.abs(prev[0] - next[0]);
var greatest = x > y ? x : y;
var speedModifier = 0.1;
var speed = Math.ceil(greatest / speedModifier);
return speed;
}
function runIt() {
var $fading = $('.a');
$fading.fadeTo("fast", Math.random(), runIt);
}