Skip to content
This repository was archived by the owner on Feb 1, 2020. It is now read-only.

Commit ddbce0d

Browse files
committed
Support variable scroll containers
1 parent 9a032eb commit ddbce0d

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

controllers/element-visible-controller.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,27 @@ function ElementVisibleController(elem, options) {
2727
this.buffer = Util.getOption(options.buffer, 0);
2828
this.isVisible = false;
2929
this.rect = null;
30+
this.scrollTarget = options.scrollTarget;
3031

3132
// Auto trigger if the last value on the stream is what we're looking for.
3233
var oldOn = this.on;
3334
this.on = function wrappedOn(eventName, callback, scope) {
3435
oldOn.apply(this, arguments);
35-
36+
3637
if (('enter' === eventName) && this.isVisible) {
3738
scope ? callback.call(scope) : callback();
3839
}
3940
};
4041

41-
var sc = new ScrollController();
42+
var sc = new ScrollController({
43+
scrollTarget: this.scrollTarget
44+
});
45+
4246
sc.on('scroll', this.didScroll, this);
4347
sc.on('scrollEnd', this.recalculatePosition, this);
4448

4549
Stream.onValue(ResizeStream.create(), Util.functionBind(this.didResize, this));
46-
50+
4751
this.viewportRect = {
4852
height: window.innerHeight,
4953
top: 0
@@ -68,7 +72,7 @@ ElementVisibleController.prototype.recalculateOffsets = function() {
6872
};
6973

7074
ElementVisibleController.prototype.recalculatePosition = function(currentScrollY) {
71-
currentScrollY || (currentScrollY = DOM.documentScrollY());
75+
currentScrollY || (currentScrollY = DOM.documentScrollY(this.scrollTarget && this.scrollTarget.parentNode));
7276

7377
this.rect = DOM.getRect(this.elem);
7478
this.rect.top += currentScrollY;
@@ -78,7 +82,7 @@ ElementVisibleController.prototype.recalculatePosition = function(currentScrollY
7882
};
7983

8084
ElementVisibleController.prototype.update = function(currentScrollY, ignoreCurrentVisibility) {
81-
currentScrollY || (currentScrollY = DOM.documentScrollY());
85+
currentScrollY || (currentScrollY = DOM.documentScrollY(this.scrollTarget && this.scrollTarget.parentNode));
8286

8387
this.viewportRect.top = currentScrollY;
8488

core/dom.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ var detectedIE10_ = (navigator.userAgent.indexOf('MSIE 10') !== -1);
3636

3737
/**
3838
* Get the document scroll.
39+
* @param {Element} targetElement - Optional target element.
3940
* @return {number}
4041
*/
41-
function documentScrollY() {
42+
function documentScrollY(targetElement) {
43+
if (targetElement && (targetElement !== window)) {
44+
return targetElement.scrollTop;
45+
}
46+
4247
if (detectedIE10_ && (window.pageYOffset != document.documentElement.scrollTop)) {
4348
return document.documentElement.scrollTop;
4449
}
@@ -59,7 +64,7 @@ function getRect(elem) {
5964
if (!elem || 1 !== elem.nodeType) {
6065
return false;
6166
}
62-
67+
6368
var bounds = elem.getBoundingClientRect();
6469

6570
return {
@@ -111,7 +116,7 @@ function insertBefore(before, elem) {
111116

112117
function detachElement(elem) {
113118
if (elem.parentNode) {
114-
elem.parentNode.removeChild(elem);
119+
elem.parentNode.removeChild(elem);
115120
}
116121
}
117122

core/responsive-image.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ function ResponsiveImage() {
1919
this.src = null;
2020
this.isFlexible = false;
2121
this.hasRetina = false;
22-
this.hasRunOnce = false;
2322
this.preserveAspectRatio = false;
2423
this.knownDimensions = null;
2524
this.hasLoaded = false;
25+
this.hasRunOnce = false;
2626
}
2727

2828
function create(imageMap) {
@@ -36,7 +36,12 @@ function create(imageMap) {
3636
}
3737

3838
if (image.lazyLoad) {
39-
image.observer = new ElementVisibleController(image.element);
39+
image.observer = new ElementVisibleController(
40+
image.element,
41+
{
42+
scrollTarget: imageMap.getScrollTarget(image.element)
43+
}
44+
);
4045

4146
image.observer.on('enter', function onEnter_() {
4247
if (!image.checkIfVisible(image)) { return; }
@@ -71,6 +76,9 @@ function createFromElement(elem, options) {
7176
preserveAspectRatio: Util.getOption(options.preserveAspectRatio, elem.getAttribute('data-preserveAspectRatio') === 'true'),
7277
checkIfVisible: Util.getOption(options.checkIfVisible, function() {
7378
return true;
79+
}),
80+
getScrollTarget: Util.getOption(options.getScrollTarget, function() {
81+
return window;
7482
})
7583
};
7684

streams/scroll-stream.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ var DOM = require('../core/dom');
44
var Events = require('../core/events');
55

66
/**
7-
* Create a stream of window.onscroll events, but only calculate their
7+
* Create a stream of onscroll events, but only calculate their
88
* position on requestAnimationFrame frames.
9+
* @param {Element=} options.scrollTarget - Targeted for scroll checking.
910
* @return {Stream}
1011
*/
11-
var create = Util.memoize(function create_() {
12+
function create(options) {
13+
options = options || {};
14+
15+
var scrollParent = options.scrollTarget ? options.scrollTarget.parentNode : window;
16+
1217
var oldScrollY;
1318
var scrollDirty = true;
14-
var scrollEventsStream = Stream.createFromEvents(window, 'scroll');
19+
var scrollEventsStream = Stream.createFromEvents(scrollParent, 'scroll');
1520

1621
Stream.onValue(scrollEventsStream, function onScrollSetDirtyBit_() {
1722
scrollDirty = true;
@@ -23,7 +28,8 @@ var create = Util.memoize(function create_() {
2328
if (!scrollDirty) { return false; }
2429
scrollDirty = false;
2530

26-
var newScrollY = DOM.documentScrollY();
31+
var newScrollY = DOM.documentScrollY(scrollParent);
32+
2733
if (oldScrollY !== newScrollY) {
2834
oldScrollY = newScrollY;
2935
return true;
@@ -33,11 +39,13 @@ var create = Util.memoize(function create_() {
3339
}, rAF);
3440

3541
// It's going to space, will you just give it a second!
36-
Util.defer(Util.partial(Events.dispatchEvent, window, 'scroll'), 10);
42+
Util.defer(Util.partial(Events.dispatchEvent, options.scrollTarget, 'scroll'), 10);
3743

3844
return Stream.map(function getWindowPosition_() {
3945
return oldScrollY;
4046
}, didChangeOnRAFStream);
41-
});
47+
};
4248

43-
module.exports = { create: create };
49+
module.exports = {
50+
create: create
51+
};

0 commit comments

Comments
 (0)