获取嵌套在聚合物纸张对话框中的自定义Web组件内内容的客户端高度
Get clientHeight of content inside custom web-component nested in Polymer paper-dialog
我会尽量把我的问题解释得最清楚。
我正在使用<paper-dialog>
和<paper-dialog-scrollable>
.
在<paper-dialog-scrollable>
我有一个表格,它是一个自定义web-component
。
在这种形式中,我使用另一个自定义web-component
来展开和折叠内容。
内容的默认状态为折叠。
在 expandcomponent 中,我将内容的clientHeight
保存在变量contentHeight
中,并将内容的height
设置为 0。
我有一个函数toggle()
,它在单击触发器时执行。
toggle()
将内容height
设置为 contentHeight
。
现在,当我单独使用我的表单或扩展组件时,这非常有效,但是当它们嵌套在paper-dialog
中时,它不起作用,因为clientHeight
为 0。
法典:
<paper-dialog with-backdrop style="min-width: 90%;">
<paper-dialog-scrollable>
<my-custom-form-component></my-custom-form-component>
</paper-dialog-scrollable>
</paper-dialog>
来自<my-custom-form-component>
的代码:
<div id="custom-expand-component-trigger"></div>
<custom-expand-component trigger="custom-expand-component-trigger">
blabla a lot of content......
</custom-expand-component>
toggle()
功能(<custom-expand-component>
内部):
function toggle(){
if(!that.opened){
content.style.height = contentHeight + 'px'; //contentHeight is 0 when form is nested in <paper-dialog>
} else{
content.style.height = startHeight;
}
that.opened = !that.opened;
}
任何想法,即使我的表单在对话框中,我如何获得clientHeight?
我希望这已经足够清楚了。
帮助将不胜感激
隐藏
元素的clientHeight
为 0,因此在呈现之前无法获取它。
当<paper-dialog>
打开时,元素实际上首先呈现。发生这种情况时,将触发iron-overlay-opened
事件。如果您以前没有设置过,这是一个获得正确clientHeight
的机会:
myDialog.addEventListener( "iron-overlay-opened", function ()
{
this.querySelector( "custom-expand-component" ).init()
} )
在 init()
方法中,为私有变量设置正确的值:
var CEC = Object.create( HTMLElement.prototype )
CEC.createdCallback = function () {
var that = this
var startHeight
var contentHeight
this.init = function () {
if ( !contentHeight ) {
contentHeight = this.clientHeight + "px" //OK
startHeight = this.getAttribute( "start-height" )
opened = false
this.style.height = startHeight
}
}
document.getElementById( this.getAttribute( "trigger" ) ).onclick = toggle
function toggle() {
opened = !opened
that.style.height = ( opened )? contentHeight : startHeight
}
}
document.registerElement( "custom-expand-component", { prototype: CEC } )
CSS 过渡现在正在工作:
custom-expand-component {
display: block ;
overflow-y: scroll ;
transition: height 0.5s ;
}
相关文章:
- 自定义HTML元素属性未显示-Web组件
- 使用javascript为web应用程序自定义键盘快捷键
- 在我的 Web API 中无法获取我的自定义标头
- Canvas仅使用fabric.js在点击事件时渲染自定义Web字体
- 使用普通javascript和验证的自定义web组件
- TextBox自定义Web控件和JavaScript客户端验证
- 根据 IP 向 Web 访问者显示自定义消息
- asp.net Web API 405 方法不允许、发布、自定义操作
- 获取嵌套在聚合物纸张对话框中的自定义Web组件内内容的客户端高度
- 使用 jQuery 和 asp.net Web 表单进行自定义确认
- 带有 Web 浏览器控件的 Excel 自定义任务窗格 - 使用功能区按钮显示/隐藏
- Web 语音 API 自定义字词
- 如何编写用于自定义剪裁的 3D Web 应用程序
- TFS 2012 Web 访问工作项自定义控件
- 如何使用 VB.NET 保护自定义 Web 服务
- 使用Selenium Web驱动程序注入自定义JS代码
- 在ASP.NET Web用户控件中生成客户端自定义事件
- 通过web界面修改自定义配置文件的最佳方式
- 带有自定义格式的C#Web服务json数据jquery ui
- 自定义方法以及web-dev中构造函数和原型的有用性