简体   繁体   English

Javascript offsetHeight 没有从初始渲染高度更新?

[英]Javascript offsetHeight not updating from initial rendered height?

I'm loading content into a div using the infinite scroll method but it's not working as it should be.我正在使用无限滚动方法将内容加载到 div 中,但它无法正常工作。

This is the calculation I'm using in my window.onscroll function to load new content :这是我在window.onscroll函数中用于加载新内容的计算:

window.innerHeight + document.documentElement.scrollTop ===
document.documentElement.offsetHeight

But document.documentElement.offsetHeight never receives an updated value when new content is added.但是在添加新内容时document.documentElement.offsetHeight永远不会收到更新的值。 It always stays at the initial value, which is causing the entire infinite scroll to work in reverse, only loading new content when you scroll to the top of the screen and not the bottom as expected.它始终保持在初始值,这导致整个无限滚动反向工作,仅在滚动到屏幕顶部时加载新内容,而不是按预期滚动到底部。

I've made sure that the appended divs are being put in the right element, also tried using document.getElementById('wrapper').offsetHeight instead of pulling it off the body but I'm getting the same result.我已经确保附加的 div 被放置在正确的元素中,也尝试使用document.getElementById('wrapper').offsetHeight而不是将它从身体上拉下来,但我得到了相同的结果。

I've also tried removing all relevant css styling just in case that was causing the issue, again, same result.我还尝试删除所有相关的 css 样式,以防万一导致问题,同样的结果。

I know it's impossible for anybody to diagnose this in any detail without a lot more information, but it's a big project with many other elements that could potentially be causing this issue, so I'm just asking if anybody else has ever run up against something similar or has any suggestions of other things to try?我知道在没有更多信息的情况下,任何人都无法详细诊断此问题,但这是一个包含许多其他可能导致此问题的因素的大项目,所以我只是问是否其他人遇到过某些问题类似的或有其他建议可以尝试吗?

Many thanks!非常感谢!

** Here's a sandbox of a working version (where I got the code from originally). ** 这是一个工作版本的沙箱(我从那里得到了最初的代码)。 I just don't know why the offsetHeight element doesn't update in my codebase.我只是不知道为什么我的代码库中的 offsetHeight 元素没有更新。

EDIT Added my React Component below, not sure that it will help diagnosis though.编辑在下面添加了我的 React 组件,但不确定它是否有助于诊断。

 import React, { Component, Fragment } from 'react'; import axios from 'axios'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; class ProfileFollowers extends Component { constructor(props) { super(props); this.state = { error: false, hasMore: true, isLoading: false, followers: [] }; window.onscroll = () => { const { loadFollowers, state: { error, isLoading, hasMore } } = this; if (error || isLoading || !hasMore) return; if ( window.innerHeight + document.documentElement.scrollTop === document.documentElement.offsetHeight ) { loadFollowers(); } }; } componentWillMount() { this.loadFollowers(); } loadFollowers = () => { axios .get('https://randomuser.me/api/?results=10') .then(results => { const nextFollowers = results.data.results.map(follower => ({ email: follower.email, name: Object.values(follower.name).join(' '), photo: follower.picture.medium, username: follower.login.username, uuid: follower.login.uuid })); this.setState({ hasMore: this.state.followers.length < 100, isLoading: false, followers: [...this.state.followers, ...nextFollowers] }); }) .catch(err => { this.setState({ error: err.message, isLoading: false }); }); }; render() { const { error, hasMore, isLoading, followers } = this.state; return ( <div id="profile-followers-wrap"> {followers.map(follower => ( <Fragment key={follower.username}> <hr /> <div style={{ display: 'flex' }}> <img alt={follower.username} src={follower.photo} style={{ borderRadius: '50%', height: 72, marginRight: 20, width: 72 }} /> <div> <h2 style={{ marginTop: 0 }}>@{follower.username}</h2> <p>Name: {follower.name}</p> <p>Email: {follower.email}</p> </div> </div> </Fragment> ))} <hr /> {error && <div style={{ color: '#900' }}>{error}</div>} {isLoading && <div>Loading...</div>} {!hasMore && <div>No more!</div>} </div> ); } } ProfileFollowers.propTypes = { auth: PropTypes.object.isRequired }; const mapStateToProps = state => ({ auth: state.auth }); export default connect(mapStateToProps)(ProfileFollowers);

EDIT 2 Added screenshot of console to show the problem in action.编辑 2添加了控制台的屏幕截图以显示实际问题。

在此处输入图片说明

FFS! FFS! Found it... I had the height set to 100% on the global html element :找到了...我将全局 html 元素的高度设置为 100%:

html {
  height: 100%; // Arrrrggghhhh!
  width: 100%;
}

This was preventing the offsetHeight from updating.这会阻止offsetHeight更新。

You won't believe how long it took to debug this so I'm leaving this here in case anybody else ever stumbles across a similar issue.你不会相信调试这个需要多长时间,所以我把它留在这里以防其他人偶然发现类似的问题。 Hope I can save somebody else a few hours of hair pulling :)希望我可以节省别人几个小时的头发拉:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果呈现,JavaScript HTMLElement.offsetHeight 未测量? - JavaScript HTMLElement.offsetHeight is not measured IF rendered? JavaScript getBoundingClientRect()vs offsetHeight,同时计算元素高度 - JavaScript getBoundingClientRect() vs offsetHeight while calculate element height 设置Java容器使用CSS填充,边距和边框的HTML容器的绝对高度(offsetHeight) - Set absolute height (offsetHeight) of HTML containers that use CSS padding, margin and border by Javascript IE6 javascript无法通过document.getElementById(id).offsetHeight获得高度 - IE6 javascript can't get height by document.getElementById(id).offsetHeight JavaScript-使用.offsetHeight的If语句不起作用 - JavaScript - If statement using .offsetHeight not working CSS转换比例和JavaScript offsetHeight - CSS transform scale and Javascript offsetHeight Javascript-获取实际渲染的字体高度 - Javascript - get actual rendered font height 使用js offsetHeight获取包含图像的div的高度 - Get height of div containing image with js offsetHeight offsetHeight、clientHeight 和 scrollHeight 没有给出正确的高度 - offsetHeight, clientHeight and scrollHeight does not give the correct height 如何使用.offsetHeight 获取元素的高度? - how to get height of an element using .offsetHeight?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM