|
1 | | -"# scroll-observer" |
| 1 | +# Scroll Observer |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +## Introduction |
| 6 | +ScrollObserver A JavaScript library to animate easily elements on scroll into view. |
| 7 | +## How it works |
| 8 | +When a DOM element is onscreen it adds `shown` class to it and when offscreen removes that `shown` class. |
| 9 | +## How to use |
| 10 | +```js |
| 11 | +scrollObserver('.card') |
| 12 | +``` |
| 13 | + |
| 14 | +The above JavaScript code adds and removes the `shown` class to the elements which have `card` class (select elements like `querySelectorAll()`) when they are onscreen and offscreen respectively. |
| 15 | + |
| 16 | + |
| 17 | +## CSS |
| 18 | +Write css of `.card` and `.card.shown` like this |
| 19 | +```css |
| 20 | +.card{ |
| 21 | + ... |
| 22 | + ... |
| 23 | + transition: 400ms; |
| 24 | + opacity : 0; |
| 25 | +} |
| 26 | +.card.shown { |
| 27 | + opacity: 1; |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Features |
| 32 | +### options argument |
| 33 | +```js |
| 34 | +const options = { |
| 35 | + root : document.querySelector('.container'), |
| 36 | + rootMargin : '10px', |
| 37 | + threshold: [0, 0.25, 0.5, 0.75, 1], |
| 38 | + |
| 39 | + once: true, |
| 40 | + onshow: function (entry) { |
| 41 | + console.log("Showing ") |
| 42 | + }, |
| 43 | + |
| 44 | + onhide: function (entry) { |
| 45 | + console.log("Hiding ") |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +scrollObserver('.card', options) |
| 50 | +``` |
| 51 | +Read more about Options argument from [MDN Docs ](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#creating_an_intersection_observer) |
| 52 | + |
| 53 | +## once |
| 54 | +If the value of once is true, it will work once. |
| 55 | + |
| 56 | +## onshow and onhide |
| 57 | +They are callback functions. They are called when the element becomes onscreen or offscreen |
| 58 | + |
| 59 | +## root, rootMargin, threshold |
| 60 | +Read them from [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#creating_an_intersection_observer) |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +## Example |
| 69 | +- index.html |
| 70 | +- style.css |
| 71 | +- script.js |
| 72 | + |
| 73 | + |
| 74 | +### index.html |
| 75 | +```html |
| 76 | +<!DOCTYPE html> |
| 77 | +<html lang="en"> |
| 78 | +<head> |
| 79 | + <meta charset="UTF-8"> |
| 80 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 81 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 82 | + <title>Scroll Observer</title> |
| 83 | + <script src="./script.js" type="module" defer></script> |
| 84 | + <link rel="stylesheet" href="style.css"> |
| 85 | +</head> |
| 86 | + |
| 87 | +<body> |
| 88 | + <div class="card"> |
| 89 | + <h1>Scroll Observer</h1> |
| 90 | + </div> |
| 91 | + <div class="card"> |
| 92 | + <h1>Scroll Observer</h1> |
| 93 | + </div> |
| 94 | + <div class="card"> |
| 95 | + <h1>Scroll Observer</h1> |
| 96 | + </div> |
| 97 | + <div class="card"> |
| 98 | + <h1>Scroll Observer</h1> |
| 99 | + </div> |
| 100 | + <div class="card"> |
| 101 | + <h1>Scroll Observer</h1> |
| 102 | + </div> |
| 103 | + <div class="card"> |
| 104 | + <h1>Scroll Observer</h1> |
| 105 | + </div> |
| 106 | + <div class="card"> |
| 107 | + <h1>Scroll Observer</h1> |
| 108 | + </div> |
| 109 | + <div class="card"> |
| 110 | + <h1>Scroll Observer</h1> |
| 111 | + </div> |
| 112 | +</body> |
| 113 | +</html> |
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | +### style.css |
| 118 | +```css |
| 119 | +*{ |
| 120 | + font-family: Arial, Helvetica, sans-serif; |
| 121 | +} |
| 122 | +body{ |
| 123 | + overflow-x: hidden; |
| 124 | +} |
| 125 | + |
| 126 | +.card{ |
| 127 | + width: min(90%, 500px); |
| 128 | + aspect-ratio: 2 / 1; |
| 129 | + margin-inline: auto; |
| 130 | + background-color: hsl(61, 70%, 70%); |
| 131 | + display: grid; |
| 132 | + place-items: center; |
| 133 | + margin-block: 7%; |
| 134 | + border-radius: 10px; |
| 135 | + transition: 400ms; |
| 136 | +} |
| 137 | + |
| 138 | +.card:nth-child(2n){ |
| 139 | + translate: 100%; |
| 140 | + opacity: 0; |
| 141 | +} |
| 142 | +.card:nth-child(2n + 1){ |
| 143 | + translate: -100%; |
| 144 | + opacity: 0; |
| 145 | +} |
| 146 | +.card.shown { |
| 147 | + translate: 0; |
| 148 | + opacity: 1; |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### script.js |
| 153 | +```js |
| 154 | +import scrollObserver from '../index.js' |
| 155 | +scrollObserver('.card') |
| 156 | +``` |
| 157 | + |
| 158 | +See above example [Live](https://codeabinash.github.io/scroll-observer/example/) |
0 commit comments