问题
I'm trying to use particles.js as background, but I am not able to set the canvas as a full-size background.
I tried at least 10 different solution from similar issues but nothing worked. The canvas always results as as an element having width-height ratio as the screen but it doesn't cover it as a whole when it is resized. In addiction, it won't set as background but as a child of the body, over or below the remaining elements.
I simplified the code as much as possible and the problem still persist.
Example of the problem
HTML
<!DOCTYPE HTML>
<html>
<head>
<title>Try</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" media="screen" href="css/style.css">
</head>
<body>
<div id="particles-js"></div>
<div>
<p>Something here</p>
</div>
</body>
<script src="assets/js/particles.js"></script>
<script src="assets/js/app.js"></script>
</html>
CSS
canvas{
display:block;
vertical-align:bottom;
position: absolute;
}
/* ---- particles.js container ---- */
#particles-js{
position: absolute;
width: 100%;
height: 100%;
background-color: #b61924;
background-image: url('');
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
top: 0;
}
APP.JS and PARTICLES.JS can be downloaded from the owner's website posted before. I'm using themas they are at the moment
Thanks for any help
回答1:
Ok I finally solved the background question: That's how I managed it (I'm pretty sure I had already tried that but re-building the whole html if finally worked)
CSS
#particles-js{
width: 100%;
height: 100%;
position: fixed;
background-color: #000;
background-image: url('');
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
}
.body-particles{
position: absolute;
top: 0;
left: 0;
z-index: 100;
}
HTML
<html>
<head>
<title>Title</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- SOME CSSs HERE -->
<link rel="stylesheet" media="screen" href="assets/css/particles.css">
<!-- SOME SCRIPTS HERE -->
</head>
<body>
<div class="body-particles">
<!-- Wrapper -->
<div id="wrapper">
<!-- MY STUFF HERE, STYLED WITH MY CSS -->
</div>
</div>
<!-- particles.js container -->
<div id="particles-js"></div>
<!-- scripts -->
<script src="assets/js/particles.js"></script>
<script src="assets/js/app.js"></script>
</body>
</html>
Now, I have a new problem: being set as an underlying layer, it catch no pointer-events. As soon as I will make it works, I'll update the answer.
Obviously, feel free to help if you have any suggestion.
UPDATE: Workaround for mouseEvents: Adding class mouseEvents to elements I need to focus (for example , navbar, etc.)
CSS
.body-particles{
pointer-events: none;
}
.mouseEvents{
pointer-events: all;
}
However, it would be nice if someone knows a better way to keep events both in front and back layer
回答2:
I only had to do this...
HTML:
<body>
<div id="particles-js"></div>
...
</body>
CSS:
#particles-js {
height: 100%;
width: 100%;
position: fixed;
}
回答3:
Assuming you're trying to mimic the example page, what I would do is this:
body: {
padding: 0px;
margin: 0px;
}
canvas{
width: 100%;
height: 100%;
}
/* ---- particles.js container ---- */
#particles-js{
position: fixed;
width: 100%;
height: 100%;
z-index: 0;
top: 0px;
left: 0px;
background-color: #b61924;
background-image: url('');
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
}
.actual-content-container
{
z-index: 100;
/* whatever other style properties you want. Apply this class to the div or other element you put your actual content in.*/
}
This is basically how the demo page is doing it. The key is the z-index property. If what you want is a background that scrolls with the actual content, try changing the position element of the particles-js id to absolute. I think that should get you in the ballpark.
回答4:
- Set positions fixed for #particles-js container
#particles-js{
position:fixed;
width: 100%;
height: 100%;
background: linear-gradient(to right, rgb(52, 152, 219), rgb(44, 62, 80));
background-repeat: repeat;
background-size: cover;
}
- assigned class panel for the div having content in it.
.panel{
position: absolute;
margin-top: 5%;
margin-bottom: 5%;
margin-left: auto;
margin-right: auto;
left: 50;
right: 50;
}
来源:https://stackoverflow.com/questions/45405463/particles-js-not-covering-entire-page