[JS] pionowa marquee

0

napisałem taki kodzik, przesuwający tekst w dół:

var scrollSpeed = 1
var scrolls = 0;

function initScroll(id) {
 var marquee = document.getElementById(id)
 if(marquee.offsetHeight > marquee.parentNode.offsetHeight) {
  scrolls++
  resetPosition(id)
  scrollDown(id)
 }
}

function scrollDown(id) {
 var marquee = document.getElementById(id)
 marquee.style.top = parseInt(marquee.style.top) - scrollSpeed
 setTimeout("scrollDown('" + id + "')", 50)
}

function resetPosition(id) {
 var marquee = document.getElementById(id)
 marquee.style.top = 0 + marquee.parentNode.offsetHeight
 var timeOut = (((marquee.offsetHeight + marquee.parentNode.offsetHeight) * 50) / scrollSpeed) + (scrolls * 100) 
 setTimeout("resetPosition('" + id + "')", timeOut)
}

wszystko działa, tylko czy nie dałoby się zrobić tego bez wywolywania za każdym razem getElementById() i zliczania zmiennej timeOut?

0

A sprawdź to (nie wywołuje cały czas getElementById):

var scrollSpeed = 1
var scrolls = 0;
var marquee;

function initScroll(id) {
  marquee = document.getElementById(id);
 if(marquee.offsetHeight > marquee.parentNode.offsetHeight) {
  scrolls++;
  resetPosition();
  scrollDown();
 }
}

function scrollDown() {
 marquee.style.top = parseInt(marquee.style.top) - scrollSpeed;
 setTimeout("scrollDown('" + id + "')", 50);
}

function resetPosition() {
 marquee.style.top = 0 + marquee.parentNode.offsetHeight;
 var timeOut = (((marquee.offsetHeight + marquee.parentNode.offsetHeight) * 50) / scrollSpeed) + (scrolls * 100);
 setTimeout("resetPosition('" + id + "')", timeOut);
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1