2013-06-12 39 views
0

我目前有此代码用于显示随机客户推荐。 我想用一个代码替换随机函数,它将按顺序显示引号,然后重复它们。旋转文本与淡入

<html style="direction:rtl;"> 
    <DIV id=textrotator style="FONT: 16px arial ; text-align:right; WIDTH: 100%; COLOR: rgb(255,255,255)"></DIV> 
    <body bgcolor="#FFFFFF" alink="#FFFFFF" vlink="#FFFFFF" topmargin="0" leftmargin="0" rightmargin="0"> 
    <script type = "text/javascript"> 
    var hexinput = 255; // initial color value. 

    quotation = new Array() 
    quotation[0] = "text1" 
    quotation[1] = "text2" 
    quotation[2] = "text3" 

    function fadingtext() 
    { 
     if(hexinput >111) 
     { 
     hexinput -=11; // increase color value 
     document.getElementById("textrotator").style.color="rgb("+hexinput+","+hexinput+","+hexinput+")"; // Set color value. 
     setTimeout("fadingtext()",200); // 200ms per step 
     } 
     else 
     { 
     hexinput = 255; //reset hex value 
     } 
    } 

    function changetext() 
    { 
     if(!document.getElementById){return} 
     var which = Math.round(Math.random()*(quotation.length - 1)); 
     document.getElementById("textrotator").innerHTML = quotation[which]; 
     fadingtext(); 
     setTimeout("changetext()",8000); 
    } 

    window.onload = changetext(); 
    </script> 

回答

1

您需要使您的索引为全局。将which投射到函数的外部,然后只增加它,确保在达到最后时进行换行。

这是 “changetext” 功能置换:

var which = 0; 

function changetext() 
{ 
    which += 1; 
    if (which >= quotation.length) 
    { 
     which = 0; 
    } 

    document.getElementById("textrotator").innerHTML = quotation[which]; 

    fadingtext(); 

    setTimeout("changetext()",8000); 
} 
+0

很大:-)感谢工作一吨! – user2479629

+0

@ user2479629好的,你能把它标记为“接受”吗? (在这个答案左边的up/number /下面的绿色复选框。)谢谢。 :) – Richard