我使用scrollama.js。
scrollama是一个插件,允许使用“相交观察器”使参考线居中。
现在(默认),我根据body
获得中心,
我想基于特定元素#cont
(=内容)获得中心。
我这样更改了scrollama.js的代码。
第213行
function handleResize() {
// viewH = window.innerHeight;
// pageH = getPageHeight();
viewH = document.getElementById("cont").offsetHeight; // change
pageH = document.getElementById("cont").offsetHeight; // change
offsetMargin = offsetVal * viewH;
if (isReady) {
stepOffsetHeight = stepEl.map(function (el) { return el.offsetHeight; });
stepOffsetTop = stepEl.map(getOffsetTop);
if (isEnabled) { updateIO(); }
}
if (isDebug) { update({ id: id, stepOffsetHeight: stepOffsetHeight, offsetMargin: offsetMargin, offsetVal: offsetVal }); }
}
现在,我可以得到#cont
,但参考线在顶部。(此虚线是用于scrollama的调试工具)
我认为,如果我更改了元素scrollama.js并像这样更改它,那将成为中心,但是我还需要更改吗?
我提取了在scrollama中使用viewH
和pageH
的所有位置。
function scrollama() {
//【Line 143~】
var viewH = 0;
var pageH = 0;
//【Line 213~】
function handleResize() {
viewH = window.innerHeight;
pageH = getPageHeight();
offsetMargin = offsetVal * viewH;
}
//【Line 471~】
// jump into viewport
function updateViewportAboveIO() {
io.viewportAbove = stepEl.map(function (el, i) {
var marginTop = pageH - stepOffsetTop[i]; // <- here
var marginBottom = offsetMargin - viewH - stepOffsetHeight[i]; // <- here
var rootMargin = marginTop + "px 0px " + marginBottom + "px 0px";
var options = { rootMargin: rootMargin };
// console.log(options);
var obs = new IntersectionObserver(intersectViewportAbove, options);
obs.observe(el);
return obs;
});
}
function updateViewportBelowIO() {
io.viewportBelow = stepEl.map(function (el, i) {
var marginTop = -offsetMargin - stepOffsetHeight[i];
var marginBottom = offsetMargin - viewH + stepOffsetHeight[i] + pageH; // <- here
var rootMargin = marginTop + "px 0px " + marginBottom + "px 0px";
var options = { rootMargin: rootMargin };
// console.log(options);
var obs = new IntersectionObserver(intersectViewportBelow, options);
obs.observe(el);
return obs;
});
}
// look above for intersection
function updateStepAboveIO() {
io.stepAbove = stepEl.map(function (el, i) {
var marginTop = -offsetMargin + stepOffsetHeight[i];
var marginBottom = offsetMargin - viewH; // <- here
var rootMargin = marginTop + "px 0px " + marginBottom + "px 0px";
var options = { rootMargin: rootMargin };
// console.log(options);
var obs = new IntersectionObserver(intersectStepAbove, options);
obs.observe(el);
return obs;
});
}
// look below for intersection
function updateStepBelowIO() {
io.stepAbove = stepEl.map(function (el, i) {
var marginTop = -offsetMargin;
var marginBottom = offsetMargin - viewH + stepOffsetHeight[i]; // <- here
var rootMargin = marginTop + "px 0px " + marginBottom + "px 0px";
var options = { rootMargin: rootMargin };
// console.log(options);
var obs = new IntersectionObserver(intersectStepBelow, options);
obs.observe(el);
return obs;
});
}
// progress progress tracker
function updateStepProgressIO() {
io.stepProgress = stepEl.map(function (el, i) {
var marginTop = stepOffsetHeight[i] - offsetMargin;
var marginBottom = -viewH + offsetMargin; // <- here
var rootMargin = marginTop + "px 0px " + marginBottom + "px 0px";
var threshold = createThreshold(stepOffsetHeight[i]);
var options = { rootMargin: rootMargin, threshold: threshold };
// console.log(options);
var obs = new IntersectionObserver(intersectStepProgress, options);
obs.observe(el);
return obs;
});
}
}