gpt4 book ai didi

javascript - 使用纯 javascript 获取元素的尺寸和位置

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:17 25 4
gpt4 key购买 nike

<分区>

情况:我想获取一个元素的尺寸和位置

上下文:我正在制作纸牌游戏。卡片可以放在板上。我需要能够检查他们的目的地是否在板上。然后我将存储它们相对于板的尺寸和位置的位置。这将有助于在可能使用不同屏幕尺寸的大量不同客户之间重现该板。

当前样式:

板的尺寸宽度和高度定义为:

width: 100%;
flex-grow:1;

它的容器有这些属性:

display: flex;
flex-direction: column;

Image of Board

请注意,实际尺寸为 [942.922x874]。我找不到位置信息,我假设是因为默认的 div position:static 而不是 position:absolute。

我的尝试:

let board = document.getElementById("gameboard");
if (board) {
console.log("style.height", board.style.height);
console.log("style.width", board.style.width);
console.log("offsetHeight", board.offsetHeight);
console.log("offsetWidth", board.offsetWidth);
console.log("getBoundingClientRect" , board.getBoundingClientRect());
}

Output of my attempts

请注意,输出尺寸与我的实际尺寸不同。[1137, 920] 而不是 [942.922x874]。 [1137, 920] 实际上是整个 body 的尺寸。此外,无论我做什么来获得位置,它总是给我 [0,0]。

EDIT-1

我们得出的结论是,这可能是时间问题。我需要确保在呈现元素后运行检索尺寸和位置的代码。因为我正在使用 react ,所以我发现了这个线程,ReactJS - Get Height of an element .这基本上是说我需要连接到 React 的组件生命周期方法 componentDidMount() ,该方法在渲染完成后立即运行。你可以在下面看到我的实现。但是,即使使用此实现,我似乎仍然获得主体容器的尺寸,而不是我正在访问的元素。

import React, {PureComponent, PropTypes} from 'react';
// import Card from '../../Components/Card/index.js';
// import Collection from '../../Components/Collection/index.js';
import {List} from 'immutable';
import Indicator from '../../Components/Indicator/index.js';
import Counter from '../../Components/Counter/index.js';
import {connect} from 'react-redux';
import * as actionCreators from './../../action_creators.js';
import './styles.scss';

export default class Game extends PureComponent {
componentDidMount() {
let board = document.getElementById("gameboard");
console.log("getBoundingClientRect" , board.getBoundingClientRect());
}
render() {
let playersById = this.props.playersById;
let collections = this.props.collections;
let counters = this.props.counters;

let indic_list = [];
let coll_list = [];

//Indicators
playersById.forEach(function(value, key, map){
indic_list.push(<p className="section-name">{playersById.get(key).get('name')+"'s Indicators"}</p>)
playersById.get(key).get('indicators').forEach(function(value, key2, map){
indic_list.push(<Indicator playerId={key} action={this.props.modIndicator} label={key2}>{value}</Indicator>)
}, this);
},this);

//Collections
collections.forEach(function(value, key, map) {
coll_list.push(<span>{key}: {collections.get(key).get("content").size}</span>);
collections.get(key).get("control").forEach(function(value, key, map){
coll_list.push(<span>Control: {value}</span>);
});
});

return (
<div className="frame">
<div className="left-col">
{indic_list}
</div>
<div className="center-col">
<span className="collections">
{coll_list}
</span>
<div id="gameboard"></div>
</div>
<div className="right-col">
<div className="counters">
{counters.map(function(type){
return <Counter label={type}>💙</Counter>
})}
</div>
</div>
</div>
)
}
}
Game.PropTypes = {
playersById: PropTypes.object.isRequired,
collections: PropTypes.object.isRequired,
counters: PropTypes.object.isRequired,
modIndicator: PropTypes.func.isRequired
}
function mapStateToProps(state) {
return {
playersById: state.get('playersById') || new List(),
collections: state.get('collections') || new List(),
counters: state.get('counters') || new List()
};
}
export const GameContainer = connect(mapStateToProps, actionCreators)(Game);

Edit-2 解决方案我的问题的解决方案来自这个线程。 Get the height of a Component in React

下面是我代码中的实现。

import React, {PureComponent, PropTypes} from 'react';
// import Card from '../../Components/Card/index.js';
// import Collection from '../../Components/Collection/index.js';
import {List} from 'immutable';
import Indicator from '../../Components/Indicator/index.js';
import Counter from '../../Components/Counter/index.js';
import {connect} from 'react-redux';
import * as actionCreators from './../../action_creators.js';
import './styles.scss';

export default class Game extends PureComponent {
render() {
let playersById = this.props.playersById;
let collections = this.props.collections;
let counters = this.props.counters;

let indic_list = [];
let coll_list = [];

//Indicators
playersById.forEach(function(value, key, map){
indic_list.push(<p className="section-name">{playersById.get(key).get('name')+"'s Indicators"}</p>)
playersById.get(key).get('indicators').forEach(function(value, key2, map){
indic_list.push(<Indicator playerId={key} action={this.props.modIndicator} label={key2}>{value}</Indicator>)
}, this);
},this);

//Collections
collections.forEach(function(value, key, map) {
coll_list.push(<span>{key}: {collections.get(key).get("content").size}</span>);
collections.get(key).get("control").forEach(function(value, key, map){
coll_list.push(<span>Control: {value}</span>);
});
});

return (
<div className="frame">
<div className="left-col">
{indic_list}
</div>
<div className="center-col">
<span className="collections">
{coll_list}
</span>
<div id="gameboard" ref={(node) => this.calcHeight(node)}></div>
</div>
<div className="right-col">
<div className="counters">
{counters.map(function(type){
return <Counter label={type}>💙</Counter>
})}
</div>
</div>
</div>
)
}
calcHeight(node){
if (node) {
console.log("calcHeight", node.getBoundingClientRect());
}
}
}
Game.PropTypes = {
playersById: PropTypes.object.isRequired,
collections: PropTypes.object.isRequired,
counters: PropTypes.object.isRequired,
modIndicator: PropTypes.func.isRequired
}
function mapStateToProps(state) {
return {
playersById: state.get('playersById') || new List(),
collections: state.get('collections') || new List(),
counters: state.get('counters') || new List()
};
}
export const GameContainer = connect(mapStateToProps, actionCreators)(Game);

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com