// Version Date Author Description // ------- ----------- ------------------- ---------------------------------------------- // 1.00 Steven Dejan Initial release // 1.01 2006Jul06 Steven Dejan Update to work in firefox /****************** How to use the scroller: ************************** Set the following html block in the form, where you want the scroller to be displayed
<<>> Height that the scroller has to have <<>> name of the scroller instance (see further) <<>> name of the first div <<>> name of the second div Set this liddle bit of code beneeth the html block: */ var scrollers = new Array(); /* * Constructor */ function NewsScroller(){ scrollers[scrollers.length] = this; this.ind = scrollers.length-1; //set methods this.addItem = NewsScroller_addItem; this.startTextScrolling = NewsScroller_startVTextScroll; this.stopScrolling = NewsScroller_stopScrolling; this.startScrolling = NewsScroller_startScrolling; this.getNextItem = NewsScroller_getNextItem; //properties to set the divs this.firstDiv = ""; this.secondDiv = ""; //set default proerties this.scrollerRate=25; this.scrollerStep=5; //Max=5 this.scrollerheight=200; this.pausebetweenimages=3000; this.itemsToShow = 1; //set system properties, do not change these, internal use!!! this.nextIndex = 0; this.scroll = true; this.first = true; this.vSlideContent=new Array(); } /* * Use this method to add a new item to the scroller. */ function NewsScroller_addItem(item){ this.vSlideContent[this.vSlideContent.length] = item; } /* * Use this method to start the scroller */ function NewsScroller_startVTextScroll(){ //sort the array (twice for good random) before starting if(this.vSlideContent.length>1){ this.vSlideContent.sort( randOrd ); this.vSlideContent.sort( randOrd ); } //test how much items the scroller has. if(this.vSlideContent.length>1){ //Test if more than one item has to be shown if(this.itemsToShow>1){ getElem(this.firstDiv).innerHTML = "
" + this.getNextItem(0) + "

" + this.getNextItem(0) + "
"; getElem(this.secondDiv).innerHTML = "
" + this.getNextItem(0) + "

" + this.getNextItem(0) + "
"; }else{ getElem(this.firstDiv).innerHTML = this.getNextItem(0); getElem(this.secondDiv).innerHTML = this.getNextItem(0); } //start scrolling first div (second will follow) getElem(scrollers[this.ind].firstDiv).style.top=this.scrollerStep + "px"; getElem(scrollers[this.ind].secondDiv).style.top=parseInt(getElem(scrollers[this.ind].secondDiv).parentNode.style.height,10)*-1; NewsScroller_moveUp(this.ind); }else{ //If there is only one item, fill the div's and don't scrolling getElem(this.firstDiv).innerHTML=this.vSlideContent[0]; getElem(scrollers[this.ind].firstDiv).style.top="0px"; } } /* * This function is used internally to manage the scrolling of the 2 items. */ function NewsScroller_moveUp(index){ var ns = scrollers[index]; div1 = getElem(ns.firstDiv); div2 = getElem(ns.secondDiv); //if the first panel reached top of the parent panel, pauze the scrolling if(parseInt(div1.style.top, 10)==ns.scrollerStep){ //if this is the first time, don't renew the content if(ns.first){ ns.first=false; //else get new content for the second item }else{ //if more than 1 item has to be shown at the same time, fill the second item with new content (2 items) if(ns.itemsToShow>1){ div2.innerHTML = "
" + ns.getNextItem(0) + "

" + ns.getNextItem(0) + "
"; //else fill panel with 1 item }else{ div2.innerHTML = ns.getNextItem(0); } } //freeze the 2 items to the starting position div1.style.top = "0px"; div2.style.top = "0px"; //after a smaal pauze, continue scrolling setTimeout("NewsScroller_moveUp("+index+")",ns.pausebetweenimages); //when the first panel is not out of the sight, scroll up }else if(parseInt(div1.style.top, 10)>(ns.scrollerheight*-1)){ //only scroll if the user is not with his mouse over the panel if(ns.scroll){ div1.style.top = (parseInt(div1.style.top, 10) - ns.scrollerStep) + "px"; div2.style.top = (parseInt(div2.style.top, 10) - ns.scrollerStep) + "px"; } setTimeout("NewsScroller_moveUp("+index+")",ns.scrollerRate); //the first panel is out of the view, so set it back to the bottom and refill its content }else{ //chech if 1 or 2 items must be visible and refill content if(ns.itemsToShow>1){ div1.innerHTML = "
" + ns.getNextItem(0) + "

" + ns.getNextItem(0) + "
"; }else{ div1.innerHTML = ns.getNextItem(0); } //set the first panel on the bottom, the second must be completely visible now div1.style.top = div1.parentNode.style.height; div2.style.top = parseInt(div2.parentNode.style.height,10)*-1; //pauze and scroll again setTimeout("NewsScroller_moveUp("+index+")",ns.pausebetweenimages); } } /* * This method makes the scroller start scrolling again */ function NewsScroller_stopScrolling(){ this.scroll = false; } /* * This method makes sure the scroller stops scrolling */ function NewsScroller_startScrolling(){ this.scroll = true; } /* * This method is used as a short handed version of 'document.getElementById()' */ function getElem(id) { return document.getElementById ? document.getElementById(id) : document.all[id]; } /* * This method returns the next item in the scroller's items range */ function NewsScroller_getNextItem(max){ nextItem = this.vSlideContent[this.nextIndex]; if(this.nextIndex==(this.vSlideContent.length-1)){ this.nextIndex = 0; }else{ this.nextIndex += 1; } return nextItem; } /* * This method returns a random integer, used to sort an array */ function randOrd(a, b){ return (Math.round(Math.random())-0.5); }