1、获取React元素的宽高遇到的问题。
2、按理说componentDidMount里面获取DOM的高度应该是36px,为什么每次都是144px,但是如果用setTimeout之后再调用就对了。
componentDidMount的函数如下:
componentDidMount(){
let tabDOM1 = this.tabDOM;
console.log(tabDOM1); //...
console.log(tabDOM1.scrollHeight); //144
console.log(window.getComputedStyle(tabDOM1).height); //144px
let tabDOM2 = ReactDom.findDOMNode(this.tabDOM);
console.log(tabDOM2); //...
console.log(tabDOM2.scrollHeight); //144
console.log(window.getComputedStyle(tabDOM2).height); //144px
setTimeout(function(){
let tabDOM1 = this.tabDOM;
console.log(tabDOM1); //...
console.log(tabDOM1.scrollHeight); //36
console.log(window.getComputedStyle(tabDOM1).height); //36px
let tabDOM2 = ReactDom.findDOMNode(this.tabDOM);
console.log(tabDOM2); //...
console.log(tabDOM2.scrollHeight); //36
console.log(window.getComputedStyle(tabDOM2).height); //36px
}.bind(this),1000)
}
render函数如下:
render() {
let tabContent;//内容主体
let list;//li的标签列表
return (
{this.tabDOM = dom}} >
{list}
{tabContent}
)
}
css如下
ul{
list-style: none;
margin:0;
padding:0;
height:36px;
border-bottom:4px solid #666;
overflow: hidden;
}
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
认证0级讲师