簡體   English   中英

在聚合物2.0元素的陰影dom中的javascript腳本標簽中找不到樣式(變形縮放)的div

[英]Can't find div for styling (tranform zoom) in javascript script tag in shadow dom of polymer 2.0 element

我正在創建一個聚合物2.0 PWA /網站。 我在訪問div容器時遇到問題(我希望根據用戶輸入來放大和縮小div)。

在帶有模板dom的shadow dom中,如果我無法獲取Javascript中的元素,以便在用戶操作(如按鈕單擊或滑塊)移動時可以縮放div。

我嘗試了文檔getElementsByClassName,shadowroot搜索。 我可以在影子dom中看到該div存在。 只是無法在Polymer的javascript函數中訪問它。

var ele = document.getElementsByClassName('containerMap')[0]; // 
returns null in console can't find the element in shadow Dom
      var el = this.shadowRoot.querySelector('someid'); // returns 
            null in console also tried '#someid' still null
    console.log('shadowRoot el = '+JSON.stringify(el));
    var elx = document.getElementById('someid'); // returns null in 
                                                   console
    console.log('shadowRoot el = '+JSON.stringify(elx));
      //var el = this.$.someid; // does not find the element
      // tried this.$['#someid'] this.$$('#someid') // does not work
    console.log(el);

這是完整的代碼(刪除了一些不需要的代碼元素,以便將重點放在手頭的問題上)

MyApp.html

 <link rel="lazy-import" href="epicsprint-page.html">

 <dom-module id="my-app">
   <template is="dom-bind">
     <style>
       ....
     </style>
     ....

<app-header-layout >

    <div style="float:left; width:100%;">
         <div style="float:left; width:10%;">
           <paper-icon-button id="menuElement" icon="icons:menu" drawer-toggle halign="left"></paper-icon-button>
         </div>
    </div>

  <div fit class="content">
        <!-- IRON PAGES MAIN CONTENT -->
        <iron-pages
            selected="[[page]]"
            attr-for-selected="name"
            fallback-selection="not-found-404"
            role="main">
          <epicsprint-page name="epicsprint-page" user="{{user}}" 
             signedin="{{signedIn}}" statusknown="{{statusKnown}}" > 
          </epicsprint-page>
                  .....

      </div>
      </app-header-layout>
   </app-drawer-layout>
  </template>
</template>
 <script>

</script>
    ...

    window.customElements.define(MyApp.is, MyApp);
  </script>
 </dom-module>

元素:epicspring-page.html

<dom-module id="epicsprint-page">
 <template is="dom-bind">
   <style include="shared-styles">

  :host {
    padding: 10px;
     font-family:'Fira Sans', sans-serif;

  }

a paper-button,
a:hover paper-button,
a:link paper-button,
a:active paper-button,
a:visited paper-button {
  width: 20%;
  height: 35px;
}


.page { overflow:auto; width: 90%; height: 100%; }
.containerMap { width:100%;  background: grey; }


.error-content{
  width: 80wh;
  height: 300px;
  overflow: auto;
  align-items: center;
}

/*************/
.board­__sprint {
  /*background: #5ba4cf;*/
  position: absolute;
  left: auto;
  padding: 20px;
     overflow-x: auto;
     white-space: nowrap;
    }

  .list {
     display: inline-block;
     vertical-align: top;
     /*background: #d2d7e5;*/
      width: 300px;
    padding: 1px;

    }

    .list-content {
      max-height: 80%;
      overflow-y: auto;
      padding-right: 5px;
      /*margin-right: -5px;*/
      margin-bottom: 20px;
      padding-bottom: 60px;
    }

    .list-name {
      padding: 10px;
      border: none;
      background: transparent;
    }

   .add-ticket {
      background: linear-gradient(to bottom, #61bd4f 0, #5aac44 
         100%);
     color: white;
     font-weight: bold;
      border: none;
      padding: 10px;
    }

   .ticket {
      /*background: white;*/
      flex: 0 0 auto;
       margin: 5px;
       width: 200px;
      height: 70px;
      align-items: center;
      white-space: normal;
   }

    .ticket-title {
      width: 100%;
     padding: 20px;
   }
   /*********/

  </style>

  <iron-ajax
      id="getEpicSprintMap"
      method="GET"
      url="{{url}}"
      handle-as="json"
      content-type="application/json"
      on-response="handleResponseGetEpicSprintMap"
      on-error="handleErrorGetEpicSprintMap"
      last-response="{{mapsData}}"
      last-error="{{lastError}}">
  </iron-ajax>

  <div style="width: 100%">
     <div style="float:left;">
        <div><h5></h5></div>
         <paper-icon-button src="../images/icons/help-icon-png.png" 
      on-tap="showHelpDialog"></paper-icon-button>
    </div>
  <div style="float:right;">
    <div><h5></h5></div>
    <paper-icon-button src="../images/icons/support-icon-png.png" 
      on-tap="showHelpDialog"></paper-icon-button>
    </div>
