我会这样做的两种方法:
1) 读取窗口宽度并更改您的 css 的自定义 javascript
2) 或者使用像 adapt.js 这样的库来检测和交换 .css
关键词:响应式网页设计
看看adapt.js ,它是一个 javascript 库,可以根据页面窗口大小加载不同的 css 文件。如果您想在移动设备和桌面屏幕之间显示不同的内容,这种技术非常有用。
在您的情况下,这也可能有效。
1)参考adapter.js
<script src="/js/adapt.js" type="text/javascript></script>
2) 配置
所以基本上你会有两种观点:
// Edit to suit your needs.
var ADAPT_CONFIG = {
// Where is your CSS?
path: 'assets/css/',
// false = Only run once, when page first loads.
// true = Change on window resize and page tilt.
dynamic: true,
// Optional callback... myCallback(i, width)
callback: myCallback,
// First range entry is the minimum.
// Last range entry is the maximum.
// Separate ranges by "to" keyword.
range: [
'0px to 760px = Narrow.css', // css for narrow views
'760px to 980px = Narrow.css',
'980px to 1280px = Wide.css', // css for wide views
'1280px to 1600px = Wide.css',
'1600px to 1920px = Wide.css',
'1940px to 2540px = Wide.css',
'2540px = Wide.css'
]
};
3)总共看起来像
<html>
<head>
...
<script src="/js/adapt.js" type="text/javascript></script>
<script type="text/javascript>
// Edit to suit your needs.
var ADAPT_CONFIG = {
// Where is your CSS?
path: 'assets/css/',
// false = Only run once, when page first loads.
// true = Change on window resize and page tilt.
dynamic: true,
// Optional callback... myCallback(i, width)
callback: myCallback,
// First range entry is the minimum.
// Last range entry is the maximum.
// Separate ranges by "to" keyword.
range: [
'0px to 760px = Narrow.css', // css for narrow views
'760px to 980px = Narrow.css',
'980px to 1280px = Wide.css', // css for wide views
'1280px to 1600px = Wide.css',
'1600px to 1920px = Wide.css',
'1940px to 2540px = Wide.css',
'2540px = Wide.css'
]
};
</script>
...
</head>
<body>
...
</body>
</html>