xxxxxxxxxxxxxscroll
by velo_ninja
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#redContainer {
position: relative;
width: 300px;
height: 900px;
overflow: auto;
border: 1px solid red;
}
.blueBox {
width: 150px;
height: 150px;
background-color: blue;
margin: 5px;
transition: opacity 0.5s ease-out;
}
</style>
<title>Scrollable Div with Blue Boxes</title>
</head>
<body>
<div id="redContainer"></div>
<script>
//---------------------------------------
function create_BlueBox() {
const blueBox = document.createElement('div');
blueBox.className = 'blueBox';
return blueBox;
}
//--------------------------------------
function add_Boxes(num, div) {
Array.from({length: num }, divBlue).forEach(element=> div.appendChild(element));
}
//--------------------------------------
function handleScroll(container, items) {
const redRect = container.getBoundingClientRect();
const offset = 50;
items.forEach(box => {
const boxRect = box.getBoundingClientRect();
const overlap = Math.max(0, Math.min(boxRect.right, redRect.right) - Math.max(boxRect.left, redRect.left)) *
Math.max(0, Math.min(boxRect.bottom, redRect.bottom) - Math.max(boxRect.top, redRect.top));
const overlapPercentage = (overlap / (boxRect.width * boxRect.height)) * 100;
const shouldAppear = overlapPercentage < 100;
box.style.opacity = overlapPercentage / 100;
box.classList.toggle('hidden', shouldAppear);
});
}
//---------------------------------------
</script>
<script>
const divRed = document.getElementById('redContainer');
const divBlue = create_BlueBox;
//---------------------------------------
add_Boxes(10, divRed);
//---------------------------------------
...