簡體   English   中英

如何獲得全高?

[英]How to get the full height?

我正在嘗試獲取頁面的完整高度以將其應用於 styles,特別是darkMask class。

我需要高度,因為當我在 header 內執行操作時,除了調用操作后出現的元素之外,整個頁面都會應用一個深色層。

現在,由於 useRef 鈎子,我得到了高度,然后我把它放到了容器中,但這並沒有給我完整的高度

import { useContext, useState, useRef, useEffect } from 'react';
import { useRouter } from 'next/router';
import { Header } from '../Header';
import { SearchToggleContext } from '../../contexts/SearchToggleContext';
import styles, { globalStyles } from './styles';

export const Layout = ({ children }) => {
  const [height, setHeight] = useState();
  const mainContainer = useRef(null);
  const { isOpenSearch } = useContext(SearchToggleContext);
  const router = useRouter();

  useEffect(() => {
    if (mainContainer.current) {
      setHeight(mainContainer.current.offsetHeight);
    }
  }, [router]);

  return (
    <>
      <div className='darkMask' />
      <Header />
      <main ref={mainContainer}>{children}</main>

      <style jsx global>
        {globalStyles}
      </style>
      <style jsx>{styles}</style>
      <style jsx>{`
        .darkMask {
          display: ${isOpenSearch ? 'block' : 'none'};
          height: ${height}px;
        }
      `}</style>
    </>
  );
};

/* Styles of layout */
import css from 'styled-jsx/css';

export const globalStyles = css.global`
  html,
  body {
    height: 100%;
    width: 100%;
    font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
      Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
  }

  a {
    color: inherit;
    text-decoration: none;
  }

  * {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
  }

  .navbarItem {
    margin-right: 2em;
  }
`;

export default css`
  main {
    height: calc(100vh - 80px);
    width: 100%;
    top: 80px;
    position: absolute;
  }

  .darkMask {
    position: relative;
    z-index: 4;
    width: 100%;
    top: 80px;
    position: absolute;
    background-color: rgba(45, 45, 45, 0.9);
  }
`;
/* _app.js file */
import { Layout } from '../components/Layout';
import { SearchToggleContextProvider } from '../contexts/SearchToggleContext';

function MyApp({ Component, pageProps }) {
  return (
    <>
      <SearchToggleContextProvider>
        <Layout>
          <Component {...pageProps} />
        </Layout>
      </SearchToggleContextProvider>
    </>
  );
}

export default MyApp;

您可以使用document.body.offsetHeight來獲取整個<body>元素的高度。

 const { useState, useEffect } = React; function App() { const [height, setHeight] = useState(null); const [count, setCount] = useState(0); useEffect(() => { setHeight(document.body.offsetHeight); }, [count]); const elements = []; for (let i = 0; i < count; i++) { elements.push(<p key={i}>{`Element ${i}`}</p>); } return ( <div className="App"> <span>{`Document height: ${height} px`}</span> <div> <button onClick={() => setCount(count + 1)}>Add element</button> {elements} </div> </div> ); } ReactDOM.render(<App />, document.getElementById("root"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="root"></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM