不同浏览器尺寸的不同CSS
我有一个关于 windowdependent CSS 的问题。这是一个非常常见的问题,但不知怎的,它对我来说不起作用,因为Wordpress。不管怎样,目前我正在使用 Nettuts 建议的脚本。我只是想当用户调整窗口大小时针对不同的 CSS 样式。我知道我需要为此使用 $(window).width 。我现在所知道的是:
function checkWindowSize() {
if ( $(window).width() < 800 ) {
$('body').addClass('small');
}
else {
$('body').removeClass('small');
}
}
$(window).resize(checkWindowSize);
它确实能完成这项工作。但是..有一个小故障。当我调整窗口大小时,它确实针对另一种 css 样式。但是,当我将窗口的尺寸设置为小于 800 时,按 F5(刷新),我需要在目标 css 发生之前手动调整窗口大小(1 px)。当我稍微调整屏幕大小时,它不会自动定位它。我调整了脚本的位置,比如将新的 CSS 放在实际的 CSS 之前,但这效果不太好。
有什么想法吗?如果我还不够清楚,请告诉我:)
I've got a question about windowdependant CSS'es. It's a very common question, but somehow it's not working for me, with Wordpress. Anyway, at the moment I'm using the script adviced by Nettuts. I just want to target different CSS styles when the user resizes his window. I know I need to use $(window).width for that. What I have now is:
function checkWindowSize() {
if ( $(window).width() < 800 ) {
$('body').addClass('small');
}
else {
$('body').removeClass('small');
}
}
$(window).resize(checkWindowSize);
It certainly does the job. But.. There's a little glitch. When I resize the window, it does target another css-style. But when I leave the window at a smaller size than 800, push F5 (refresh), I need to resize the window manually (for 1 px) before the targeted css takes place. It doesnt automatically target it, just when I resize the screen for a small bit.. I played with the position of the scripts, like putting the new CSS before the actual CSS, but that didn't do so well.
Any ideas? If I'm not clear enough, just tell me :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否考虑过使用媒体查询?
http://www.alistapart.com/articles/responsive-web-design/
Have you considered using media queries?
http://www.alistapart.com/articles/responsive-web-design/
您可以使用 Adapt.js 来实现此目的。 Adapt 根据宽度确定加载哪个 CSS 文件,并在 resize-event 或由平板电脑或手机倾斜触发时切换 CSS 文件。
您还可以将媒体查询与polyfill结合使用用于旧版浏览器支持而不是 Adapt.js。
You can use Adapt.js for this. Adapt determines which CSS file to load depending on the width and will switch the CSS-file on resize-event or triggered by tablet or phone tilt.
Also you can use Media Queries with a polyfill for older browser support instead of Adapt.js.
只需在窗口开始加载后调用它即可:
Just call it once the window loads at the start as well:
您仅在窗口大小调整时调用
checkWindowSize
方法。当您刷新页面时,不会发生大小调整,因此您永远不会检查大小,也永远不会添加类。因此,您还需要在页面加载时调用
checkWindowSize
。除了您已有的代码之外,添加以下内容:You're only calling your
checkWindowSize
method when the window resizes. When you refresh the page, no resizing occurs, so you never check the size and never add the class.So you also need to call
checkWindowSize
when the page loads. Add this, in addition to the code you already have:如上所述,媒体查询是最佳选择。您可能见过这样的 HTML:
W3C 引入了 Mediaqueries 来进一步指定媒体应该使用哪些样式表,这避免了浏览器嗅探的缺点,并且使其无需 JavaScript 也能工作。
As mentioned above, mediaqueries are the way togo. You've brobably seen HTML like this:
The W3C introduced Mediaqueries to further specify the media on which stylesheets should be used, this circumvents the disadvantages of browser sniffing, and makes it also work without javascript.