简体   繁体   English

确定所有屏幕尺寸的元素样式

[英]Determine element styles at all screen sizes

In Javascript we can use something like window.getComputedStyle(element,null).getPropertyValue(property) to retrieve a given element's style property. 在Javascript中,我们可以使用window.getComputedStyle(element,null).getPropertyValue(property)来检索给定元素的样式属性。 That being said, any property can change with responsive web design at any screen size. 话虽这么说,任何属性都可以随着任何屏幕尺寸的响应式网页设计而改变。

I'm wondering if it's possible to analyze the element's related stylesheet to determine the styles that will be applied to it at a all window sizes/breakpoints. 我想知道是否可以分析元素的相关样式表来确定将在所有窗口大小/断点处应用于它的样式。

For example, a <p> has font-size: 16px on desktop, and font-size: 18px on mobile (set by @media (max-width: 768px) { ... } . I'd like to know that the font size will be 18px on mobile without having to resize down to mobile size and sample the font size again. 例如, <p>在桌面上有font-size: 16px ,在移动设备上有font-size: 18px (由@media (max-width: 768px) { ... }设置@media (max-width: 768px) { ... } 。我想知道移动设备上的字体大小为18px,无需调整到移动设备大小并再次采样字体大小。

I suppose with some clever text processing in JS I could sort through a stylesheet for @media and see if it reflects on the element, but for larger or multiple stylesheets, inline styles, and injected styles that method would likely be impossible to get 100% accurate. 我想在JS中有一些聪明的文本处理,我可以通过@media的样式表进行排序,看看它是否反映在元素上,但对于更大或更多样式表,内联样式和注入样式,该方法可能无法获得100%准确。

Any ideas? 有任何想法吗?

Thought... 思想...

Is it possible to wrap an element in a simulated (hidden) window element and then use JS to resize it so it triggers the media queries? 是否可以将一个元素包装在一个模拟(隐藏) window元素中,然后使用JS调整它的大小,以便触发媒体查询?

Another approach... 另一种方法......

I started playing around with document.styleSheets but even that seems like a pretty impossible task to get perfect. 我开始玩document.styleSheets但即便如此,这也是一项非常不可能完成的任务。 In my example below I have wrapped some selected text in an element and then passed it to the method below (written in coffeescript). 在下面的示例中,我将一些选定的文本包装在一个元素中,然后将其传递给下面的方法(用coffeescript编写)。

analyzeSelectiontyles: (selectionElement) ->
    selectionParents = []
    while selectionElement
        selectionParents.unshift selectionElement
        selectionElement = selectionElement.parentNode

    pageStylesheets = document.styleSheets

    for stylesheet in pageStylesheets
        rules = stylesheet.cssRules
        for rule of rules
            if rules[rule]["type"] is 1
                console.log 'standard style'
            if rules[rule]["type"] is 4
                console.log 'media query'
            # If it's a standard style rule or a media query containing
            # style rules we have to check to see if the style rule applies
            # to any one of our selectionParents defined above, and in
            # Which order so as to simulate "cascading"

Something like the following might work iff all of the styles are hosted on the same origin . 如果所有样式都托管在同一个源上,则以下内容可能会起作用。 I tested it here and it couldn't get the all.css file; 我在这里测试了它,它无法获得all.css文件; I assume because it is hosted on a different cdn domain. 我假设因为它托管在不同的cdn域上。

function css(element) {
    var sheets = document.styleSheets,
    standard = [],
    mediaStyles = {};
    element.matches = element.matches || element.webkitMatchesSelector || element.mozMatchesSelector || element.msMatchesSelector || element.oMatchesSelector;

    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (element.matches(rules[r].selectorText)) {
                if (rules[r]["type"] === 4) {
                    mediaStyles[rules[r].media] = (mediaStyles[rules[r].media] || []).push(rules[r].cssText);
                } else {
                    standard.push(rules[r].cssText);
                }
            }
        }
    }

    return { 'standard': standard, 'media': mediaStyles };
}

If you would like to retrive all css rules for a specific element (especially a mediaquery), then you don't have to parse each css file. 如果您想要检索特定元素(尤其是媒体查询)的所有css规则,那么您不必解析每个css文件。 You can use window.getMatchedCSSRule . 您可以使用window.getMatchedCSSRule After that, check the returned css rules if they have a mediaquery parent rule. 之后,检查返回的css规则是否具有mediaquery父规则。

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

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