Random Css for random background image

Discussion in 'jQuery' started by Malatya, Dec 24, 2010.

  1. #1
    Hi , I am using Charles Harvey's random background image plugin. But i have a problem when i want to add a new function. I search all the web and i cant find an answer. Need some help.

    I want to do:

    If load a blue background image then i want to make tables background color blue
    If load a red background image then i want to make tables background color red

    So, i try something. I added the green lines to code that can explain what i try to do.

    (function($) {
    
     $.randombg = {
     defaults: {
     directory: "images/backgrounds/",
     howmany: 3
    
     }
     }
        $.fn.extend({
            randombg:function(config) {
                var config = $.extend({}, $.randombg.defaults, config);
     return this.each(function() {
     
     var directory = config.directory, howmany = config.howmany;
     var which = Math.floor(Math.random()*howmany)+1;
                                    [COLOR="green"]if (which<16 ) {//1-15.jpg are blue 
                                    $('div.tableLedger').css({"background-color" : "#00688B"});
    }[/COLOR]
     $(this).css({"background" : "url(" +directory + which + ".jpg)"});
       
                })
            }
        })
    })(jQuery);
    
    Code (markup):
    this is the html <tr class="tableLedger"> and also my css is

    
    /* I want to change this 2 class*/
    .tableLedger{
     font-weight: bold;
     color: #FFFFFF; 
            background-color: #69D2E7;
     height: 26px;
    }
    
    
    .tableSubLedger{
     font-weight: bold;
     background-color: #d2f1f1;
     height: 22px;
    }
    
    Code (markup):
     
    Malatya, Dec 24, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    try something like this:
    
    $('div.tableLedger').css("background-color", "#00688B");
    // or 
    $('div.tableLedger').css("background", "#00688B");
    
    Code (markup):
     
    gapz101, Dec 25, 2010 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    Not sure about the HTML around your tr, but I don't see why you have the div included. This should work for the whole tr:

    The following three coding styles work with jQuery:

    
    $('.tableLedger').css({'background-color' : '#00688B'});
    // or
    $('.tableLedger').css({backgroundColor: '#00688B'});
    // or
    $('.tableLedger').css('backgroundColor', '#00688B');
    
    Code (markup):
     
    ThePHPMaster, Dec 26, 2010 IP
  4. Malatya

    Malatya Active Member

    Messages:
    710
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Thanks for reading and answering my friends.

    I tried both of them but it doesnt work. I tried tons of combination but cant find "where i am wrong"


    Thanks for your time and effort. I try 3 of them but i doesnt work. I tried tons of combinations but i cant work the jQuery. Background chances but table color dont. I cant found the mistake.

    Here is my html around table

    <table cellspacing="1" cellpadding="3" class="tableBorder" align="center">
    
     <tr class="tableLedger">
    
      <td width="4%">&nbsp;</td>
    
      <td width="56%">Forum</td>
    
      <td width="6%" align="center">Topics</td>
    
      <td width="6%" align="center">Sent By</td>
    
      <td width="28%" align="center">Last Post</td>
    
     </tr>
    
    Code (markup):
     
    Malatya, Dec 27, 2010 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Just for testing purposes, change your tr to:

    <tr class="tableLedger" id="theTD">

    and your javascript to:

    $('#theTD').css('backgroundColor', '#00688B');

    Does it work?
     
    ThePHPMaster, Dec 27, 2010 IP
  6. Malatya

    Malatya Active Member

    Messages:
    710
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I tried , it doesnt work too:) Thanks for your reply ThePHPMaster. You are so helpfull.

    I get the answer in another forum and i want to share it for other people.

    
    //Thanks to Engin Deniz
    $(document).ready(function(){
          bgImageTotal=44;
    
          randomNumber = Math.round(Math.random()*(bgImageTotal-1))+1;
     
          imgPath=('images/'+randomNumber+'.jpg');
    
          $('body').css('background-image', ('url("'+imgPath+'")'));
    if(randomNumber < 23) {
    $('.tableLedger').css({"background" : "url(mavi.png)"});
    }
    
          });  
    Code (markup):
     
    Malatya, Dec 28, 2010 IP
  7. eminenzmedia

    eminenzmedia Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    Quite interesting... its useful as well
    Thanks all for sharing
     
    eminenzmedia, Dec 28, 2010 IP