简体   繁体   English

如何扩展媒体查询?

[英]How can I scale media queries?

Ahoy... 嗨...

I'm doing an Ember application, and I need to be able to scale the layout, as it sometimes needs to be displayed on large panels, where I can't get the pixel ratio. 我正在做一个Ember应用程序,我需要能够缩放布局,因为有时需要在无法获得像素比率的大面板上显示它。 (oh, and all the way down to iPhone screens as well...) (呵呵一路下跌到iPhone的屏幕,以及...)

So, here's my problem: I've made the whole thing totally scalable and dandy, but I do also need to use a bunch of media queries here and there. 所以,这是我的问题:我已经使整个事情变得完全可扩展且花哨,但我确实需要在这里和那里使用大量媒体查询。

Now, while I can scale the application (fonts etc.) up and down with my JS skillz, the general layout remains the same, as the media queries are unchanged. 现在,尽管我可以使用JS Skillz上下缩放应用程序(字体等),但总体布局保持不变,因为媒体查询不变。 This obviously makes everything look somewhat distorted. 显然,这使所有内容看起来都有些失真。

What I need is basically to be able to apply a scaling factor to the limits in my media queries. 我基本上需要的是能够对媒体查询中的限制应用比例因子。

I realise that a workaround would be to change the meta tag for the screen width, but that's a no-go, as we need device pixel = actual pixel for the cleanest rendering (many Androids look fuzzy with pixel ratio eg 1.75). 我意识到一种解决方法是更改​​屏幕宽度的meta标签,但这是不行的,因为我们需要设备pixel =实际像素才能进行最清晰的渲染(许多Android的像素比看起来很模糊,例如1.75)。

I've looked at using calc(..) in the media queries (doesn't work). 我看过在媒体查询中使用calc(..)(不起作用)。 As I'm using Ember, I have access to all the templates and stuff. 在使用Ember时,我可以访问所有模板和内容。 I'm thinking along the lines of calculating it all in JS and shoving it into the DOM in a STYLE tag, but with ~100 media queries, I imagine it will get painful... And keep me from lager in the weekend... So here's me drafting... No pun intended... 我正在考虑在JS中将其全部计算出来并以STYLE标签将其推入DOM的思路,但是通过大约100个媒体查询,我想它会变得很痛苦...并且在周末避免我进来。所以这是我起草...没有双关语...

EDIT: 编辑:

Seems I haven't stressed a key issue enough: The MQs need to be changeable client-side. 似乎我没有足够强调一个关键问题:MQ必须在客户端可更改。 As mentioned, the application needs to run on wall-mount TV screens of arbitrary size, so while they probably all have HD or HD-ready resolution, the PPI varies a lot, and the only way to obtain the appropriate scale of the design, is to fire the thing up and scale it to fit in the app config. 如前所述,该应用程序需要在任意尺寸的壁挂电视屏幕上运行,因此尽管它们可能都具有高清或支持高清的分辨率,但PPI却相差很大,这是获得适当设计尺寸的唯一方法,是将事物放火并缩放以适合应用程序配置。 Just going with plain ol' MQs won't cut it. 只使用普通的MQ并不会减少它。

Following the positive feedback from my comment, I've created this as an answer to help people viewing this question down the line. 收到我的评论的积极反馈后,我创建了此答案,以帮助人们从头到尾查看此问题。

When creating an application or website which needs to scale from small devices up to large screens, it is always best to use easily scalable units of measurement. 创建需要从小型设备扩展到大屏幕的应用程序或网站时,始终最好使用易于扩展的度量单位。 This means avoiding the use of px in your CSS. 这意味着避免在CSS中使用px

The following units should be used for the best cross-device compatibility and scalability: 以下单元应用于最佳的跨设备兼容性和可伸缩性:

%. %。 The percentage of the screen to measure. 要测量的屏幕百分比。 eg 100% is 100% of the screen, 50% is half of the screen, and so on. 例如100%是屏幕的100%, 50%是屏幕的一半,依此类推。 Fairly straight forward. 非常坦率的。

rem. REM。 The size relative to the root element em size. 相对于根元素em大小的大小。 This means that whatever the base font size is for the document, everything scales in relation to that. 这意味着无论文档的基本字体大小是多少,所有内容都相对于它进行缩放。 If the base font size is 16px and you set something to be 1.5rem, then it will be 24px; 如果基本字体大小为16px,而您将其设置为1.5rem,则它将为24px; 1.5 x 16px. 1.5 x 16像素。 This is ideal when adding accessibility and allowing users to increase the font size themselves. 当增加可访问性并允许用户自己增加字体大小时,这是理想的选择。

em (where things can get complicated). em(事情可能会变得复杂)。 This is the size relative to the direct parent container font size. 这是相对于直接父容器字体大小的大小。 It's similar to rem (see above), but will multiply the further down the structure you go. 它与rem类似(请参见上文),但是会进一步扩展您所使用的结构。

Assume the following css: 假设以下CSS:

html {
    font-size : 16px;
}

div {
    font-size : 1.5em;
}

When we couple this with nested divs, things can get frustrating: 当我们将其与嵌套div结合使用时,事情可能会令人沮丧:

<div>
    <!-- font-size is 24px !-->
    <div>
        <!-- font-size is 36px !-->
        <div>
            <!-- font-size is 54px !-->
        </div>
    </div>
</div>

Use em 's carefully as you can find things scaling very rapidly when you didn't expect it. 请谨慎使用em ,因为您会发现在不期望的情况下扩展很快。

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

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