Javascript 基于相对于其他元素的滚动深度追加类

Javascript 基于相对于其他元素的滚动深度追加类,javascript,Javascript,我试图根据滚动深度附加一个类,但是我在控制台中得到一个“getBoundingClientRect不是函数”,看不到任何效果 请看这里: 使用的JS: var leftSection=document.getElementsByClassName('js-detail-product'), leftSectionTop=leftSection.getBoundingClientRect().top, rightSection=document.getElementsByClassName('j

我试图根据滚动深度附加一个类,但是我在控制台中得到一个
“getBoundingClientRect不是函数”
,看不到任何效果

请看这里:

使用的JS:

var leftSection=document.getElementsByClassName('js-detail-product'),
leftSectionTop=leftSection.getBoundingClientRect().top,
rightSection=document.getElementsByClassName('js-product-info-main'),
rightSectionHeight=rightSection.getBoundingClientRect().height;
如果(leftSectionTop=rightSectionHeight){
rightSection.className=('inactive');
}
试试这个:

document.addEventListener(“DOMContentLoaded”,()=>{
//在此处插入所有代码
});请尝试以下操作:

document.addEventListener(“DOMContentLoaded”,()=>{
//在此处插入所有代码

});
getElementsByClassName
返回一个数组

试试这个:

leftSection=document.getElementsByClassName('js-detail-product')[0],
leftSectionTop=leftSection.getBoundingClientRect().top,
rightSection=document.getElementsByCassName('js-product-info-main')[0],
rightSectionHeight=rightSection.getBoundingClientRect().height;
或者为了更安全:

document.addEventListener(“DOMContentLoaded”,函数(){
allLeftSections=document.getElementsByClassName('js-detail-product');
如果(allLeftSections.length>0){
leftSection=所有leftSection[0],
leftSectionTop=leftSection.getBoundingClientRect().top,
}
allRightSection=document.getElementsByClassName('js-product-info-main');
如果(allRightSection.length>0){
rightSection=allRightSection[0],
rightSectionHeight=rightSection.getBoundingClientRect().height;
}
});

getElementsByClassName
返回一个数组

试试这个:

leftSection=document.getElementsByClassName('js-detail-product')[0],
leftSectionTop=leftSection.getBoundingClientRect().top,
rightSection=document.getElementsByCassName('js-product-info-main')[0],
rightSectionHeight=rightSection.getBoundingClientRect().height;
或者为了更安全:

document.addEventListener(“DOMContentLoaded”,函数(){
allLeftSections=document.getElementsByClassName('js-detail-product');
如果(allLeftSections.length>0){
leftSection=所有leftSection[0],
leftSectionTop=leftSection.getBoundingClientRect().top,
}
allRightSection=document.getElementsByClassName('js-product-info-main');
如果(allRightSection.length>0){
rightSection=allRightSection[0],
rightSectionHeight=rightSection.getBoundingClientRect().height;
}
});

Double check leftSection实际上是一个元素或它的未定义元素,因为它没有初始化。Double check leftSection实际上是一个元素或它的未定义元素,因为它没有初始化。它工作了!但总的来说,它并没有达到预期的效果,有什么想法吗?@WojciechŚwięch,如果没有演示代码,很难解决这个问题。你能创建一个吗?你能参考一下吗?@WojciechŚwięch,你想隐藏
js product info main
块吗?js product info main(右部分)默认位于固定位置。当用户滚动到js detail product(左部分)时,我想在其上附加一个类作为相对位置。它成功了!但总的来说,它并没有达到预期的效果,有什么想法吗?@WojciechŚwięch,如果没有演示代码,很难解决这个问题。你能创建一个吗?你能参考一下吗?@WojciechŚwięch,你想隐藏
js product info main
块吗?js product info main(右部分)默认位于固定位置。当用户滚动到js detail product(左侧部分)时,我想为它添加一个类以获得相对位置。