</div>

 <template is="dom-if" if="[[signedin]]">
    <template is="dom-if" if="[[mapsData.successFlag]]">

    <div style="float: center;">
          <h5 style="color: white; font-size: 12px; text- 
   align:left;">&nbsp;</h5>
    </div>
    <div style="float: center; display: flex">
        <iron-image src="../images/logo.png" preload 
    sizing="contain"
          on-tap="reloadEpics" style="width:200px; height:200px; 
    margin:auto;" ></iron-image>
    </div>
    <div style="float: center;">
        <h1 style="color: white" class="launchAmount">Epics & 
    Sprints</h1>
        <h2 style="color: white" class="launchAmount">Number of 
   sprints that an Epic spans</h2>
    </div>

 <input id="test" min="1" max="1000" value='1000' step="1" 
     onchange="showVal(this.value)" type="range"/>
 <div class="containerMap" id="someid">
   <div class="page">
      <div class="content">
      <div class="board­__sprint">
        <template is="dom-repeat" items="{{mapsData.data}}" 
        as="row">
        <div class="list">
          <div class="list­-content">
            <div style="text-align: center;">
              <iron-image class=""
                           src="../images/icons/epic-icon-png.png"
                           preload
                           sizing="contain"
                           style="width:50px; height:50px; margin- 
         left: 50px;" ></iron-image>
            </div>
            <div style="text-align: center;">
              <h3 class="sprintTitle" style="color: white; font- 
    size: 15px;background: black; padding: 10px; margin-top: 5px;">
                <b>Epic:&nbsp;&nbsp;</b>[[row.key]]
              </h3>
              <h3 class="sprintTitle" style="color: white; font- 
   size: 15px;background: black; padding: 10px; margin-top: 5px;">
                [[row.title]]
              </h3>
            </div>
              <template is="dom-repeat" items="{{row.sprint}}" 
    as="sprint">
                  <div class="ticket">
                      <paper-card style="float:center; width: 95%" 
    class$="{{_computedCardColorStories()}}">
                        <div style="width: 100%;">
                          <div style="float:left; width: 10%" 
   style$="{{getRandomInt(0, 20)}}">
                            <h7>&nbsp;</h7>
                          </div>
                          <div style="float:left; width: 90%; text- 
    align: center; padding: 10px;">
                              <h7 style="color: black; font-size: 
      18px; text-align:center;">[[sprint]]</h7>
                           </div>
                         </div>
                      </paper-card>
                  </div>
              </template>
            </div>
        </div>
  </template>
</div>

  </div>
  </div>
 </div>

</template>
</template>

</template>

  </template>

<script>

/**
* @polymer
* @extends HTMLElement
*/
class EpicSprintMapPage extends Polymer.Element {
  static get is() { return 'epicsprint-page'; }

  constructor() {
      super();
    }

  ready() {
    super.ready();
  }

  static get properties() {
    return {
      user: { type: Object, notify: true, readOnly: false, observer: '_userChanged' },
      signedin: { type: Boolean, notify: true, readOnly: false, observer: '_signedinChanged' },
      statusKnown: { type: Boolean, notify: true, readOnly: false, observer: '_statusChanged' },
      localSettings : { type: Object, notify: true, readOnly: false },
      mapsData: { type: Object, notify: true },
      filterFromDate : { type: String, notify: true },
      filterToDate: { type: String, notify: true },
      defaultSprintVelocity: { type: Number, value: 0, notify: true },
      zoomVal: { type: Number, value: 100, notify: true },
      }
    };

  _userChanged(user){
    this.user = user;
  }

  _signedinChanged(signedin){
    this.signedin = signedin;
  }

  _statusChanged(statusKnown){
    this.statusKnown = statusKnown;
  }

  showVal()
  {
    var zoomScale = Number(this.zoomVal)/100;
    console.log('zoomScale = '+zoomScale);
    var ele = document.getElementsByClassName('containerMap')[0]; // returns null in console can't find the element in shadow Dom
    var el = this.shadowRoot.querySelector('someid'); // returns null in console also tried '#someid' still null
    console.log('shadowRoot el = '+JSON.stringify(el));
    var elx = document.getElementById('someid'); // returns null in console
    console.log('shadowRoot el = '+JSON.stringify(elx));
    //var el = this.$.someid; // doesn't find the element
// tried this.$['#someid'] this.$$('#someid')
    console.log(el);

    transformOrigin = [0,0];
    el = el || instance.getContainer();
    var p = ["webkit", "moz", "ms", "o"],
    s = "scale(" + zoomScale + ")",
    oString = (transformOrigin[0] * 100) + "% " + (transformOrigin[1] * 100) + "%";
    for (var i = 0; i < p.length; i++) {
        el.style[p[i] + "Transform"] = s;
        el.style[p[i] + "TransformOrigin"] = oString;
    }
    el.style["transform"] = s;
    el.style["transformOrigin"] = oString;
  }



}//end Polymer

   window.customElements.define(EpicSprintMapPage.is, EpicSprintMapPage);
  </script>

 </dom-module>

我希望我可以在shadow dom中找到該元素,並且能夠通過轉換div來設置該元素的樣式並模仿縮放功能。

我不確定如何獲取元素以及應該使用什么語法。

嘗試刪除:

<template is="dom-bind">
...
</template>

您可以在index.html中使用dom-bind模板來進行綁定,而無需定義新的custome元素。 然后,您可以使用this.shadowRoot...選擇div元素this.shadowRoot...有關dom-bind的更多信息

編輯

由於存在一些我不知道其值的變量,但是請記住,如果dom-if語句為false,則其內容不會呈現。 因此,您不能訪問其中的任何元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM