当我在瀑布式模式下点击纸张标签上方的空间时,我想触发一个主页功能。
<core-header-panel mode="waterfall-tall">
<core-toolbar class="animate" id="core_bar" on-tap={{home}}>
<paper-tabs selected="0" self-end id="paper_tabs">
<paper-tab>0</paper-tab>
<paper-tab>1</paper-tab>
<paper-tab>2</paper-tab>
<paper-tab>3</paper-tab>
</paper-tabs>
</core-toolbar>
<core-animated-pages transitions="slide-from-right" selected="{{ $.paper_tabs.selected }}" id="core_pages">
<section></section>
<section></section>
<section></section>
<section></section>
<section>My home</section>
</core-animated-pages>
</core-header-panel>
这会触发主页功能,但当我点击纸质标签时,主页功能也会被调用。单击纸张选项卡时,如何取消主页功能?
<script>
Polymer('my-pages', {
home:function(){
this.$.paper_tabs.selected=4
}
});
</script>
你可以这样做:
home: function(e) {
// make sure this tap was on the core-toolbar itself
if (e.target.localName === 'core-toolbar') {
this.$.paper_tabs.selected = 4;
}
}
然而,以这种方式引用$.paper_tabs
并不是一个好的做法。相反,决定my-pages
具有activePage
的属性,并将UI元素绑定到该属性。通过这种方式,您的逻辑和模板UI是松散耦合的。
...
<template>
...
<paper-tabs selected="{{activePage}}" self-end>
...
<core-animated-pages selected="{{activePage}}" transitions="slide-from-right">
...
</template>
<script>
Polymer({
activePage: 0,
home: function(e) {
if (e.target.localName === 'core-toolbar') {
this.activePage = 4;
}
}
});
</script>
一般来说,尽量避免在模板中使用id
。这意味着你的元素是数据驱动的,你可以在不接触脚本的情况下重新设计你的UI。