jQuery plugin for generating multi coloured text which changes colour on hover

前端 未结 2 1993
予麋鹿
予麋鹿 2021-01-28 11:42

I would like to generate multicoloured text for various links, with a random assignment of colours to individual letters, from a pre-specified array of colours, which would chan

相关标签:
2条回答
  • 2021-01-28 12:08

    You could try this:

       $('a').on('hover', function( {
          $(this).css('color', getRandomColor());
         })
       );
    
    function getRandomColor() {
      var myAwesomeColorList['#ccc', '#fff'];
    
      return myAwesomeColorList[Math.floor((Math.random()*myAwesomeColorList.length))]
    }
    
    0 讨论(0)
  • 2021-01-28 12:26

    Ok i whip up an example using jquery.

    first your text

    <p id="example">Multi color me</p>​
    

    then the javascript:

    $(document).ready(function() {
      var test = $("#example").text().split('');
    
        var result = "";
        var i = 0;
        for(i=0; i < test.length; i++) {
            result += "<span style='color:"+getColor()+"'>"+test[i]+"</span>";
        }
        $("#example").html(result);      
    });
    
    function getColor() {
        var colList = ['#00FF00', '#FF0000','#0000FF'];
    
        var i = Math.floor((Math.random()*colList.length));
      return colList[i];
    }​
    

    Heres the jsFiddle Example

    Note: i did not do the hover but I guessing you can take it from here :)

    0 讨论(0)
提交回复
热议问题