我正在使用Polymer Web組件,並且我發現core-pages
元素的默認高度爲0.我希望它取決於其子元素的高度,理想情況下是所選子元素的高度。<core-pages>如何才能從選定的孩子中獲得高度?
以下是一個演示問題(live jsbin)的示例頁面:
<!DOCTYPE html>
<html>
<head>
<script src="https://www.polymer-project.org/components/webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="https://www.polymer-project.org/components/core-pages/core-pages.html">
</head>
<body>
<h1>Before <core-pages> element</h1>
<core-pages selected="0" block>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.</div>
</core-pages>
<h1>After <core-pages> element</h1>
<script>
// Uncomment this kludge to fix the layout.
/* window.addEventListener('polymer-ready', function() {
var selected = document.querySelector('core-selected');
var height = window.getComputedStyle(selected).height;
document.querySelector('core-pages').setAttribute('style', 'height:' + height);
}); */
</script>
</body>
</html>
在我的桌面版Chrome 39,後頭部重疊core-pages
內容(而不是出現在它下面),因爲core-pages
元素被賦予了0的高度(我相信這是因爲core-pages
實現對子元素使用絕對定位,但我可能是錯的)。
這可以通過給core-pages
元素指定一個明確的高度(例如在示例中使用註釋的javascript)來「固定」,但這是不受歡迎的,真的應該在迴流時觸發。這是醜陋的,並不符合聚合物的聲明強調。
是否有任何優雅的方式適合core-pages
到它的內容,因此它以直觀的方式奠定了?我希望有一些神奇的CSS或Polymer設置可以解決這個問題。
你有沒有找到解決方案 – aloo
不,沒有任何非克魯格方式,不涉及試圖捕捉所有可能影響佈局的事件並執行JavaScript來糾正大小。 – rhashimoto