Ciao a tutti,
ho uno script che server per realizzare uno scroller (tipo le classiche news a scorrimento).
Tale script funziona ma vorrei riuscire a trasformarlo in una classe con prototype ma ho delle difficoltà.
Script originale:
codice:
var speed = 5;
var tickerObj;
var containerHeight = null;
var contentHeight = null;
var currentX = 0;
var newsTikerTimer = null;
/*Speed 1 to 10, if null, default value is 5*/
function initialiseList(container, content, speed) {
document.getElementById(container).className = 'boxNewsContainer';
document.getElementById(content).className = 'boxNewsContent';
containerHeight = document.getElementById(container).clientHeight;
tickerObj = document.getElementById(content);
if ( (speed > 0) && (speed <= 10) )
this.speed = speed;
if(!tickerObj)
return false;
var list = tickerObj.childNodes;
if(list.length <= 0)
return false;
for (var i=0; i<list.length; i++) {
var node = list[i];
if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
tickerObj.removeChild(node);
}
list = tickerObj.getElementsByTagName("div");
if(list.length <= 0)
return false;
for (var i=0; i<list.length; i++) {
list[i].className = 'newsScorrevoliItem';
list[i].onmouseover = clearTimer;
list[i].onmouseout = startTimer;
}
contentHeight = tickerObj.clientHeight;
startTimer();
}
function run() {
currentX = currentX - 1;
if ( (currentX < 0) && (Math.pow(currentX,2) > Math.pow(contentHeight,2)) )
currentX = containerHeight;
tickerObj.style.top = currentX+'px';
}
function clearTimer(){
clearInterval(newsTikerTimer);
}
function startTimer(){
newsTikerTimer = window.setInterval("run()", 100-(speed*10));
}
Script riscritto con prototype:
codice:
var VerticalScroller = Class.create({
initialize: function(container, content, speed){
this.speed = 5;
this.tickerObj;
this.containerHeight = null;
this.contentHeight = null;
this.currentX = 0;
this.newsTikerTimer = null;
document.getElementById(container).className = 'boxNewsContainer';
document.getElementById(content).className = 'boxNewsContent';
this.containerHeight = document.getElementById(container).clientHeight;
this.tickerObj = document.getElementById(content);
if ( (speed > 0) && (speed <= 10) )
this.speed = speed;
if(!this.tickerObj)
return false;
var list = this.tickerObj.childNodes;
if(list.length <= 0)
return false;
for (var i=0; i<list.length; i++) {
var node = list[i];
if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
this.tickerObj.removeChild(node);
}
list = this.tickerObj.getElementsByTagName("div");
if( list.length <= 0 )
return false;
for ( var i=0; i<list.length; i++ ) {
list[i].className = 'newsScorrevoliItem';
list[i].onmouseover = this.clearTimer;
list[i].onmouseout = this.startTimer;
}
this.contentHeight = this.tickerObj.clientHeight;
this.startTimer();
},
run: function() {
this.currentX = this.currentX - 1;
if ( (this.currentX < 0) && (Math.pow(this.currentX,2) > Math.pow(this.contentHeight,2)) )
this.currentX = this.containerHeight;
this.tickerObj.style.top = this.currentX+'px';
},
clearTimer: function(){
clearInterval(this.newsTikerTimer);
},
startTimer: function (){
this.newsTikerTimer = window.setInterval("this.run()", 100-(this.speed*10));
}
});
In grassetto ho evidenziato i passaggi che temo siano errati.
Grazie in anticipo a tutti quelli che ci vorranno perdere la testa.