用小窗口浏览器拉伸的背景

时间:2020-12-26 09:36:23

标签: html css reactjs particles.js

我在一个新的 React 项目中使用了particles-js 以了解有关其工作原理的更多信息。我已经设置了所有并设法将它显示为背景,同时在页面中间有 React 徽标。当网页在一个小窗口中加载并且粒子被拉伸时,问题就出现了。我想我缺少 CSS 属性或其他东西,但我无法弄清楚。

你知道可能是什么错误吗?

真的谢谢你!

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="homeContainer">
      <div id="particles-js">
        <div id="root"></div>
      </div>
    </div>
    <!--particles functions-->
    <script type="text/javascript" src="%PUBLIC_URL%/particles.js"></script>
    <!--particles config-->
    <script type="text/javascript" src="%PUBLIC_URL%/utilities.js"></script>
  </body>
</html>

index.css:

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
    monospace;
}

#root {
  z-index: 100;
  margin: auto;
  width: 100%;
  height: 100%;
  position: absolute;
}

canvas {
  position : absolute;
  width:100%;
  height:100%;
  z-index:-1;
}in m

这里有一些照片,看看发生了什么:

  • 在大窗口中看起来很棒的粒子: Particles looking great in a big window
  • 粒子在小窗口中加载时拉伸 Particles stretched when loaded in a small window

更新1: 如果它可能是由于 App.css 中的错误,这里是它的代码:

.App {
  text-align: center;
}

.App-logo {
  height: 40vmin;
  pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
  .App-logo {
    animation: App-logo-spin infinite 20s linear;
  }
}

.App-header {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.App-link {
  color: #61dafb;
}

@keyframes App-logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

1 个答案:

答案 0 :(得分:0)

你能去掉homeContainer div吗?然后添加这些 CSS 样式:

#particles-js {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: $FFF;
  background-image: url("");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 50% 50%;
}

canvas {
  display: block;
  vertical-align: bottom;
}