DEV Community

Cover image for Add Particles.js to Your Squarespace Site
Michael Synan
Michael Synan

Posted on

1

Add Particles.js to Your Squarespace Site

Particles.js background on Squarespace

Particles.js is a lightweight plugin that adds motion and interaction to your Squarespace site. You can set up a Particles.js background on your site in five steps:

  1. Upload the Javascript file
  2. Inject Particles.js and Javascript file into the header
  3. Add CSS styles
  4. Add custom code-block with target div

Let's go through each of steps.


Upload the Javascript file

First you should upload this file to your Squarespace site: https://github.com/michaelsynan/squarespace-files/blob/main/floatingMO.js

The technique uses the Mutation Observer API to make sure the necessary script doesn't load until its target div has been rendered. To upload a file to Squarespace you need to go to "pages" -> new page -> "link" -> gear icon -> "file" -> "upload file" (I know its a little unintuitive). Note the link address, which is "/s/floatingMO.js" in this case.

Inject Javascript into your Squarespace site

To inject your new Javascript file you need to go to "settings" -> "developer tools" -> "code injection", then place the following code in the header (remember to check your javasript file link):

<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script src="/s/floatingMO.js"></script>
Enter fullscreen mode Exit fullscreen mode

Add CSS Styles

"design" -> "custom CSS"

#particles-js-wrapper {
    position: fixed;
    width: 100%;
    height: 100vh;
    top: 0;
    left: 0;
    background-color: #000;
    z-index: -1;
}

#particles-js {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 0;
}

#particles-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: red;
    z-index: 1;
}
Enter fullscreen mode Exit fullscreen mode

Add Custom Code Block

Go to the editor in your Squarespace site and add the following code block:

<div id="particles-js-wrapper">
    <div id="particles-js"></div>
</div>
Enter fullscreen mode Exit fullscreen mode

It's important to note that the code above targets the div "#particles-js" - if you want to change this you should update the JavaScript GitHub file linked to above.

If you just want to play around with Particles.js outside of Squarespace, you can check out my CodePen here. Feel free to fork it and post your Pens below - I'll be sure to like and reply.

Enjoy and feel free to reach out if you have any questions!

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay