聚合物 - show/hide div by paper-checkbox

Polymer - show/hide div by paper-checkbox

来自 Meteor 背景,我会使用 JQuery 到 show/hide 和 div,纸质复选框如下:

HTML:

<paper-checkbox name="remoteLocation" id="remote-chk" checked>Remote Location</paper-checkbox>
<div id="autoUpdate" class="autoUpdate" style="display: none;">content</div>

脚本:

Template.<templateName>.events({
  'change #remote-chk' : function(e){
      if (e.target.checked) {
        $('#autoUpdate').fadeOut('slow');
      } else {
        $('#autoUpdate').fadeIn('slow');
      }
    }
)};

现在,在 Polymer 1.0 中,我试图实现同样的事情:

<link rel="import" href="/bower_components/paper-checkbox/paper-checkbox.html">
<dom-module id="my-app">
 <template>
  <paper-checkbox name="remoteLocation" id="remote-chk" on-click="showMe" checked>Remote Location</paper-checkbox>
<div id="autoUpdate" class="autoUpdate" style="display: none;">content</div>
 </template>
 <script>
  Polymer({
   is: "my-app",
   showMe: function () {
           if (e.target.checked) {
             $('#autoUpdate').fadeOut('slow');
           } else {
             $('#autoUpdate').fadeIn('slow');
           }

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

谁能多看一眼,因为什么都不管用?谢谢。

我相信褪色过渡在聚合物实验室中仍处于实验阶段(我可能错了)但是对于简单的 hide/show 内容来说,一个好的方法可以是:

在你index.html

<my-app></my-app>

您在示例中将该组件命名为 my-app

在你的我的-app.html

<link rel="import" href="../bower_components/paper-checkbox/paper-checkbox.html">
<dom-module id="my-app">
    <template>
        <paper-checkbox name="remoteLocation" id="remote-chk" on-click="showMe">Remote Location</paper-checkbox>
        <div id="autoUpdate" class="autoUpdate" hidden$="{{hide}}">content</div>
    </template>
    <script>
        Polymer({
            is: "my-app",
            properties: {
                hide: {
                    type: Boolean,
                    value: true // init the value to true so it will be hidden on page load
                }
            },
            showMe: function() {
                this.hide = !this.hide

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

使用隐藏的数据绑定助手

https://www.polymer-project.org/1.0/docs/devguide/templates.html#dom-if

您可以将 属性 隐藏设置​​为 true 并标识要隐藏的 div 使用

hidden$="{{hide}}"

函数 showMe 然后将布尔值反转为 true/false 并且由于双向数据绑定,内容将显示

对于淡入淡出,您甚至可以使用 animate.css 并使用语法

class$="{{your-class}}"

您也可以声明性地删除 hidden 属性,然后命令式地执行所有操作。

像这样:

<div id="autoUpdate" class="autoUpdate">content</div>
...
if (e.target.checked) {
  this.$.autoUpdate.hidden = true;
} else {
  this.$.autoUpdate.hidden = false;
}

好吧,这个答案已经很晚了none我认为在 Polymer 中隐藏和显示元素应该像这样完成。

指定为 dom-if 的模板。如果 属性 sendInProgress 为 false,将显示其中的元素。

<template is="dom-if" if="{{!sendInProgress}}">
          <paper-input id="replyInputField"></paper-input>
</template>
<paper-button class="reply-button" on-click="_handleReply">Send</paper-button>


Polymer({
  is: 'hide-elements',
  properties: {
  sendInProgress: {value: false, notify: true}
  },
  _handleReply: function() {

    if (this.sendInProgress){
      //Will display element #replyInputField
      this.set('sendInProgress', false);
    } else {
      //Will hide element #replyInputField
      this.set('sendInProgress', true);
    }
  }


});