我正在尝试创建一个响应式Grid,它可以在容器调整大小时更改元素的顺序。
如果容器变大,那么如果有足够的空间,第二行中的元素只会进入行中。如果没有足够的空间,元素之间的空间会变大,直到第二行的元素可以放在第一行内。
Here is a working example how it's realized without Polymer
但问题是我是否将其嵌入到自定义的Polymer元素中。两个元素之间的空间不会改变。这个问题有没有解决办法?
这是我的Polymer-Element:
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<dom-module id="test-element">
<template>
<style>
.container {
min-height: 400px;
display: grid;
grid-template-columns: repeat(auto-fill, 60px);
justify-content: space-around;
align-items: center;
align-content: start;
}
.item {
flex: 0 0 auto;
margin: 5px;
width: 50px;
height: 50px;
background-color: green;
}
</style>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</template>
<script>
class TestElement extends Polymer.Element {
static get is() {
return 'test-element';
}
}
window.customElements.define(TestElement.is, TestElement);
</script>
</dom-module>
答案 0 :(得分:1)
也许您的问题是您没有在主机元素上设置显示样式:
自定义元素显示:默认为内联,因此设置它们的宽度或高度将不起作用。这通常会给开发人员带来惊喜,并可能导致与布局页面相关的问题。除非您更喜欢内联显示,否则应始终设置默认显示值。
来源:Custom Element Best Practices, Web Fundamentals
我使用的是聚合物3但它不应该有任何区别:
import {Element as PolymerElement} from './node_modules/@polymer/polymer/polymer-element.js';
class TestElement extends PolymerElement {
static get template() {
return `
<style>
:host {
display: block;
}
.container {
min-height: 400px;
display: grid;
grid-template-columns: repeat(auto-fill, 60px);
justify-content: space-around;
align-items: center;
align-content: start;
}
.item {
flex: 0 0 auto;
margin: 5px;
width: 50px;
height: 50px;
background-color: green;
}
</style>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
`;
}
}
customElements.define('test-element', TestElement);
然后&#34;运行它&#34;:
<html>
<head>
<script type="module" src="./test-element.js"></script>
</head>
<body>
<test-element id="test"></test-element>
</body>
</html>
这就是我所看到的: