JS判斷不同解析度調用不同的CSS樣式檔案實現思路及測試代碼

來源:互聯網
上載者:User

最近看一個網站,發現顯示器不同的解析度,樣式檔案調用的也不一樣,今天寫了一個例子研究一下, 複製代碼 代碼如下:<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>無標題文檔</title>
<link rel="stylesheet" id="sc" type="text/css" href="css/c1.css"/>
<script type="text/javascript">
window.onload=function(){
var sc=document.getElementById("sc");
var d=document.getElementById("d");
if(screen.width>1024) //擷取螢幕的的寬度
{
sc.setAttribute("href","css/c2.css"); //設定css引入樣式表的路徑
d.innerHTML = "你的電腦螢幕寬度大於1024,我的寬度是 1200px, 背景色現在是紅色。";
}
else{
sc.setAttribute("href","css/c1.css");
d.innerHTML = "你的電腦螢幕寬度小於或是等於1024,我的寬度是 960px, 背景色現在是藍色。";
}
}
</script>
</head>
<body>
<div id="d"></div>
</body>
</html>

c1.css裡面的內容 複製代碼 代碼如下:*{ margin:0; padding:0;}
div{ width:960px; height:400px; margin:0 auto; background:blue; color:#ffffff;}

c2.css裡面的內容 複製代碼 代碼如下:*{ margin:0; padding:0;}
div{ width:1200px; height:400px; margin:0 auto; background:red; color:#fff;}

相關文章

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.