根據網頁解析度不同調用不同的css檔案

來源:互聯網
上載者:User
css|解析度|網頁

<SCRIPT LANGUAGE="JavaScript">
<!--
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{
var IE1024="";
var IE800="";
var IE1152="";
var IEother="";

ScreenWidth(IE1024,IE800,IE1152,IEother)
}else{
if (window.navigator.userAgent.indexOf("Firefox")>=1)
{
file://如果瀏覽器為Firefox
var Firefox1024="";
var Firefox800="";
var Firefox1152="";
var Firefoxother="";

ScreenWidth(Firefox1024,Firefox800,Firefox1152,Firefoxother)
}else{
file://如果瀏覽器為其他
var Other1024="";
var Other800="";
var Other1152="";
var Otherother="";
ScreenWidth(Other1024,Other800,Other1152,Otherother)
}
}

function ScreenWidth(CSS1,CSS2,CSS3,CSS4){
if ((screen.width == 1024) && (screen.height == 768)){
setActiveStyleSheet(CSS1);
}else{
if ((screen.width == 800) && (screen.height == 600)){
setActiveStyleSheet(CSS2);
}else{
if ((screen.width == 1152) && (screen.height == 864)){
setActiveStyleSheet(CSS3);
}else{
setActiveStyleSheet(CSS4);
}}}
}

function setActiveStyleSheet(title){
document.getElementsByTagName("link")[0].href="style/"+title;
}
file://-->
</SCRIPT>

解釋:

var IE1024="";
var IE800="";
var IE1152="";
var IEother="";

引號裡面分別填寫,使用者使用IE的時候並且解析度為1024*768,800*600,1152*864要使用的css檔案名稱.

var Firefox1024="";
var Firefox800="";
var Firefox1152="";
var Firefoxother="";

  引號裡面分別填寫,使用者使用FF的時候並且解析度為1024*768,800*600,1152*864要使用的css檔案名稱.

var Other1024="";
var Other800="";
var Other1152="";
var Otherother="";

  引號裡面分別填寫,使用者使用其他瀏覽器的時候並且解析度為1024*768,800*600,1152*864要使用的css檔案名稱.

  不判斷解析度,只判斷瀏覽器

  應E.Qiang提議,編如下代碼。實現根據瀏覽器類型自動調用不同CSS。

  代碼:

<SCRIPT LANGUAGE="JavaScript">
<!--
if (window.navigator.userAgent.indexOf("MSIE")>=1)
{
file://如果瀏覽器為IE
setActiveStyleSheet("default.css");
}else{
if (window.navigator.userAgent.indexOf("Firefox")>=1)
{
file://如果瀏覽器為Firefox
setActiveStyleSheet("default2.css");
}else{
file://如果瀏覽器為其他
setActiveStyleSheet("newsky.css");
}
}

function setActiveStyleSheet(title){
document.getElementsByTagName("link")[0].href="style/"+title;
}
file://-->
</SCRIPT>

  解釋:

  如果瀏覽器為IE,則調用default.css

  如果瀏覽器為Firefox,則調用default2.css

  如果瀏覽器為其他,則調用newsky.css

  用法:放在</head>前面即可。



相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.