Skip to content

Commit 774d291

Browse files
committed
scroll-stop
1 parent 144c288 commit 774d291

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

100-maps/scroll-stop/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width">
6+
<title>Lazy Load Test</title>
7+
8+
<link rel="stylesheet" href="https://js.arcgis.com/4.14/esri/themes/light/main.css">
9+
<link rel="stylesheet" href="main.css">
10+
<script src="https://js.arcgis.com/4.14/"></script>
11+
<script src="main.js" type="module"></script>
12+
</head>
13+
<body>
14+
<div id="wrapperDiv"></div>
15+
</body>
16+
</html>

100-maps/scroll-stop/main.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
html,
2+
body {
3+
padding: 0;
4+
margin: 0;
5+
height: 100%;
6+
width: 100%;
7+
}
8+
9+
.map {
10+
background-color: lightgray;
11+
margin: 5px;
12+
float: left;
13+
}
14+
15+
.map, .map > div {
16+
width: 500px;
17+
height: 500px;
18+
}
19+
.intersected {
20+
background-color: green;
21+
}

100-maps/scroll-stop/main.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { loadModules } from "https://unpkg.com/esri-loader/dist/esm/esri-loader.js";
2+
3+
4+
// scrollStop from https://vanillajstoolkit.com/helpers/scrollstop/
5+
var scrollStop = function (callback) {
6+
// Make sure a valid callback was provided
7+
if (!callback || typeof callback !== 'function') return;
8+
// Setup scrolling variable
9+
var isScrolling;
10+
// Listen for scroll events
11+
window.addEventListener('scroll', function (event) {
12+
// Clear our timeout throughout the scroll
13+
window.clearTimeout(isScrolling);
14+
// Set a timeout to run after scrolling ends
15+
isScrolling = setTimeout(function() {
16+
// Run the callback
17+
callback();
18+
}, 66);
19+
}, false);
20+
};
21+
22+
const isElementOutViewport = (el) => {
23+
var rect = el.getBoundingClientRect();
24+
return rect.bottom < 0 || rect.right < 0 || rect.left > window.innerWidth || rect.top > window.innerHeight;
25+
}
26+
27+
const createMap = async (element) => {
28+
var childElement = document.createElement("div");
29+
element.appendChild(childElement);
30+
// More info on esri-loader's loadModules function:
31+
// https://github.com/Esri/esri-loader#loading-modules-from-the-arcgis-api-for-javascript
32+
const [Map, SceneView] = await loadModules([
33+
"esri/Map",
34+
"esri/views/SceneView"
35+
], {css: true});
36+
37+
const map = new Map({
38+
basemap: "streets"
39+
});
40+
41+
const viewOptions = {
42+
container: childElement,
43+
map: map,
44+
center: [-101.17, 21.78],
45+
zoom: 2
46+
};
47+
48+
// 3D:
49+
new SceneView(viewOptions);
50+
};
51+
52+
53+
window.addEventListener("load", (event) => {
54+
55+
const allNodes = [];
56+
57+
const checkAllNodes = () => {
58+
allNodes.forEach((node) => {
59+
if (isElementOutViewport(node)) {
60+
if(node.classList.contains('intersected')) {
61+
node.classList.remove('intersected');
62+
while(node.firstChild) {
63+
node.removeChild(node.firstChild);
64+
}
65+
}
66+
} else {
67+
if(!node.classList.contains('intersected')) {
68+
node.classList.add('intersected');
69+
createMap(node);
70+
}
71+
72+
}
73+
});
74+
};
75+
76+
const rootElement = document.getElementById("wrapperDiv");
77+
for(let i = 0; i < 100; i++) {
78+
var element = document.createElement("div");
79+
element.classList.add('map');
80+
rootElement.appendChild(element);
81+
allNodes.push(element);
82+
}
83+
84+
scrollStop(function () {
85+
console.log('Scrolling has stopped.');
86+
checkAllNodes();
87+
});
88+
89+
checkAllNodes(); // once on page load
90+
}, false);
91+
92+

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ Test of loading 100 map controls on a single page.
1414

1515
- [control](https://gavinr.github.io/lazy-load-arcgis-js-api-experiments/100-maps/control/)
1616
- No lazy loading
17-
- on most browsers last n maps load because only n WebGLRenderingContexts allowed
18-
- [intersection-observer](https://gavinr.github.io/lazy-load-arcgis-js-api-experiments/100-maps/intersection-observer/) (Lazy Loading with Intersection Observer)
17+
- On most browsers last n maps load because only n WebGLRenderingContexts allowed
18+
- [intersection-observer](https://gavinr.github.io/lazy-load-arcgis-js-api-experiments/100-maps/intersection-observer/)
19+
- Lazy Loading with Intersection Observer
20+
- [scroll-stop](https://gavinr.github.io/lazy-load-arcgis-js-api-experiments/100-maps/scroll-stop/)
21+
- Lazy Loading by checking which maps are in view when scrolling stops

0 commit comments

Comments
 (0)