【问题标题】:Background stretched with small window browser使用小窗口浏览器拉伸背景
【发布时间】:2021-04-03 21:29:12
【问题描述】:

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

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

  • 在大窗口中看起来很棒的粒子:
  • 在小窗口中加载时拉伸的粒子

更新 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);
  }
}

【问题讨论】:

    标签: html css reactjs particles.js


    【解决方案1】:

    这段代码对我有用:

    position: fixed!important;
    left: 0;
    right: 0;
    top: 0;
    visibility: visible;
    }
    

    【讨论】:

      【解决方案2】:

      你能删除 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;
      }
      

      【讨论】:

      • 同样的结果,不知道为什么。似乎在加载视图时,背景位于顶部的矩形内,然后将其缩小以创建此拉伸
      • 分割根 div 和粒子 div 时会发生什么?像这样&lt;div id="particles-js"&gt;&lt;/div&gt; &lt;div id="root"&gt;&lt;/div&gt;
      • 没有任何变化
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多