【发布时间】:2014-11-09 01:38:02
【问题描述】:
我正在考虑为我的网站创建一个响应式框架——这对我来说是第一次。没有花哨的东西,只是通过手机 -> 平板电脑 -> 桌面做出响应。
我对媒体查询的工作方式很满意,但是通过 javascript 添加类似乎是另一个可行的选择。
这样做很容易:
function queryViewport() {
var $window = $(window);
var $body = $("body");
var windowWidth = $window.width();
var windowHeight = $window.height();
// Query window sizes to determine whether device is to be
// classified as a small mobile device or a larger tablet/
// desktop device.
var isMobile = windowWidth < 600 || windowHeight < 600;
$body.toggleClass("mobile", isMobile).toggleClass("desktop", !isMobile);
// Calculate whether viewport is portrait or landscape
var isPortrait = windowHeight > windowWidth;
$body.toggleClass("portrait", isPortrait).toggleClass("landscape", !isPortrait);
}
但是,我不是这方面的专家 - 我错过了什么还是真的那么简单?
感谢所有建议。
【问题讨论】:
-
这种方法可行。我喜欢媒体查询保留在 CSS 中,其余的格式都在 CSS 中发生。
-
从来没有那么简单,因为那里有过多的宽度(和设备宽度,注意区别)。我总是建议mobiledetect.net。
标签: javascript responsive-design media-queries