使用javascript根据分辨率更改css文件

发布于 2024-10-14 19:40:44 字数 1343 浏览 3 评论 0原文

我在网站上找到了它,但对我来说似乎不起作用。 问题是CSS没有改变或改变得很快,我能看到的只是黑色闪光和空白页面(http://gino.net16.net/inde.php,可以用firefox清楚地看到)。因为我没有javascript技能,所以我不知道出了什么问题。

主文件

<html>
<head>
      <title>my title</title>
<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
function redirectCSS() {

if ((screen.width == 640) && (screen.height == 480))
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 800) && (screen.height == 600))
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 1024) && (screen.height == 768))
      { document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}
}
// End -->
</script>      

</head>
<body onLoad="redirectCSS()">

<h1>Use the appropriate stylesheet to display this content</h1>

</body>
</div>
</html>

css样式文件

@charset "utf-8";
/* CSS Document */

body {
    background-color: #2a2725;
    font-family: Arial, Helvetica, sans-serif;
    text-shadow: 0px 0px 1px #2a2725;
    color: #fff;
}
/* }}} END TableSorter */

I found it on website but for me it seems doesn't work.
The problem is that css doesn't change or changes very fast all i can see is black flash and blank page(http://gino.net16.net/inde.php it can be clearly see with firefox).Since i have no javascript skills , so i don't know what's wrong.

Main file

<html>
<head>
      <title>my title</title>
<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
function redirectCSS() {

if ((screen.width == 640) && (screen.height == 480))
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 800) && (screen.height == 600))
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 1024) && (screen.height == 768))
      { document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}
}
// End -->
</script>      

</head>
<body onLoad="redirectCSS()">

<h1>Use the appropriate stylesheet to display this content</h1>

</body>
</div>
</html>

css style file

@charset "utf-8";
/* CSS Document */

body {
    background-color: #2a2725;
    font-family: Arial, Helvetica, sans-serif;
    text-shadow: 0px 0px 1px #2a2725;
    color: #fff;
}
/* }}} END TableSorter */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听闻余生 2024-10-21 19:40:44

尝试使用 CSS 媒体查询,它们正是您想要的:)

Try using CSS Media Queries, they do exactly what you want :)

最近可好 2024-10-21 19:40:44

您正在覆盖页面的内容。 onload 后的 document.write 覆盖整个页面。您需要在页面加载时执行标头中的 JavaScript,而不是将其放入函数中。

<script type="text/javascript">
if ((screen.width == 640) && (screen.height == 480))
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 800) && (screen.height == 600))
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 1024) && (screen.height == 768))
      { document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}
}
</script>      

更多说明:

1) 请记住为不想或不能支持 JavaScript 的用户和浏览器添加一个不使用 JavaScript 的“通用”样式表。

2) language="JavaScript" 已过时。请改用 type="text/javascript"

3) 无需“注释掉”JavaScript

4) 请记住,屏幕尺寸与视口(浏览器窗口)尺寸无关。并不是每个人都会最大化浏览器窗口。

5) 最重要的是:没有真正的理由再为此使用 JavaScript。除非你真的真的需要支持 IE,那么你可以使用 CSS3 媒体查询。请参阅此问题的示例:使用 CSS 的流体布局

You are overwriting the content of the page. document.write after onload overwrites the whole page. Instead of putting it in a function, you need to execute the JavaScript in the header while the page is loading.

<script type="text/javascript">
if ((screen.width == 640) && (screen.height == 480))
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 800) && (screen.height == 600))
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 1024) && (screen.height == 768))
      { document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}

else
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}
}
</script>      

Some more remarks:

1) Remember to incude a "general" stylesheet without using JavaScript, for the users and browsers who don't want to or can't support JavaScript.

2) language="JavaScript" is out-of-date. Use type="text/javascript" instead.

3) It's no need to "comment out" the JavaScript

4) Keep in mind that the screen size has nothing to do with the view port (browser window) size. No everyone maximizes their browser window.

