代码之家  ›  专栏  ›  技术社区  ›  Tom

通过JS的随机CSS

  •  6
  • Tom  · 技术社区  · 17 年前

    我试图通过JS旋转随机CSS表单-我有以下脚本,但当我使用它时-它似乎不起作用?

    function getRand(){
        return Math.round(Math.random()*(css.length-1));
    }
    var css = new Array(
    '<link rel="stylesheet" type="text/css" href="css/1.css">',
    '<link rel="stylesheet" type="text/css" href="css/2.css">',
    '<link rel="stylesheet" type="text/css" href="css/3.css">',
    '<link rel="stylesheet" type="text/css" href="css/4.css">'
    );
    rand = getRand();
    document.write(css[rand]);
    

    谢谢你的帮助?

    9 回复  |  直到 17 年前
        1
  •  9
  •   Christian C. Salvadó    17 年前

    link

    function applyRandCSS(){
      var css = ["css/1.css", "css/2.css", "css/3.css", "css/4.css"];
      var randomFile = css[Math.round(Math.random()*(css.length-1))];
      var ss = document.createElement("link");
    
      ss.type = "text/css";
      ss.rel = "stylesheet";
      ss.href = randomFile;
    
      document.getElementsByTagName("head")[0].appendChild(ss);
    }
    
        2
  •  3
  •   Diodeus - James MacFarlane    17 年前

    “添加”样式表时,页面已呈现。这种类型的替换最好在服务器上完成。

        3
  •  0
  •   Steve Schnepp    17 年前

    css 函数内部。所以你必须在函数之前声明它?

    var css = new Array(
    '<link rel="stylesheet" type="text/css" href="css/1.css">',
    '<link rel="stylesheet" type="text/css" href="css/2.css">',
    '<link rel="stylesheet" type="text/css" href="css/3.css">',
    '<link rel="stylesheet" type="text/css" href="css/4.css">'
    );
    
    function getRand(){
        var css
        return Math.round(Math.random()*(css.length-1));
    }
    
    rand = getRand();
    document.write(css[rand]);
    
        4
  •  0
  •   montrealist    17 年前

    使用jQuery切换样式表有一种非常好的技术 here . 它可以很容易地与你的随机化组合。

        5
  •  0
  •   bobobobo    17 年前

    HI!
    
    <script>
    function getRand(){
        return Math.round(Math.random()*(css.length-1));
    }
    var css = new Array(
    '<link rel="stylesheet" type="text/css" href="1.css">',
    '<link rel="stylesheet" type="text/css" href="2.css">',
    '<link rel="stylesheet" type="text/css" href="3.css">',
    '<link rel="stylesheet" type="text/css" href="4.css">'
    );
    rand = getRand();
    
    document.write( "Choosing " + rand ) ;
    document.write(css[rand]);
    
    </script>
    
    HELLO!
    
    
    

    在1.css中

    *
    {
      color: Red;
    }
    

    在2.css中

    *
    {
      border: solid 2px green ;
    }
    

    在3.css中

    *
    {
      font-size: 40em;
    }
    

    *
    {
      border: solid 5px red ;
    }
    
        6
  •  0
  •   August Lilleaas    17 年前

    除非出于某种原因必须在客户端执行,否则请在服务器上执行。

        7
  •  0
  •   croxmeister    17 年前

    link元素必须位于head元素内部才能工作,document.createElement/element.append

        8
  •  0
  •   Marcello Bastea-Forte    17 年前

    可能是吹毛求疵,但您可能想要:

    function getRand() {
        return parseInt(Math.random()*css.length);
    }
    
        9
  •  0
  •   Jonno_FTW    17 年前

    如果您的CSS变化不大,您可以像我一样,在每次加载页面时随机生成CSS

        function mutate() {
    
    var font = new Array();
        font[0] = 'monospace';
        font[1] = 'arial';
        font[2] = 'verdana';
        font[3] = 'trebuchet-ms';
        font[4] = 'lucida grande';
        font[5] = 'calibri';
        font[6] = 'bitstream charter';
        font[7] = 'liberation sans';
        font[8] = 'Mona';
        font[9]= 'MS Pgothic';
        font[11]= 'helvetica';
        font[11]= 'Dejavu sans';
        var whichbfont = Math.floor(Math.random()*(font.length));
    
    /* Random bgcolor maker */
    function bgcolour() {
       var bred   = Math.floor(128+Math.random()*128);
       var bgreen = Math.floor(128+Math.random()*128);
       var bblue  = Math.floor(128+Math.random()*128);
       return  'rgb('+bred+', '+bgreen+', '+bblue+')'; 
    }
    var bgcolor = bgcolour();
    
    /* Random bgcolor maker */
    function bcolour() {
       var red   = Math.floor(Math.random()*128);
       var green = Math.floor(Math.random()*128);
       var blue  = Math.floor(Math.random()*128);
       return  'rgb('+red+', '+green+', '+blue+')'; 
    }
    var bcolor = bcolour;
    
    document.write ('<style type=\"text/css\">'+
    'b,a \n'+
    '{font-family: '+font[whichbfont]+' !important; color: '+bcolor+' !important;}\n'+
    'body {background-color: '+bgcolor+';!important}\n'+
    '</style>');
    }