tags:

views:

53

answers:

4

Hi, I am an iPhone App Developer and have very very little knowledge about css.I have a code to create a no of cells.here this is:

<style type="text/css" media="screen">

      body {
        margin: 0;
        width: 320px;
        height: 200px;
      }

      .ad-carousel-view .cells > li {
        width: 200px;
        height: 70px;
        padding-top: 16px;
        padding-left: 20px;
        color: white;
        font-size: 20px;
        font-family: "Helvetica Neue", Helvetica;
        -webkit-border-radius: 10px;
        -webkit-box-sizing: border-box;
        background-image:url('13.jpg');
      }

      .ad-carousel-view .cells > li:nth-child(odd) {
        background-color: rgb(255,59,59);

      }

      .ad-carousel-view .cells > li:nth-child(even) {
        background-color: rgb(80,80,80);
        background-image:url('13.jpg');
      }

      .ad-carousel-view .cells > li.active {
        background-color: red;
        background-image:url('23.jpg');
      }

    </style>
    <style type="text/css">

    .wrap{
    border:solid 1px #000000;
    width:320px;
    height:150px;
    position:relative;
    top:0px;
    left:0px;
    }

    </style>

in the above code the following section is used to set cell's BG color and image

//odd cell
    .ad-carousel-view .cells > li:nth-child(odd) {
            background-color: rgb(255,59,59);

          }
    //even cell
          .ad-carousel-view .cells > li:nth-child(even) {
            background-color: rgb(80,80,80);
            background-image:url('13.jpg');
          }
    //highlighted cell
          .ad-carousel-view .cells > li.active {
            background-color: red;
            background-image:url('23.jpg');
          }

now what i need is to change BG image dynamically...i mean instead of using

background-image:url('23.jpg');

i want use variable something like

background-image:url(var);

is it possible in CSS??....if no then can anyone have any idea to dynamically set image name??

A: 

Yes this should be fine. Have you tested it? I'm sure it works OK.

Joel Kennedy
how to set the 'var'... suppose i know 'li' and i need li+".jpg"..then if the value of li=4 then 4.jpg image will load...then does background-image:url(li+".jpg"); is the way??
Rony
+2  A: 

It's not possible in pure CSS. You could use Javascript to set the background-image property on load:

jQuery example:

$(document).ready(function(){

    $('.ad-carousel-view .cells > li.active').each(function(){
        var img = getYourImagePath();
        $(this).css({backgroundImage: img});
    });
});
Pat
i think u r right.....
Rony
can u send me more details...i tried to use the code..on html body on load i call the function..setImage.like..but nothing happens.. function setImage() { alert("Hello.."); $('.ad-carousel-view .cells > li.active').each(function(){ alert("Hello.."); var img = getYourImagePath(); $(this).css({backgroundImage: img}); }); }
Rony
Do you have a function called getYourImagePath() defined? That's where you're going to need to put the logic that returns the background image path.
Pat
A: 

The LESS allows you to use variables in the CSS :)

Example:

@brand_color: #4D926F;

#header {
  color: @brand_color;
}

h2 {
  color: @brand_color;
}
Sarfraz
how to dynamically assign value to this variable??
Rony
+1  A: 

You can do this in your HTML if you're using a server-side technology by using an inline style, so on your element you'd have something like:

<li class="active" style="background-image:url(###);"></li>

where you would replace ### with whatever output method you're using to dynamically set the image.

You can also set it with javascript if you don't have any server-side technology to work with:

var obj= document.getElementById('aUniqueId');
obj.style.backgroundImage = yourImage;

jQuery is similar but a little nicer because it can pick up the class instead of just the ID - which would be better if you want to apply more than one active class on the page:

$(".active").css("backgroundImage",yourImage);
hollsk
i am going to try ur idea...
Rony
how to add JQuery...??i want to set the image here.ad-carousel-view .cells > li { width: 200px; height: 70px; padding-top: 16px; padding-left: 20px; color: white; font-size: 20px; font-family: "Helvetica Neue", Helvetica; -webkit-border-radius: 10px; -webkit-box-sizing: border-box; background-image:url(li); }
Rony
If you've never used jQuery before then the best place to start is always with the documentation which is excellent: http://docs.jquery.com/Downloading_jQuery... and http://api.jquery.com/class-selector/ for what we're talking about here
hollsk
$(" .ad-carousel-view .cells > li:nth-child(odd)").css("background-image","333_1.jpg");i use something like this....but image does not load....can help??
Rony
ok....i have done....thanks a lot...
Rony