javascript经典效果(五)

14年前

五 接收键盘指令的脚本:


 

  1. <SCRIPT language="JavaScript">
  2. <!--
  3. var hotkey=97
  4. var destination="http://www.wyev.com"
  5. if (document.layers)
  6. document.captureEvents(Event.KEYPRESS)
  7. function backhome(e){
  8. if (document.layers){
  9. if (e.which==hotkey)
  10. window.location=destination
  11. }
  12. else if (document.all){
  13. if (event.keyCode==hotkey)
  14. window.location=destination
  15. }
  16. }
  17. document.onkeypress=backhome
  18. //-->
  19. </SCRIPT>
  20. onkeydown="javascript:onenter();"
  21. function onenter(){
  22. if(event.keyCode==13){
  23. alert("回车");
  24. }
  25. }
复制代码


 

六 让你的文本链接渐隐渐显:


 

  1. <META NAME="Description" CONTENT="">
  2. </HEAD>
  3. <BODY>
  4. <script language="javascript" type="text/javascript">
  5. startColor = "#671700"; // 定义链接颜色
  6. endColor = "#D8D1C5";  // 定义要渐变到最后的颜色
  7. stepIn = 17;
  8. stepOut = 23;
  9. /*
  10. 定义是否让所有的文本链接自动渐变,true为是,false为否
  11. */
  12. autoFade = true;  
  13. /*
  14. 在这里定义css样式里的类class:fade,如果为true,那么你要将要渐变的链接上加上此fade样式
  15. */
  16. sloppyClass = false;
  17. hexa = new makearray(16);
  18. for(var i = 0; i < 10; i++)
  19.     hexa[i] = i;
  20. hexa[10]="a"; hexa[11]="b"; hexa[12]="c";
  21. hexa[13]="d"; hexa[14]="e"; hexa[15]="f";
  22. document.onmouseover = domouseover;
  23. document.onmouseout = domouseout;
  24. startColor = dehexize(startColor.toLowerCase());
  25. endColor = dehexize(endColor.toLowerCase());
  26. var fadeId = new Array();
  27. function dehexize(Color){
  28. var colorArr = new makearray(3);
  29. for (i=1; i<7; i++){
  30.   for (j=0; j<16; j++){
  31.    if (Color.charAt(i) == hexa[j]){
  32.     if (i%2 !=0)
  33.      colorArr[Math.floor((i-1)/2)]=eval(j)*16;
  34.     else
  35.      colorArr[Math.floor((i-1)/2)]+=eval(j);
  36.    }
  37.   }
  38. }
  39. return colorArr;
  40. }
  41. function domouseover() {
  42.   if(document.all){
  43.    var srcElement = event.srcElement;
  44.    if ((srcElement.tagName == "A" && autoFade) || srcElement.className == "fade" || (sloppyClass && srcElement.className.indexOf("fade") != -1))
  45.         fade(startColor,endColor,srcElement.uniqueID,stepIn);      
  46.    }
  47. }
  48. function domouseout() {
  49.   if (document.all){
  50.    var srcElement = event.srcElement;
  51.     if ((srcElement.tagName == "A" && autoFade) || srcElement.className == "fade" || (sloppyClass && srcElement.className.indexOf("fade") != -1))
  52.         fade(endColor,startColor,srcElement.uniqueID,stepOut);
  53.     }
  54. }
  55. function makearray(n) {
  56.     this.length = n;
  57.     for(var i = 1; i <= n; i++)
  58.         this[i] = 0;
  59.     return this;
  60. }
  61. function hex(i) {
  62.     if (i < 0)
  63.         return "00";
  64.     else if (i > 255)
  65.         return "ff";
  66.     else
  67.        return "" + hexa[Math.floor(i/16)] + hexa[i%16];}
  68. function setColor(r, g, b, element) {
  69.       var hr = hex(r); var hg = hex(g); var hb = hex(b);
  70.       element.style.color = "#"+hr+hg+hb;
  71. }
  72. function fade(s,e, element,step){
  73. var sr = s[0]; var sg = s[1]; var sb = s[2];
  74. var er = e[0]; var eg = e[1]; var eb = e[2];
  75. if (fadeId[0] != null && fade[0] != element){
  76.   setColor(sr,sg,sb,eval(fadeId[0]));
  77.   var i = 1;
  78.   while(i < fadeId.length){
  79.    clearTimeout(fadeId[i]);
  80.    i++;
  81.    }
  82.   }
  83.   
  84.     for(var i = 0; i <= step; i++) {
  85.      fadeId[i+1] = setTimeout("setColor(Math.floor(" +sr+ " *(( " +step+ " - " +i+ " )/ " +step+ " ) + " +er+ " * (" +i+ "/" +
  86.    step+ ")),Math.floor(" +sg+ " * (( " +step+ " - " +i+ " )/ " +step+ " ) + " +eg+ " * (" +i+ "/" +step+
  87.    ")),Math.floor(" +sb+ " * ((" +step+ "-" +i+ ")/" +step+ ") + " +eb+ " * (" +i+ "/" +step+ ")),"+element+");",i*step);
  88.   }
  89. fadeId[0] = element;
  90. }
  91. </script>
  92. </BODY>
  93. </HTML>
  94. <A HREF="">让你的文本链接渐隐渐显</A>
复制代码