Installation
Install
Section titled “Install”npm install motion-on-scroll
pnpm add motion-on-scroll
yarn add motion-on-scroll
Minimal bootstrapping
Section titled “Minimal bootstrapping”You’ll need to import MOS styles, and run the MOS.init()
function.
<!-- Import MOS styles --><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/motion-on-scroll@latest/dist/mos.css" />
<!-- Initialize MOS --><script type="module"> import { MOS } from 'motion-on-scroll'; MOS.init();</script>
<!-- Somewhere in your markup --><section data-mos="fade-up"> ...</section>
---import 'motion-on-scroll/dist/mos.css';---<html lang="en"> <head> ... </head> <body> <section data-mos="fade-up"> ... </section>
<script> import { MOS } from 'motion-on-scroll'; MOS.init(); </script> </body></html>
That’s it! Every element declaring a data-mos
value is now animated when it enters the viewport.
CSS file
Section titled “CSS file”MOS ships with a small stylesheet that ensures elements are hidden upon initial page load. Import it from motion-on-scroll/dist/mos.css
or copy the file into your own pipeline. The contents of the file are below for convenience.
/* no pointer events until animation starts */[data-mos] { pointer-events: none;}
[data-mos].mos-animate { pointer-events: auto;}
/* fade & zoom presets */[data-mos^="fade"],[data-mos^="zoom"] { will-change: opacity, transform; opacity: 0;}
/* slide presets */[data-mos^="slide"] { will-change: transform;}
/* flip presets */[data-mos^="flip"] { will-change: transform; transform: rotateY(-180deg); backface-visibility: hidden;}