JS judges different resolutions and calls different CSS style file Implementation ideas and test code

Source: Internet
Author: User

I recently read a website and found that different resolutions of the display and style files are called differently. Today I wrote an example to study it, Copy codeThe Code is as follows: <! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> untitled document </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) // obtain the screen width.
{
SC. setAttribute ("href", "css/c2.css"); // you can specify the path for css to introduce a style sheet.
D. innerHTML = "the screen width of your computer is greater than 1024. My width is pixel PX, and the background color is red now. ";
}
Else {
SC. setAttribute ("href", "css/c1.css ");
D. innerHTML = "your computer's screen width is less than or equal to 1024. My width is 960px, and the background color is blue now. ";
}
}
</Script>
</Head>
<Body>
<Div id = "d"> </div>
</Body>
</Html>

Content in c1.css Copy codeThe Code is as follows: * {margin: 0; padding: 0 ;}
Div {width: 960px; height: 400px; margin: 0 auto; background: blue; color: # ffffff ;}

Content in c2.css Copy codeThe Code is as follows: * {margin: 0; padding: 0 ;}
Div {width: pixel PX; height: 400px; margin: 0 auto; background: red; color: # fff ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.