微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决用小窗口浏览器拉伸的背景

我在一个新的 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);
  }
}

解决方法

你能去掉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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。