Javascript 滚动后如何将div固定到背景?

Javascript 滚动后如何将div固定到背景?,javascript,css,Javascript,Css,我试图使每个div在滚动到div的底部后都处于固定位置,这样div的底部仍然固定在屏幕的底部(但是,在以下元素后面) 我已经设法让效果对第一个div起作用,这样当第二个div进入视口时(由于滚动),效果就会变得固定,但是我似乎无法让下面的div复制相同的行为 任何帮助都将不胜感激!(我已经花了整整两天的时间来修补这个,呃) setInterval(函数stickyScrolling(){ var docViewTop=window.pageYOffset, docViewBottom=docV

我试图使每个div在滚动到div的底部后都处于固定位置,这样div的底部仍然固定在屏幕的底部(但是,在以下元素后面)

我已经设法让效果对第一个div起作用,这样当第二个div进入视口时(由于滚动),效果就会变得固定,但是我似乎无法让下面的div复制相同的行为

任何帮助都将不胜感激!(我已经花了整整两天的时间来修补这个,呃)

setInterval(函数stickyScrolling(){
var docViewTop=window.pageYOffset,
docViewBottom=docViewTop+window.innerHeight,
sec1Bottom=document.getElementById(“第1节”)。离视,
sec2Bottom=sec1Bottom+document.getElementById(“section-2”)。离视,
sec3Bottom=sec2Bottom+document.getElementById(“section-3”)。离视,
sec4Bottom=sec3Bottom+document.getElementById(“section-4”).offsetHeight;
如果(docViewBottom>=sec1Bottom){
document.getElementById(“section-1”).style.top=window.innerHeight-document.getElementById(“section-1”).offsetHeight+“px”;
document.getElementById(“第1节”).style.position=“固定”;
document.getElementById(“section-2”).style.marginTop=sec1Bottom+“px”;
}否则{
document.getElementById(“第1节”).style.position=“static”;
document.getElementById(“第2节”).style.marginTop=“0”;
}
}, 20);
.sections{
位置:相对位置;
}
#第1节{
z指数:1;
背景色:#f00;
}
#第2节{
z指数:2;
背景色:#0f0;
}
#第3节{
z指数:3;
背景色:#044;
}
#第4节{
z指数:4;
背景色:#440;
}

1佩伦特式居住者morbi tristique Sentecus和netus及malesuada以turpis egestas闻名。前庭侵权人、世仇、别有用心者、临时诉讼人、担保人。不要让你的自由人坐在那里。我的生命是永恒的。莫里斯
Placelat eleifend leo。他是一个聪明的人。威斯康辛州的前庭、调味品、康茂德维塔、奥纳雷西特、威斯康辛州。埃涅亚发酵液、elit eget tincidunt调味品、欧洛斯益智果、桑慈姑
埃尼姆·ac·杜伊。在turpis枕面部进行非enim治疗。乌特·费利斯。事实上,这是一个错误,是一个错误,是一个错误,是一个错误。阿利奎姆·埃拉特·帕特。Nam dui mi、tincidunt quis、accumsan porttitor、facilisis
卢克图斯,梅特斯
2佩伦特式居住者morbi tristique Sentecus和netus及malesuada以turpis egestas闻名。前庭侵权人、世仇、别有用心者、临时诉讼人、担保人。不要让你的自由人坐在那里。我的生命是永恒的。
莫里斯·普莱斯特拉特·埃利芬德·利奥

3佩伦特式居住者morbi tristique Sentecus和netus及malesuada以turpis egestas闻名。前庭侵权人、世仇、别有用心者、临时诉讼人、担保人。不要让你的自由人坐在那里。我的生命是永恒的。 莫里斯·普莱斯特拉特·埃利芬德·利奥。他是一个聪明的人。威斯康辛州的前庭、调味品、康茂德维塔、奥纳雷西特、威斯康辛州。埃涅亚发酵液、elit eget tincidunt调味品、欧洛斯益智果、甜慈姑 拉卡斯·埃尼姆酒后驾车。在turpis枕面部进行非enim治疗。乌特·费利斯。事实上,这是一个错误,是一个错误,是一个错误,是一个错误。阿利奎姆·埃拉特·帕特。Nam dui mi、tincidunt quis、accumsan porttitor、, 美托斯卢克图斯面肌

