gpt4 book ai didi

javascript - 在页面调整大小时更改 CSS

转载 作者:行者123 更新时间:2023-11-30 07:43:33 25 4
gpt4 key购买 nike

我把这段代码放在一起......

<title>Style Change on Resize</title>
<script type="text/javascript">

function resize() {

if (screen.width <= 640)

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

else

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

</script>
</head>

<body onResize="resize();">

<div id="styleMe">This is a test of a resize javascirpt that will change its CSS sheet
when the browser window is resized, or when being viewed on a mobile device, such as
iOS, etc. Hello.</div>

</body>

</html>

CSS 文件中只有这个(只是背景颜色不同,所以我可以看看它是否改变了 CSS 表)...

#styleMe {
background-color:#309;
height:200px;
width:100%;
}

问题是当我运行页面时,文字上没有样式,然后当我调整页面大小时它就变黑了,有什么想法吗?

谢谢

最佳答案

您可以使用 mediaqueries 执行此操作仅使用 CSS。

#styleMe {
background-color:#309;
height:200px;
width:100%;
}

@media only screen and (max-width: 640px) {

#styleMe {
background-color: magenta;
}

}

关于javascript - 在页面调整大小时更改 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11560026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com