简体   繁体   English

JavaScript与媒体查询

[英]javascript vs media queries

I'm looking at creating a responsive framework for my website - its a first for me. 我正在为我的网站创建一个响应框架-这对我来说是第一个。 No fancy stuff just responsive through mobile -> tablet -> desktop. 没有花哨的东西只是通过移动->平板电脑->桌面做出响应。

I'm happy with how media queries work, however adding classes through javascript seems like another viable option. 我对媒体查询的工作方式感到满意,但是通过javascript添加类似乎是另一个可行的选择。

It would be easy to do something like this: 做这样的事情很容易:

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);
}

However, I'm not an expert in this area - am I missing something or is it really that simple? 但是,我不是该领域的专家-我错过了什么吗?真的那么简单吗?

All advice appreciated. 所有建议表示赞赏。

you can use this minified jQuery snippet to detect if your user is viewing using a mobile device. 您可以使用这个缩小的jQuery代码段来检测您的用户是否正在使用移动设备进行查看。 If you need to test for a specific device I've included a collection of JavaScript snippets below which can be used to detect various mobile handheld devices such as iPad, iPhone, iPod, iDevice, Andriod, Blackberry, WebOs and Windows Phone. 如果您需要测试特定的设备,我在下面提供了一系列JavaScript代码段,这些代码段可用于检测各种移动手持设备,例如iPad,iPhone,iPod,iDevice,Andriod,Blackberry,WebO和Windows Phone。

if(jQuery.browser.mobile)
{
   console.log("You are using a mobile device!");
}
else
{
   console.log("You are not using a mobile device!");
}

See more DETECT MOBILE DEVICES USING JQUERY 查看更多使用JQUERY检测移动设备

See the link below to understand the difference 请参阅下面的链接以了解区别

What is better: CSS media queries or JQuery mobile? 哪个更好:CSS媒体查询或JQuery mobile?

I would suggest media queries, as all future amends can be done in the CSS without adding more and more logic to a separate JS file for new breakpoints. 我建议媒体查询,因为所有将来的修改都可以在CSS中完成,而不必为新的断点在单独的JS文件中添加越来越多的逻辑。

Additionally, the CSS solution is supported down to IE9+ and there are JS polyfills ( Respond ) for backwards compatibility. 此外,CSS解决方案一直支持到IE9 +,并且具有JS polyfills( Respond )以实现向后兼容性。 Basically it's just built in to CSS and works well. 基本上,它只是内置在CSS中并且运行良好。 There seems little point of rewriting the same logic in javascript, having a new class for every new size. 似乎没有必要用javascript重写相同的逻辑,每个新的大小都有一个新的类。

On top of this, media queries allow you to target CSS as different media types such as print, or if you want to use height-based media queries or target retina displays you can do this without having to add new classes. 最重要的是,媒体查询使您可以将CSS定位为不同的媒体类型,例如打印,或者,如果您要使用基于高度的媒体查询或目标视网膜显示,则无需添加新类就可以做到这一点。 As a rule the convention is to use media queries with JS fallbacks and I see no reason to suggest otherwise. 通常,惯例是将媒体查询与JS备用广告一起使用,我认为没有其他建议。

JS produces different results for detecting viewport heights and widths depending on how you get them: JS根据获取视口高度和宽度的方式产生不同的结果:

In that case, you could get screen width using window.outerWidth , window.innerWidth , document.documentElement.clientWidth . 在这种情况下,您可以使用window.outerWidthwindow.innerWidthdocument.documentElement.clientWidth获得屏幕宽度。 All these produce different results, but the second will give you values identical to CSS @media breakpoint. 所有这些都会产生不同的结果,但是第二个将为您提供与CSS @media断点相同的值。

$(window).width() too, is different from @media breakpoint. $(window).width()也与@media断点不同。

I depends on browser differences, eg if the take in account the vertical scrollbar or not. 我取决于浏览器的差异,例如是否考虑垂直滚动条。 Better go with CSS . 最好使用CSS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM