window.addEventListener('resize', function(event) { console.log('变了');}, true);
window.addEventListener('scroll', function () {
var stickyElements = document.querySelectorAll('.sticky');
stickyElements.forEach(function (element) {
// 计算元素距离其父元素顶部的距离
var parentTop = element.parentElement.getBoundingClientRect().top;
var parentBottom = element.parentElement.getBoundingClientRect().bottom;
var elementTop = element.getBoundingClientRect().top;
var elementBottom = element.getBoundingClientRect().bottom;
var offset = elementTop - parentTop;
console.log(parentTop,elementBottom,parentBottom);
// 当父元素滚动到顶部时,设置sticky定位
if (parentTop < 0 && elementBottom < parentBottom) {
element.style.position = 'sticky';
//element.style.top = offset + 'px';
element.style.top ='0px';
} else {
element.style.position = 'relative';
}
});
var stickyElementsTable = document.querySelectorAll('.Room-Table-Box');
stickyElementsTable.forEach(function (element) {
// 计算元素距离其父元素顶部的距离
var parentTop = element.parentElement.getBoundingClientRect().top;
var parentBottom = element.parentElement.getBoundingClientRect().bottom;
var elementTop = element.getBoundingClientRect().top;
var elementBottom = element.getBoundingClientRect().bottom;
var offset = elementTop - parentTop;
console.log(parentTop,elementBottom,parentBottom);
// 当父元素滚动到顶部时,设置sticky定位
if (parentTop < 0) {
element.style.position = 'fixed';
//element.style.top = offset + 'px';
element.style.top ='100px';
} else {
element.style.position = 'relative';
element.style.top ='0px';
}
});
},true);