繁体   English   中英

使用Iron-Media-Query将CSS应用于Polymer自定义元素

[英]Applying CSS to Polymer custom element with iron-media-query

我正在尝试使用Polymer API中的<iron-media-query>在自定义元素上实现简单的“ media-query”行为。

假设我有一个容器,顶部有一些文本,下面是主要内容。.我的目标是编写媒体查询,以便当元素在大屏幕上显示时(对于我的测试,刚好大于768px),我可以对元素本地DOM样式进行一些简单的边距和填充修改。

我只是无法使其工作。 我在这里完全错过了什么吗?

<link rel="import" href="../../bower_components/polymer/polymer.html"/>
<link rel="import" href="../../bower_components/iron-media-query/iron-media-query.html" />

<iron-media-query query="(max-width:768px)" query-matches="{{isMobilePhone}}"></iron-media-query>

<template is="dom-if" if="{{isMobilePhone}}">

    <style>
        #title {
            color: #000000;
            font-size: 1.8em;
        }
    </style>


</template>


<template>
    <style>
        :host {
            background-color: gray;
            flex-direction: column;
            margin: auto;
            margin-top: 40px;
            display: flex;
            width: 90%;
            flex-grow: 1;
            max-width: 1300px;
        }

        #title {
            color: #7DB8C9;
            font-size: 1.3em;
        }

        .content-wrapper {
            height: 100%;
        }
    </style>


    <p id="title">
        [[title]]
    </p>

    <div class="content-wrapper">
        <content select=".content"></content>
    </div>

</template>




<script>
    Polymer({

        is: 'content-container',
        properties: {
            title: String,
            isMobilePhone: Boolean
        },
        listeners: {
            'tap': 'spit'
        },
        spit: function() {
            console.log("BOOL: " + this.isMobilePhone);
        }

    });
</script> </dom-module>

我还尝试将整个模板(带有样式和标记)复制到“ if”模板中,然后只修改我想要的样式,但是它也不起作用。

(所有内容都在同一个文件中,即content-container.html)

实现此目的最简单的方法之一(在iron-media-query演示中使用的一种方法 )是将Polymer的带注释的属性绑定属性选择器一起使用。

一个元素模板的简单示例如下所示

<template>
<style>
  .content-wrapper ::content {
    color: blue;
  }

  .content-wrapper[mobile-layout] ::content {
    color: green;
  }
</style>

<iron-media-query query="(max-width:768px)" query-matches="{{isMobilePhone}}"></iron-media-query>
<div class="content-wrapper" mobile-layout$="[[isMobilePhone]]">
  <content></content>
</div>
</template>

这是一个摆弄它的小提琴

<dom-module> (甚至dom-if )内任何地方的<style>标记都会立即应用于元素(如本演示中所示 ),因此将<style>放入dom-if不会提供条件样式。

而且,如果使用<iron-media-query>的唯一目的是添加条件<style> ,则根本不需要该元素。 只需在CSS中正常使用媒体查询即可:

<style>
    ...

    #title {
        color: #7DB8C9;
        font-size: 1.3em;
    }

    @media (max-width:768px) {
        #title {
            color: #000000;
            font-size: 1.8em;
        }
    }
</style>

码笔

暂无
暂无

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

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