Главная » 2009 » Октябрь » 14 » Футбольный мяч летает по странице (Javascript)
14.10.2009, 17:41

Футбольный мяч летает по странице



Футбольный мяч летает по странице. Хорошо подойдет для фан-сайтов.

Это в head:

Code
<SCRIPT>
<!-- Beginning of JavaScript -

var x,y
var marginbottom
var marginleft=0
var margintop=0
var marginright

// less is faster
var tempo=10

// horizontal move (pixels) within time unit
var stepx=20

// vertical move (pixels) within time unit
var stepy=20
var timer

// preloading of the four balls
img0 = new Image();
img0.src = "primer86-1.gif";

img1 = new Image();
img1.src = "primer86-2.gif";

img2 = new Image();
img2.src = "primer86-3.gif";

img3 = new Image();
img3.src = "primer86-4.gif";

// wait 2 seconds after loading (to ensure that images are really loaded
function setValues() {
  var firsttimer= setTimeout("setValues2()",2000)
}

// check the width and the height of the document
// gets the starting position of the ball by random
function setValues2() {
  if (document.all) {
  marginbottom = document.body.clientHeight-50
  marginright = document.body.clientWidth-50
  document.all.ball.style.posLeft=randommaker(marginright-30)+30
  document.all.ball.style.posTop=10
  moveball()
  }
  if (document.layers) {
  marginbottom = window.innerHeight-10
  marginright = window.innerWidth-10
  document.ball.left=randommaker(marginright-30)+30
  document.ball.top=10
  moveball()
  }
}

// randomfunction
function randommaker(range) {  
  rand=Math.floor(range*Math.random())
  return rand
}

function moveball() {
  checkposition()
  chooseimage()
  if (document.all) {  
  document.all.ball.style.posLeft+=stepx
  document.all.ball.style.posTop+=stepy
  timer=setTimeout("moveball()",tempo)
  }
  if (document.layers) {
  document.ball.left+=stepx
  document.ball.top+=stepy
  timer=setTimeout("moveball()",tempo)
  }
}

function checkposition() {
  if (document.all) {
  if (document.all.ball.style.posLeft>=marginright) {
  stepx=stepx*-1
  document.all.ball.style.posLeft-=10
  }
  if (document.all.ball.style.posLeft<=marginleft) {
  stepx=stepx*-1
  document.all.ball.style.posLeft+=10
  }  
  if (document.all.ball.style.posTop>=marginbottom) {
  stepy=stepy*-1
  document.all.ball.style.posTop-=10
  }
  if (document.all.ball.style.posTop<=margintop) {
  stepy=stepy*-1
  document.all.ball.style.posTop+=10
  }
  }
  if (document.layers) {
  if (document.ball.left>=marginright) {
  stepx=stepx*-1
  document.ball.left-=10
  }
  if (document.ball.left<=marginleft) {
  stepx=stepx*-1
  document.ball.left+=10
  }  
  if (document.ball.top>=marginbottom) {
  stepy=stepy*-1
  document.ball.top-=10
  }
  if (document.ball.top<=margintop) {
  stepy=stepy*-1
  document.ball.top+=10
  }
  }
}

function chooseimage() {
  if (document.all) {
  if (stepx <= 0 && stepy <=0) {ballimage.src="primer86-2.gif"}
  if (stepx >= 0 && stepy <=0) {ballimage.src="primer86-4.gif"}
  if (stepx <= 0 && stepy >=0) {ballimage.src="primer86-1.gif"}
  if (stepx >= 0 && stepy >=0) {ballimage.src="primer86-3.gif"}
  }
   
  if (document.layers) {
  if (stepx <= 0 && stepy <=0) {document.ball.document.ballimage.src="primer86-2.gif"}
  if (stepx >= 0 && stepy <=0) {document.ball.document.ballimage.src="primer86-4.gif"}
  if (stepx <= 0 && stepy >=0) {document.ball.document.ballimage.src="primer86-1.gif"}
  if (stepx >= 0 && stepy >=0) {document.ball.document.ballimage.src="primer86-3.gif"}
  }
}

// - End of JavaScript - -->
</SCRIPT>

body заменяем на это:

Code
<BODY bgColor=#ffffff id=thisbody onload=setValues()><B>
<P align=center>"А здесь скачет мячик"</P></B>
<DIV id=ball style="POSITION: absolute; TOP: -50px"><IMG border=0 name=ballimage  
src="primer86-3.gif" width="45" height="45"></DIV>

Везде в скрипте заменяем primer86-2.gif, primer86-4.gif, primer86-1.gif, primer86-3.gif на путь до своих картинок мяча.

Сами картинки:

Материал взят с сайта infoscript.ru
Внимание! Копирование данного материала запрещено без указания ссылки на сайт Infoscript.ru
Источник/Автор: Неизвестен
Категория: JavaScript's | Просмотров: 2816 | Добавил: antisept | Рейтинг: 2.5/2 |
Теги: Футбольный мяч летает по странице
Пока комментариев нет. Ты можешь быть первым!
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]