5) And most importantly: There is no real reason to use JavaScript for this anymore. Unless you really, really, really need to support IE, then you can use CSS3 Media Queries. See this question for an example: Fluid layouts with CSS

孤芳又自赏 2024-10-21 19:40:44

您可能永远不会在页面加载后执行 document.write 。
您需要像这样内联调用该函数(如果您也有不同的 css 文件,它会有所帮助),

<html>
<head>
      <title>my title</title>
<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
function redirectCSS() {

if ((screen.width == 640) && (screen.height == 480))
      {document.write("<link href='small.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 800) && (screen.height == 600))
      {document.write("<link href='medium.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 1024) && (screen.height == 768))
      { document.write("<link href='large.css' rel='stylesheet' type='text/css'>")}

else
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}
}
redirectCSS()
// End -->
</script>      

</head>
<body >

<h1>Use the appropriate stylesheet to display this content</h1>

</body>
</div>
</html>

如果我必须这样做,我个人会这样做:

<html>
<head>
      <title>my title</title>
<link href='style.css' rel='stylesheet' type='text/css' />
<script type="text/javascript"><!-- Begin
var css = "";
if (screen.width < 800) css='small.css';
else if (screen.width < 1024)  css='medium.css';
else css='large.css';
document.write("<link href='"+css+"' rel='stylesheet' type='text/css' />");
// End -->
</script>      

</head>
<body >

<h1>Use the appropriate stylesheet to display this content</h1>

</body>
</div>
</html>

You may NEVER do a document.write after page load.
You need to call the function inline like this (and it helps if you have different css files too)

<html>
<head>
      <title>my title</title>
<SCRIPT LANGUAGE="JavaScript">


<!-- Begin
function redirectCSS() {

if ((screen.width == 640) && (screen.height == 480))
      {document.write("<link href='small.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 800) && (screen.height == 600))
      {document.write("<link href='medium.css' rel='stylesheet' type='text/css'>")}

else if ((screen.width == 1024) && (screen.height == 768))
      { document.write("<link href='large.css' rel='stylesheet' type='text/css'>")}

else
      {document.write("<link href='style.css' rel='stylesheet' type='text/css'>")}
}
redirectCSS()
// End -->
</script>      

</head>
<body >

<h1>Use the appropriate stylesheet to display this content</h1>

</body>
</div>
</html>

I would personally do this if I had to do it:

<html>
<head>
      <title>my title</title>
<link href='style.css' rel='stylesheet' type='text/css' />
<script type="text/javascript"><!-- Begin
var css = "";
if (screen.width < 800) css='small.css';
else if (screen.width < 1024)  css='medium.css';
else css='large.css';
document.write("<link href='"+css+"' rel='stylesheet' type='text/css' />");
// End -->
</script>      

</head>
<body >

<h1>Use the appropriate stylesheet to display this content</h1>

</body>
</div>
</html>
内心激荡 2024-10-21 19:40:44

不确定它是否能正常工作,在某些情况下,您的布局总是会出现问题。
css 的标准声明:

link rel="stylesheet" type="text/css" href="css/style.css"

link rel="stylesheet" media="screen and (max-width: 700px)" href="css/narrow.css"

link rel="stylesheet" media="screen and (min-width: 701px) and (max-width: 900px)"
        href="css/medium.css"

link rel="stylesheet" media="screen and (min-width: 901px)" href="css/wide.css"

即使您调整视图大小,浏览器也会更改视图。

Not sure that it works well, in some cases you always will have a problem with your layouts.
Standard declaration for css:

link rel="stylesheet" type="text/css" href="css/style.css"

link rel="stylesheet" media="screen and (max-width: 700px)" href="css/narrow.css"

link rel="stylesheet" media="screen and (min-width: 701px) and (max-width: 900px)"
        href="css/medium.css"

link rel="stylesheet" media="screen and (min-width: 901px)" href="css/wide.css"

Browser will change a view even when you resize it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文