javascript - polymer 1.0/leaflet 0.7.3 中的 Element.clientWidth/.clientHeight

标签 javascript polymer leaflet polymer-1.0

Leaflet 通常需要一个具有定义高度的 ... 作为其在普通 DOM 中的容器。我想在自定义元素中复制它。

问题是:如何让自定义元素的第一个子内容元素具有由 Leaflet 使用的 Element.clientHeight、Element.clientWidth 报告的定义的高度和宽度?

我正在尝试使用 Polymer 1.0 + leaflet 0.7.3 构建自定义元素。我意识到传单对 DOM 的使用可能会成为一个需要管理的问题,但是我在设置传单容器以便首先创建有效的 map 时遇到问题。我认为这是因为 leaflet 使用 Element.clientHeight/.clientWidth 来生成它请求图 block 的范围。

我在这里设置了一个plunker:http://plnkr.co/edit/uELMbapeApmp8MBfomfr?p=info

  console.log(this.localName + '#' + this.id + ' was attached');
  console.log("getComputedStyle("+this.localName+").height/width: " + window.getComputedStyle(this).height+"/"+ window.getComputedStyle(this).width);
  console.log(this.localName + "this.clientHeight/Width: "+this.clientHeight+ "/"+this.clientWidth);
  this.map = L.map(this.$.map, {
    center: new L.LatLng(this.getAttribute('lat'), this.getAttribute('lon')),
    zoom: this.getAttribute('zoom')
  });
  console.log("map center: "+this.map.getCenter());
  var west = this.map.getBounds().getWest(),
      south = this.map.getBounds().getSouth(),
      east = this.map.getBounds().getEast(),
      north = this.map.getBounds().getNorth();

  if (west === east || south === north) {
    console.log('ERROR: BAD EXTENT');
  }

如果您显示控制台,它应该记录首次设置 map 时计算的范围。奇怪的是,如果我在本地运行它,并在 my-map.html 附加回调的第一行(上面的第一个 console.log)上放置一个断点,并逐步执行它,则范围的计算方式会有所不同(正确) 。如果没有断点,则计算范围的高度为 0,并记录错误。

我已经尝试过:

<template><div id="map"></div></template> <template><div id="map"><content></content></div></template>

当然,模板中根本没有任何内容。

最佳答案

问题是附加的回调在样式等完全传输到元素之前执行。这是通过使用 async(function, [wait]) 库方法解决的。

关于javascript - polymer 1.0/leaflet 0.7.3 中的 Element.clientWidth/.clientHeight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30852285/

相关文章:

c# - 在 Javascript 中使用模型属性

javascript - NodeJs 使用 PHP 和 EJS 作为模板引擎,我如何从路由到模板和在模板中传递和访问数据?

polymer 1.0 : reference selected iron-list item in CSS

polymer 初始化(无响应)

javascript - 对象实例的引用 - React

javascript - map 中的圆圈(大小可缩放)

javascript - 单击时更改文本未按预期工作

javascript - 我可以假设我的 js 文件采用 gzip 压缩吗?

javascript - 将 Vue.js 与 WebComponents 结合使用,例如 Polymer 的 Paper-Elements

javascript - 使用 AJAX 调用将 GeoJSON 数据拉入 Leaflet