Repository: russellgoldenberg/enter-view
Branch: master
Commit: ec856046f026
Files: 8
Total size: 13.0 KB
Directory structure:
gitextract_ssz2htd4/
├── .eslintrc
├── .gitignore
├── .nojekyll
├── LICENSE
├── README.md
├── enter-view.js
├── index.html
└── package.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .eslintrc
================================================
{
"extends": ["airbnb-base", "prettier"],
"plugins": ["prettier"],
"rules": {
"quotes": [1, "single"],
"no-tabs": 0,
"indent": ["error", "tab"]
}
}
================================================
FILE: .gitignore
================================================
.DS_Store
node_modules
package-lock.json
================================================
FILE: .nojekyll
================================================
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2022 Russell Samora
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
================================================
FILE: README.md
================================================
# enter-view
Dependency-free JavaScript library to detect when element enters into view. [See demo](https://russellsamora.github.io/enter-view/). It uses requestAnimationFrame in favor of scroll events for less jank. Less than 1kb minified + gzipped.
## Installation
Old school (exposes the `enterView` global):
```html
<script src="https://unpkg.com/enter-view"></script>
```
New school:
```sh
npm install enter-view --save
```
And then import/require it:
```js
import enterView from 'enter-view'; // or...
const enterView = require('enterView');
```
## Usage
```
enterView({
selector: '.class-name',
enter: function(el) {
el.classList.add('entered');
}
});
```
```
enterView({
selector: '.class-name',
enter: function(el) {
el.classList.add('entered');
},
exit: function(el) {
el.classList.remove('entered');
},
progress: function(el, progress) {
el.style.opacity = progress;
},
offset: 0.5, // enter at middle of viewport
once: true, // trigger just once
});
```
## Options
#### selector: [string or array of elements] _required_
Takes a class, id, or array of dom elements.
#### enter: [function] _optional_
Callback function that returns the element that was entered.
#### exit: [function] _optional_
Callback function that returns the element that was exited.
#### progress: [function] _optional_
Callback function that returns the element that was progressed through, and a value between 0 and 1 of how far through the element progress has been made.
#### offset: [number] _optional_ (defaults to 0)
A value from 0 to 1 of how far from the bottom of the viewport to offset the trigger by. 0 = top of element crosses bottom of viewport (enters screen from bottom), 1 = top of element crosses top of viewport (exits screen top).
#### once: [boolean] _optional_ (defaults to false)
Whether or not to trigger the callback just once.
## Contributors
- [Russell Samora](https://github.com/russellsamora)
- [Jonathan Soma](https://github.com/jsoma)
================================================
FILE: enter-view.js
================================================
/*
* enter-view.js is library
*/
(function(factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
} else {
window.enterView = factory.call(this);
}
})(() => {
const lib = ({
selector,
enter = () => {},
exit = () => {},
progress = () => {},
offset = 0,
once = false
}) => {
let raf = null;
let ticking = false;
let elements = [];
let height = 0;
function setupRaf() {
raf =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
return setTimeout(callback, 1000 / 60);
};
}
function getOffsetHeight() {
if (offset && typeof offset === 'number') {
const fraction = Math.min(Math.max(0, offset), 1);
return height - fraction * height;
}
return height;
}
function updateHeight() {
const cH = document.documentElement.clientHeight;
const wH = window.innerHeight || 0;
height = Math.max(cH, wH);
}
function updateScroll() {
ticking = false;
const targetFromTop = getOffsetHeight();
elements = elements.filter(el => {
const { top, bottom, height } = el.getBoundingClientRect();
const entered = top < targetFromTop;
const exited = bottom < targetFromTop;
// enter + exit
if (entered && !el.__ev_entered) {
enter(el);
el.__ev_progress = 0;
progress(el, el.__ev_progress);
if (once) return false;
} else if (!entered && el.__ev_entered) {
el.__ev_progress = 0;
progress(el, el.__ev_progress);
exit(el);
}
// progress
if (entered && !exited) {
const delta = (targetFromTop - top) / height;
el.__ev_progress = Math.min(1, Math.max(0, delta));
progress(el, el.__ev_progress);
}
if (entered && exited && el.__ev_progress !== 1) {
el.__ev_progress = 1;
progress(el, el.__ev_progress);
}
el.__ev_entered = entered;
return true;
});
if (!elements.length) {
window.removeEventListener('scroll', onScroll, true);
window.removeEventListener('resize', onResize, true);
window.removeEventListener('load', onLoad, true);
}
}
function onScroll() {
if (!ticking) {
ticking = true;
raf(updateScroll);
}
}
function onResize() {
updateHeight();
updateScroll();
}
function onLoad() {
updateHeight();
updateScroll();
}
function selectionToArray(selection) {
const len = selection.length;
const result = [];
for (let i = 0; i < len; i += 1) {
result.push(selection[i]);
}
return result;
}
function selectAll(selector, parent = document) {
if (typeof selector === 'string') {
return selectionToArray(parent.querySelectorAll(selector));
} else if (selector instanceof NodeList) {
return selectionToArray(selector);
} else if (selector instanceof Array) {
return selector;
}
}
function setupElements() {
elements = selectAll(selector);
}
function setupEvents() {
window.addEventListener('resize', onResize, true);
window.addEventListener('scroll', onScroll, true);
window.addEventListener('load', onLoad, true);
onResize();
}
function init() {
if (!selector) {
console.error('must pass a selector');
return false;
}
setupElements();
if (!elements || !elements.length) {
console.error('no selector elements found');
return false;
}
setupRaf();
setupEvents();
updateScroll();
}
init();
};
return lib;
});
================================================
FILE: index.html
================================================
<!doctype html>
<html lang='en'>
<head>
<title>enter-view.js</title>
<meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no'>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<style>
* {
box-sizing: border-box;
}
html {
margin: 0;
padding: 0;
}
body {
margin: 0;
padding: 0;
width: 100%;
font-size: 17px;
font-family: Helvetica, sans-serif;
font-weight: 300;
}
p {
color: #333;
font-size: 1.2em;
line-height: 1.6;
max-width: 40em;
padding: 1em;
margin: 0 auto;
}
h1 {
font-weight: 300;
margin: 1em;
text-align: center;
}
p.description {
font-style: italic;
font-size: 1em;
text-align: center;
max-width: 100%;
}
article {
padding: 1em;
}
.prompt {
text-align: center;
height: 90vh;
}
.explain {
text-align: center;
margin-top: 50vh;
}
.blocks {
margin-bottom: 75vh;
}
.block {
width: 100%;
margin-top: 1em;
margin-bottom: 1em;
height: 25vh;
background: #ccc;
text-align: center;
transform-style: preserve-3d;
transition: 1.5s background;
}
.block p {
position: relative;
top: 50%;
transform: translateY(-50%);
font-size: 2em;
}
.block.entered {
background: #fff888;
}
.progress {
font-size: 0.75em;
}
pre {
max-width: 35rem;
margin: 0 auto;
background-color: #eee;
padding: 1rem;
}
</style>
</head>
<body>
<article>
<h1>Enter-view.js Demo</h1>
<p class='description'>Dependency-free JavaScript <a href='https://github.com/russellsamora/enter-view'>library</a> to detect when element enters into view</p>
<p class='prompt'>Start scrolling to see examples.</p>
<div class='blocks'>
<p class='explain'>Default (no offset)</p>
<code>
<pre>
enterView({
selector: '.block-a',
enter: function(el) {
el.classList.add('entered');
}
});
</pre>
</code>
<div class='block block-a'><p>.block-a</p></div>
<p class='explain'>50% offset</p>
<code>
<pre>
enterView({
selector: '.block-b',
offset: 0.5,
enter: function(el) {
el.classList.add('entered');
}
});
</pre>
</code>
<div class='block block-b'><p>.block-b</p></div>
<p class='explain'>75% offset, trigger every time</p>
<code>
<pre>
var count = 0;
enterView({
selector: '.block-c',
offset: '0.75,
enter: function(el) {
el.classList.add('entered');
count += 1;
el.querySelector('span').innerText = count;
}
});
</pre>
</code>
<div class='block block-c'><p>.block-c (<span>0</span>)</p></div>
<p class='explain'>Multiple elements, 50% offset</p>
<code>
<pre>
enterView({
selector: '.block-d',
offset: 0.5,
enter: function(el) {
el.classList.add('entered');
}
});
</pre>
</code>
<div class='block block-d'><p>.block-d</p></div>
<div class='block block-d'><p>.block-d</p></div>
<div class='block block-d'><p>.block-d</p></div>
<p class='explain'>Exit element, 50% offset</p>
<code>
<pre>
enterView({
selector: '.block-e',
offset: 0.5,
enter: function(el) {
el.classList.add('entered');
},
exit: function(el) {
el.classList.remove('entered');
},
});
</pre>
</code>
<div class='block block-e'><p>.block-e</p></div>
<p class='explain'>Progress increments, 50% offset</p>
<code>
<pre>
enterView({
selector: '.block-f',
offset: 0.5,
enter: function(el) {
el.classList.add('entered');
},
progress: function(el, progress) {
var p = el.querySelector('.progress')
p.innerText = progress.toFixed(2);
}
});
</pre>
</code>
<div class='block block-f'><p>.block-f <span class='progress'></span></p></div>
</div>
</article>
<script src='enter-view.min.js'></script>
<script>
enterView({
selector: '.block-a',
enter: function(el) {
console.log("entered", el);
el.classList.add('entered');
}
});
enterView({
selector: '.block-b',
offset: 0.5,
enter: function(el) {
el.classList.add('entered');
}
});
var count = 0;
enterView({
selector: '.block-c',
offset: 0.75,
enter: function(el) {
el.classList.add('entered');
count += 1;
el.querySelector('span').innerText = count;
},
});
enterView({
selector: '.block-d',
offset: 0.5,
enter: function(el) {
el.classList.add('entered');
}
});
enterView({
selector: '.block-e',
offset: 0.5,
enter: function (el) {
el.classList.add('entered');
},
exit: function (el) {
el.classList.remove('entered');
}
});
enterView({
selector: '.block-f',
offset: 0.5,
enter: function(el) {
el.classList.add('entered');
},
progress: function(el, progress) {
var p = el.querySelector('.progress')
p.innerText = progress.toFixed(2);
}
});
</script>
</body>
</html>
================================================
FILE: package.json
================================================
{
"name": "enter-view",
"version": "2.0.1",
"description": "Dependency-free JavaScript library to detect when element enters into view",
"main": "enter-view.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"enter",
"view",
"viewport",
"scroll",
"into",
"element"
],
"author": "Russell Samora",
"license": "MIT",
"devDependencies": {
"eslint": "^2.5.3",
"eslint-config-airbnb": "^6.2.0",
"eslint-plugin-react": "^4.2.3",
"terser": "^5.5.1"
},
"dependencies": {}
}
gitextract_ssz2htd4/ ├── .eslintrc ├── .gitignore ├── .nojekyll ├── LICENSE ├── README.md ├── enter-view.js ├── index.html └── package.json
SYMBOL INDEX (12 symbols across 1 files)
FILE: enter-view.js
function setupRaf (line 27) | function setupRaf() {
function getOffsetHeight (line 38) | function getOffsetHeight() {
function updateHeight (line 46) | function updateHeight() {
function updateScroll (line 52) | function updateScroll() {
function onScroll (line 96) | function onScroll() {
function onResize (line 103) | function onResize() {
function onLoad (line 108) | function onLoad() {
function selectionToArray (line 113) | function selectionToArray(selection) {
function selectAll (line 122) | function selectAll(selector, parent = document) {
function setupElements (line 132) | function setupElements() {
function setupEvents (line 136) | function setupEvents() {
function init (line 143) | function init() {
Condensed preview — 8 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (15K chars).
[
{
"path": ".eslintrc",
"chars": 168,
"preview": "{\n \"extends\": [\"airbnb-base\", \"prettier\"],\n \"plugins\": [\"prettier\"],\n \"rules\": {\n \"quotes\": [1, \"single\"],\n \"no"
},
{
"path": ".gitignore",
"chars": 40,
"preview": ".DS_Store\nnode_modules\npackage-lock.json"
},
{
"path": ".nojekyll",
"chars": 0,
"preview": ""
},
{
"path": "LICENSE",
"chars": 1080,
"preview": "The MIT License (MIT)\n\nCopyright (c) 2022 Russell Samora\n\nPermission is hereby granted, free of charge, to any person ob"
},
{
"path": "README.md",
"chars": 1994,
"preview": "# enter-view\n\nDependency-free JavaScript library to detect when element enters into view. [See demo](https://russellsamo"
},
{
"path": "enter-view.js",
"chars": 4025,
"preview": "/*\n * enter-view.js is library\n */\n\n(function(factory) {\n if (typeof define === 'function' && define.amd) {\n define("
},
{
"path": "index.html",
"chars": 5488,
"preview": "<!doctype html>\n<html lang='en'>\n\t<head>\n\t <title>enter-view.js</title>\n <meta name='viewport' content='width="
},
{
"path": "package.json",
"chars": 541,
"preview": "{\n\t\"name\": \"enter-view\",\n\t\"version\": \"2.0.1\",\n\t\"description\": \"Dependency-free JavaScript library to detect when element"
}
]
About this extraction
This page contains the full source code of the russellgoldenberg/enter-view GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 8 files (13.0 KB), approximately 3.7k tokens, and a symbol index with 12 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.