You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+40-4Lines changed: 40 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
scrollMonitor
2
2
=============
3
3
4
-
The scroll monitor allows you to receive events when elements enter or exit the viewport. It does this using watcher objects, which watch an element and trigger events. Watcher objects also contain information about the element they watch, including the element's visibility and location relative to the viewport.
4
+
The scroll monitor allows you to receive events when elements enter or exit a viewport. It does this using watcher objects, which watch an element and trigger events. Watcher objects also contain information about the element they watch, including the element's visibility and location relative to the viewport. If your scroll container is an element other than the body you can create a container that creates watchers.
5
5
6
6
The scroll monitor was designed to be very fast. On each scroll event the DOM is only touched twice, once to find the document height and again to find the viewport top. No variables are declared, nor are any objects, arrays, or strings created.
7
7
8
-
The code is based on vanilla javascript and has no external dependencies. Except if you want to put it in the head of the document, then you'll need jQuery for the DOMContentLoaded event.
8
+
The code is based on vanilla javascript and has no external dependencies. _The script cannot be put in the head_.
var containerElement =document.getElementById("container");
34
+
35
+
var containerMonitor =scrollMonitor.createContainer(containerElement);
36
+
// this containerMonitor is an instance of the scroll monitor
37
+
// that listens to scroll events on your container.
38
+
39
+
var childElement =document.getElementById("child-of-container");
40
+
var elementWatcher =containerMonitor.create(childElement);
41
+
42
+
elementWatcher.enterViewport(function() {
43
+
console.log( 'I have entered the viewport' );
44
+
});
45
+
elementWatcher.exitViewport(function() {
46
+
console.log( 'I have left the viewport' );
47
+
});
48
+
```
49
+
50
+
_Note: an element is said to be in the viewport if it is scrolled into its parent, it does not matter if the parent is in the viewport._
51
+
29
52
## Demos
30
53
31
54
*[Stress Test](http://stutrek.github.com/scrollMonitor/demos/stress.html) - Test with as many watchers as you'd like
55
+
*[Stress Test in a div](http://stutrek.github.com/scrollMonitor/demos/stressTestInADiv.html) - Note how much slower scrolling a div is than scrolling the body.
*`scrollMonitor.createContainer( containerEl )` - returns a new ScrollMonitorContainer that can be used just like the scrollMonitor module.
137
163
*`scrollMonitor.create( watchItem, offsets )` - Returns a new watcher. `watchItem` is a DOM element, jQuery object, NodeList, CSS selector, object with .top and .bottom, or a number.
138
164
*`scrollMonitor.update()` - update and trigger all watchers.
139
165
*`scrollMonitor.recalculateLocations()` - recalculate the location of all unlocked watchers and trigger if needed.
0 commit comments