2010-07-01 95 views
2

我包括一個備用樣式表爲iPhone/iTouch設備。起初,我想簡單地使用handheld媒體類型:如何讓桌面瀏覽器忽略iPhone css3媒體查詢

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type='text/xsl' href='/css/homepage_handheld.xsl' media='handheld'?> 
<?xml-stylesheet type='text/xsl' href='/css/homepage.xsl' media='all'?> 

我添加到正是如此DOM文檔:

//Add handheld stylesheet 
ProcessingInstruction pi = doc.CreateProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/css/homepage_handheld.xsl' media='handheld'"); 
doc.InsertBefore(pi, doc.DocumentElement); 

//Default css, for everyone else 
pi = doc.CreateProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/css/homepage.xsl' media='all'"); 
doc.InsertBefore(pi, doc.DocumentElement); 

不幸的是,iPhone的的Safari的瀏覽器不支持handheld媒體類型。蘋果提出的解決方法是使用一個CSS3媒體查詢,免去handheld

media='handheld' 

與檢測iPhone- 設備的查詢:

media="only screen and (max-device-width: 480px)" 

注: 480像素的翻轉iPhone的寬度(即風景)

所以我包括這種媒體類型在我的樣式錶鏈接:

//Add handheld stylesheet 
ProcessingInstruction pi = doc.CreateProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/css/homepage_handheld.xsl' media='handheld'"); 
doc.InsertBefore(pi, doc.DocumentElement); 

//BUG: iPhone doesn't support handheld media type. Use the CSS3 media query hack 
pi = doc.CreateProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/css/homepage_handheld.xsl' media='only screen and (max-device-width: 480px)'"); 
doc.InsertBefore(pi, doc.DocumentElement); 

//Default css, for everyone else 
pi = doc.CreateProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/css/homepage.xsl' media='all'"); 
doc.InsertBefore(pi, doc.DocumentElement); 

這給XML:

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type='text/xsl' href='/css/homepage_handheld.xsl' media='handheld'?> 
<?xml-stylesheet type='text/xsl' href='/css/homepage_handheld.xsl' media='only screen and (max-device-width: 480px)'?> 
<?xml-stylesheet type='text/xsl' href='/css/homepage.xsl' media='all'?> 

和所有似乎是好的,但現在瀏覽器(IE8,Chrome5)開始使用 「手持式」 XSL。

「瀏覽器」。

注:火狐(3.5)不支持渲染xml內容與 xml-stylesheet可言。這是什麼原因我 不包括它在 「瀏覽器」列表中。


我已經試過移動 「所有」 樣式的iPhone專用媒體查詢以上節點:

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type='text/xsl' href='/css/homepage_handheld.xsl' media='handheld'?> 
<?xml-stylesheet type='text/xsl' href='/css/homepage.xsl' media='all'?> 
<?xml-stylesheet type='text/xsl' href='/css/homepage_handheld.xsl' media='only screen and (max-device-width: 480px)'?> 

繼續使用only screen and (max-device-wisth: 480px)媒體類型。


怎樣纔可以有一個樣式表申請手持,另一個申請「其他人」

回答

0

與XML實現不太相似,但這裏有一篇很好的文章,我只是用媒體屬性(忽略了IE用戶)來閱讀,或者改爲使用javascript。

http://css-tricks.com/resolution-specific-stylesheets/

這樣做的缺點是,你的廣告的ID爲<link>是這樣的:
<link rel="stylesheet" type="text/css" href="main.css" /> <link id="size-stylesheet" rel="stylesheet" type="text/css" href="narrow.css" />

然後用js判斷瀏覽器窗口的大小,並根據哪些參數要目標,您可以使用ID作爲引擎來用適當的樣式表替換href值。

0

下面是解決這個問題是有趣的對這個問題的幾個項目:

  • 320 and up

    「320及以上」防止移動設備 從 下載桌面資產使用小屏幕的樣式表作爲 的起點。

  • Lessframework

    少框架是設計自適應網站的CSS網格系統 。它 包含4個佈局和3套 排版預設,全部基於 單個網格。