Polymer 2.0 - 尝试实现HTML5拖放

时间:2017-07-03 18:46:52

标签: polymer polymer-2.x

我正在尝试为Polymer2.0组件实现HTML5拖放,类似于https://jsfiddle.net/Nagasai_Aytha/b62to481/中的拖放选项

HTML:

<base href="https://raw-dot-custom-elements.appspot.com/PolymerElements/iron-flex-layout/v2.0.0/iron-flex-layout/">
<script src="../webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="iron-flex-layout-classes.html">
<dom-module id="demo-element">
  <template>
    <style include="shared-styles">
      :host {
        display: block;

        padding: 10px;
      }

     .card {
        margin: 24px;
        padding: 16px;
        color: #757575;
        border-radius: 5px;
        background-color: #fff;
        box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
      }
    </style>


    <div  draggable="true" ondragstart="{{drag(event)}}" >
    Input:
    <input type="text"/>
    </div>
              <div class="card" id="div1" ondrop="{{drop(event)}}" ondragover="{{allowDrop(event)}}"></div>
  </template>

  <script>
    Polymer({
      is: 'demo-element',
      allowDrop:function(ev){
            ev.preventDefault();
        },
      drag: function(ev){
          ev.dataTransfer.setData("text",ev.target.id);
      },
      drop:function(ev){
          ev.preventDefault();
          var data=ev.dataTransfer.getData("text");
          console.log(ev)
          ev.target.appendChild(document.getElementById(data));
      }
    });

  </script>
  <script>Polymer({is: "demo-element"});</script>
</dom-module>
<demo-element></demo-element>

JSFiddle供参考: enter image description here

2 个答案:

答案 0 :(得分:0)

这里的关键是对原生HTML5拖放事件使用on-<event>表示法。此外,您不需要事件的数据绑定表示法。

这是您的更正标记。

HTML:

    <base href="https://raw-dot-custom-elements.appspot.com/PolymerElements/iron-flex-layout/v2.0.0/iron-flex-layout/">
    <script src="../webcomponentsjs/webcomponents-lite.min.js"></script>
    <link rel="import" href="iron-flex-layout-classes.html">
    <dom-module id="demo-element">
      <template>
        <style include="shared-styles">
          :host {
            display: block;

            padding: 10px;
          }

         .card {
            margin: 24px;
            padding: 16px;
            color: #757575;
            border-radius: 5px;
            background-color: #fff;
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
          }
        </style>


        <div id="draggedDiv" draggable="true" on-dragstart="drag" >
        Input:
        <input type="text"/>
        </div>
                  <div class="card" id="div1" on-drop="drop" on-dragover="allowDrop"></div>
      </template>

      <script>
        Polymer({
          is: 'demo-element',
          allowDrop:function(ev){
                ev.preventDefault();
            },
          drag: function(ev){
              ev.dataTransfer.setData("text",ev.target.id);
          },
          drop:function(ev){
              ev.preventDefault();
              var data=ev.dataTransfer.getData("text");
              console.log(ev)
              ev.target.appendChild(this.shadowRoot.querySelector('#' + data));
          }
        });

      </script>
      <script>Polymer({is: "demo-element"});</script>
    </dom-module>
    <demo-element></demo-element>

答案 1 :(得分:0)

对于Polymer 2.0,将this.$$更改为this.shadowRoot.querySelector

  drop (ev){
      ev.preventDefault();
      var data=ev.dataTransfer.getData("text");
      ev.target.appendChild(this.shadowRoot.querySelector('#' + data));
  }