简体   繁体   中英

rotating text with fade-in

I currently have this code for displaying random customer testimonials. I would like to replace the random function with a code that will display the quotes by their order and then repeat them.

<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>

You need to make your index global. Throw which outside of the function and then just increment it, make sure to wrap when you get to the end.

This is the replacement for the "changetext" function:

var which = 0;

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

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

    fadingtext();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM