Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Include my Javascript animation on all the html body so it stays while scrolling the page

Hi i'm learning html/css and javascript and I think I'm having an issue with my html structure. Basically what I want to do is that my particles animation stays on the website while scrolling the page. I have a Javascript file that does a getElementById('particles') to run the canvas on a div but it only stays on the first page.

I tried to move the "particles" div as a main div that will contain all the sections but it didn't work.

Here's the repository of the files if anyone is interested: https://github.com/DanielVillacis/DanielVillacis.github.io

Here's my html structure :

document.addEventListener('DOMContentLoaded', function() {
  particleground(document.getElementById('particles'), {
    dotColor: '#FFFFFF',
    lineColor: '#FFFFFF'
  });
  var intro = document.getElementById('intro');
  intro.style.marginTop = -intro.offsetHeight / 2 + 'px';
}, false);
html,
body {
  width: 100%;
  height: 100vh;
}

canvas {
  display: inline-block;
  vertical-align: baseline;
}

header,
section {
  display: block;
}

#particles {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

.container {
  scroll-snap-type: y mandatory;
  overflow-y: scroll;
  height: 100vh;
}

section {
  height: 100vh;
  width: 100%;
  display: inline-block;
  scroll-snap-align: start;
}
<body>
  <div class="container">
    <main role="main">
      <section class="intro">
        <div id="particles">
          <header class="splash" id="splash" role="banner">
            <div id="intro">

            </div>
          </header>
        </div>
      </section>

      <section class="AboutMe">
        <div class="introduction">
        </div>
      </section>

      <section class="box">
        <div class="projectContainer">
        </div>
      </section>

      <section class="Contact">
        <h2 class="ContactTitle">Contact</h2>
        <div class="contactLinks">
        </div>
      </section>
    </main>
  </div>
</body>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use the CSS position: fixed; property.

With position set to fixed, your canvas is positioned relative to the viewport and hence would remain even while scrolling.

.pg-canvas {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    pointer-events: none;
    width: 100%;
    height: 100vh;
}

about 4 years ago · Juan Pablo Isaza Report

0

You have put the particles (which are shown on a canvas) into a section which will scroll out of view.

The particles library you are using places this canvas just before the element you have given it, which has id particles.

You can fix just the canvas by adding position: fixed to the canvas selector in your style sheet (watch out if you have other canvases to give a more definite selector).

This will work in many cases to fix the canvas with the particles to the viewport. But note this description from MDN

The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to the initial containing block established by the viewport, except when one of its ancestors has a transform, perspective, or filter property set to something other than none (see the CSS Transforms Spec), in which case that ancestor behaves as the containing block. (Note that there are browser inconsistencies with perspective and filter contributing to containing block formation.) Its final position is determined by the values of top, right, bottom, and left.

You are OK at the moment because you move intro with top but if that were a translate you’d have to put the canvas out of intro.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!