2011-07-11 70 views
3

我有以下的HTML代碼指定所選選項卡的樣式在導航欄

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta name="viewport" content="width=device-width; initial-scale=1.0" /> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.css" /> 
     <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.0b1/jquery.mobile-1.0b1.min.js"></script> 
    </head> 
    <body> 
     <div data-role="page" id="home"> 
      <div data-role="header" data-theme="b"> 
       <h1>Test</h1> 
      </div> 
      <div data-role="content" data-theme="d"> 
       <div data-role="navbar" data-theme="d"> 
        <ul> 
         <li><a href="#" data-icon="custom" class="ui-btn-active">Home</a></li> 
         <li><a href="#" data-icon="grid">Second page</a></li> 
         <li><a href="#" data-icon="star">Third page</a></li> 
        </ul> 
       </div> 
      </div> 

     </div> 

    </body> 
</html> 

我想實現的是設置一些樣式在導航欄當前活動的選項卡並更改圖標和背景對於選定的選項卡(圖標對於不同的選項卡會有所不同)。

使用這個CSS我可以針對活動標籤

.ui-btn-active{ 
    background:red !important; 
} 

但我想分別針對相應的標籤,因爲我需要爲每個標籤獨立選擇的圖標(或者讓說,爲了便於說明,爲特定目的的我需要爲每個標籤不同的活動標籤的顏色:紅色第一,藍色爲第二,綠色爲第三

你可以在這裏看到它 - http://jsfiddle.net/8pwFK/

請讓我知道如何實現this.Thanks提前

編輯:我面對樹立了一個選擇狀態爲我定製icon.This真正的挑戰是我使用的自定義圖標的CSS:

.tab1 .ui-icon { background: url(icon1.png) 50% 50% no-repeat; background-size: 30px 30px; } 

我想我需要以某種方式連接.ui-btn-active.ui-icon爲此work.But我無法弄清楚如何。

回答

2

添加一個ID您要的風格,像這樣用CSS每個元素:

活生生的例子:

HTML:

<li><a href="#" data-icon="custom" class="ui-btn-active" id="custom-li-1">Home</a></li> 
<li><a href="#" data-icon="grid" id="custom-li-2">Second page</a></li> 
<li><a href="#" data-icon="star" id="custom-li-3">Third page</a></li> 

CSS:

#custom-li-1.ui-btn-active { 
    background:green !important; 
} 

#custom-li-2.ui-btn-active { 
    background:red !important; 
} 

#custom-li-3.ui-btn-active { 
    background:blue !important; 
} 

文檔,自定義圖標:

自定義圖標

要使用自定義圖標,指定具有獨特的名稱,如數據圖標值myapp-email和按鈕插件將通過在數據圖標值前綴ui-icon-生成一個類並將其應用於按鈕。然後,您可以編寫一個針對ui-icon-myapp-email類的CSS規則來指定圖標背景源。爲了保持視覺一致性,請創建一個18x18像素的白色圖標作爲具有Alpha透明度的PNG-8保存。

使用第三方圖標設置

您可以添加任何類似Glyphish流行的圖標庫的實現具有堆疊在文字標籤的頂部大圖標iOS的風格標籤選項卡。所需要的只是一些自定義樣式來鏈接到圖標並將其放置在導航欄中。下面是使用Glyphish圖標和自定義樣式的例子在我們的導航欄(對於風格查看網頁的源文件):

相關鏈接:

UPDATE:

以下是我認爲你正在尋找的東西:

JS:

$('#custom-li-1').click(function() { 
    $(this).attr('data-icon','star'); 
    $(this).children().children().next().removeClass('ui-icon-custom').addClass('ui-icon-star'); 
}).page(); 

$('#custom-li-2').click(function() { 
    $(this).attr('data-icon','home'); 
    $(this).children().children().next().removeClass('ui-icon-grid').addClass('ui-icon-home'); 
}).page(); 

$('#custom-li-3').click(function() { 
    $(this).attr('data-icon','grid'); 
    $(this).children().children().next().removeClass('ui-icon-star').addClass('ui-icon-grid'); 
}).page(); 
+0

Thanks..What有關該圖標。我正在使用自定義圖標和我?需要在選定狀態下顯示圖標。 – user700284

+0

更新我的答案,以幫助自定義圖標 –

+0

感謝您的答案。我已成功添加自定義圖標。但我的挑戰是,當我選擇一個選項卡時,我需要更改該選項卡中的圖標,以便爲圖標「選中「感覺。預先感謝 – user700284

2

合併CSS類。

HTML:

<li><a href="#" data-icon="custom" class="home ui-btn-active">Home</a></li> 
<li><a href="#" data-icon="grid" class="two">Second page</a></li> 
<li><a href="#" data-icon="star" class="three">Third page</a></li> 

CSS:

.home.ui-btn-active { 
    background:red !important; 
} 
.two.ui-btn-active { 
    background:green !important; 
} 
.three.ui-btn-active { 
    background:blue !important; 
} 

更新小提琴:

http://jsfiddle.net/8pwFK/3/

+0

謝謝..關於圖標?我正在使用自定義圖標,我需要在選定狀態下顯示圖標。 – user700284

+0

只需將圖標引用添加到上面樣式@Guest顯示中(線索:'attr(data-icon)')。 – Ben