2010-07-20 29 views
2

我是一個iPhone應用程序開發者和有關於css.I非常瞭解甚少有一個代碼來創建一個沒有cells.here這就是:CSS代碼定製

<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> 

在上面

下面的一段代碼被用於設置單元格的背景色和圖像

//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'); 
      } 

現在我需要的是動態改變背景圖片...我的意思,而不是使用

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

我想用變量類似

background-image:url(var); 

是否有可能在CSS?....如果沒有那麼任何人都可以有任何想法,以動態地設置圖像的名字?

回答

2

在純CSS中是不可能的。你可以使用JavaScript來設置負載background-image屬性:

jQuery的例子:

$(document).ready(function(){ 

    $('.ad-carousel-view .cells > li.active').each(function(){ 
     var img = getYourImagePath(); 
     $(this).css({backgroundImage: img}); 
    }); 
}); 
+0

我認爲你的權利..... – Rony 2010-07-20 13:10:04

+0

你可以給我更多的細節...我試圖使用的代碼..在負載上的HTML身體我稱之爲功能..setImage.like..but沒有任何反應.. function setImage() \t { \t \t alert(「Hello ..」); \t $('。ad-carousel-view .cells> li.active')。each(function(){ \t \t alert(「Hello ..「); VAR IMG = getYourImagePath(); $(本)的.css({背景圖片:IMG}); });定義 \t \t } – Rony 2010-07-21 05:22:33

+0

你有一個叫getYourImagePath(功能)這就是你將需要把返回背景圖像路徑的邏輯 – Pat 2010-07-21 10:27:49

0

是的,這應該沒問題。你測試過了嗎?我確定它可以正常工作。

+0

如何設置「無功」 ......假設我知道「禮」,我需要Li +」在JPG。 「..然後,如果李= 4然後4.jpg圖像的值將加載...然後 做background-image:url(li +」。jpg「);就是這樣?? – Rony 2010-07-20 13:07:55

0

LESS允許你在CSS :)

示例使用變量:

@brand_color: #4D926F; 

#header { 
    color: @brand_color; 
} 

h2 { 
    color: @brand_color; 
} 
+0

如何動態地賦值給這個變量 – Rony 2010-07-21 03:52:22

1

如果您使用內聯s使用服務器端技術,則可以在HTML中執行此操作tyle,所以你的元素,你會碰到這樣的:

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

,你將取代###與任何輸出方法您使用的是動態地設置圖像。

你也可以用JavaScript設置,如果你沒有任何服務器端的技術工作:

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

jQuery是相似的,但更好一點,因爲它可以拿起類,而不是隻該ID - 如果你想申請的頁面上有多個活動類,其效果會更好:

$(".active").css("backgroundImage",yourImage); 
+0

我要去嘗試你的想法... – Rony 2010-07-20 13:11:21

+0

如何添加JQuery ... ??我想要在這裏設置圖像 .ad-carousel-view .cells> li { width:200px; height:70px; padding-top:16px; padding-left:20px; 顏色:白色; font-size:20px; font-family:「Helvetica Neue」,Helvetica; -webkit-border-radius:10px; -webkit-box-sizing:border-box; \t \t background-image:url(li); } – Rony 2010-07-21 04:05:43

+0

如果你之前從未使用jQuery,那麼最好的地方始終是優秀的文檔:http://docs.jquery.com/Downloading_jQuery ...和http://api.jquery.com/class-selector /我們在這裏討論的內容 – hollsk 2010-07-21 08:40:51