4佩伦特式居住者morbi tristique Sentecus和netus及malesuada以turpis egestas闻名。前庭侵权人、世仇、别有用心者、临时诉讼人、担保人。不要让你的自由人坐在那里。我的生命是永恒的。 莫里斯·普莱斯特拉特·埃利芬德·利奥

5佩伦特式居住者morbi tristique Sentecus和netus及malesuada以turpis egestas闻名。前庭侵权人、世仇、别有用心者、临时诉讼人、担保人。不要让你的自由人坐在那里。我的生命是永恒的。 莫里斯·普莱斯特拉特·埃利芬德·利奥。他是一个聪明的人。威斯康辛州的前庭、调味品、康茂德维塔、奥纳雷西特、威斯康辛州。埃涅亚发酵液、elit eget tincidunt调味品、欧洛斯益智果、甜慈姑 拉卡斯·埃尼姆酒后驾车。在turpis枕面部进行非enim治疗。乌特·费利斯。事实上,这是一个错误,是一个错误,是一个错误,是一个错误。阿利奎姆·埃拉特·帕特。Nam dui mi、tincidunt quis、accumsan porttitor、, 美托斯卢克图斯面肌

佩伦特式居住者morbi tristique Sentecus et netus et malesuada以turpis egestas闻名。前庭侵权人、世仇、别有用心者、临时诉讼人、担保人。不要让你的自由人坐在那里。我的生命是永恒的。 莫里斯·普莱斯特拉特·埃利芬德·利奥

佩伦特式居住者morbi tristique Sentecus et netus et malesuada以turpis egestas闻名。前庭侵权人、世仇、别有用心者、临时诉讼人、担保人。不要让你的自由人坐在那里。我的生命是永恒的。 莫里斯·普莱斯特拉特·埃利芬德·利奥。他是一个聪明的人。威斯康辛州的前庭、调味品、康茂德维塔、奥纳雷西特、威斯康辛州。埃涅亚发酵液、elit eget tincidunt调味品、欧洛斯益智果、甜慈姑 拉卡斯·埃尼姆酒后驾车。在turpis枕面部进行非enim治疗。乌特·费利斯。事实上,这是一个错误,是一个错误,是一个错误,是一个错误。阿利奎姆·埃拉特·帕特。Nam dui mi、tincidunt quis、accumsan porttitor、, 美托斯卢克图斯面肌

佩伦特式居住者morbi tristique Sentecus et netus et malesuada以turpis egestas闻名。前庭肌
setInterval(function stickyScrolling() {
'use strict';

var docViewTop = window.pageYOffset,
    docViewBottom = docViewTop + window.innerHeight,

    sec1Bottom = document.getElementById("section-1").offsetHeight,
    sec2Bottom = sec1Bottom + document.getElementById("section-2").offsetHeight,
    sec3Bottom = sec2Bottom + document.getElementById("section-3").offsetHeight,
    sec4Bottom = sec3Bottom + document.getElementById("section-4").offsetHeight;

if (docViewBottom >= sec3Bottom) {
    document.getElementById("section-3").style.bottom = "0";
    document.getElementById("section-3").style.position = "fixed";
    document.getElementById("section-4").style.marginTop = sec3Bottom + "px";
} else if (docViewBottom >= sec2Bottom) {
    document.getElementById("section-2").style.bottom = "0";
    document.getElementById("section-2").style.position = "fixed";
    document.getElementById("section-3").style.marginTop = sec2Bottom + "px";        
    document.getElementById("section-3").style.position = "relative";
} else if (docViewBottom >= sec1Bottom) {
    document.getElementById("section-1").style.bottom = "0";
    document.getElementById("section-1").style.position = "fixed";
    document.getElementById("section-2").style.marginTop = sec1Bottom + "px";
    document.getElementById("section-2").style.position = "relative";
} else {
    document.getElementById("section-1").style.position = "static";
    document.getElementById("section-1").style.top = "auto";
    document.getElementById("section-2").style.marginTop = "0";
}