我正在考虑为我的网站创建一个响应式框架——这对我来说是第一次。没有花哨的东西,只需要在移动端、平板电脑和桌面上实现响应。
我很满意媒体查询的工作方式,但是通过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);
}
然而,我不是这个领域的专家 - 我是否遗漏了什么或者真的如此简单?欢迎任何建议。