Thursday, October 20, 2011

Javascript to Move the HTML Body Background Image (Shake Like Wave)

Call this wave_background() function and your background will shake like you are underwater. But before that, make sure you have a background image that can mirror to x or y axis. Otherwise, the shake will look ugly.




<script>

var xpos = 100;
var wave_timer;
var tmp_string;
var ran_num;
var ran_dir;

function wave_background() {
wave_timer=setInterval("move_background()",200);
}



function move_background() {
ran_num = Math.floor(Math.random()*5);
ran_dir = Math.floor(Math.random()*6);
if (ran_dir == 2) {
xpos = xpos - ran_num;
}
else {
xpos = xpos + ran_num;
}
tmp_string = xpos + '';
tmp_string = tmp_string + 'px -100px';
document.body.style.backgroundPosition = tmp_string;
}



</script>

No comments:

Post a Comment