I'm practicing by making this website - https://backstagetalks.com/ I created divs that are scrollable and made it such that each generates a random color.
However I want to know how to use Javascript to identify when a particular div has snapped to the center so that it can set the background color to match the color of that div
I tried to use the scrollX, scrollY and offSetTop, offsetHeight properties. But the divs all have fixed offsetHeight, so it didn't work
I don't know jquery yet and I haven't started learning CSS or JS libraries/frameworks, so I don't know how to use those to help.
This is the Code:
const gra = function (min, max) {
return Math.random() * (max - min) + min;
};
const init = function () {
let items = document.querySelectorAll(".scroller div");
for (let i = 0; i < items.length; i++) {
items[i].style.minHeight = gra(100, 100) + "vh";
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
items[i].style.backgroundColor = "#" + randomColor;
let itemColor = items[i].style.backgroundColor;
// items[i].style.borderColor = "#" + ((1 << 24) * Math.random() | 0).toString(16);
}
}
init();
@import url('https://fonts.googleapis.com/css2?family=Bangers&family=Chivo+Mono:ital,wght@1,500&family=Hanalei+Fill&family=Fredoka+One&display=swap');
html, body{
margin: 0;
/* font-family: Arial, Helvetica, sans-serif; */
}
::-webkit-scrollbar {
display: none;
}
header {
padding: 0.7em 0 0 0 ;
color: red;
font-family: 'Bangers';
font-size: 25px;
}
header h2 {
margin: 0;
}
.container {
height: 100vh;
display: flex;
justify-content: space-between;
/* border: 2px solid brown; */
}
.scroller {
max-height: 100vh;
overflow: scroll;
scroll-snap-type: y mandatory;
margin: 0 auto;
width: 100%;
position: absolute;
scroll-behavior: smooth;
z-index: 1;
}
.book {
/* border: 20px solid; */
margin: 0 auto;
/* padding: 1em; */
width: 300px;
scroll-snap-align: center;
scroll-padding-top: 300px;
display: flex;
flex-flow: column;
justify-content: center;
text-align: center;
font-size: 4em;
}
.links {
display: flex;
justify-content: space-between;
flex-direction: column;
/* padding: 0 1em; */
}
.links h2 {
font-family: 'Fredoka One';
font-weight: 100;
padding: 0;
}
.issue-list {
width: fit-content;
margin-left: auto;
font-family: 'Arial';
font-size: 18px;
z-index: 1;
}
.issue-list p{
margin: 0;
}
.issue-list p a {
cursor: pointer;
text-decoration: none;
color: black;
}
.info {
position: absolute;
bottom: 0;
/* padding: 0 1em; */
}
.info p {
font-size: 19px;
font-weight: bold;
font-family: 'Chivo Mono';
}
<div id="background" class="container">
<header>
<h2>BACKSTAGE TALKS</h2>
</header>
<div class="info">
<p> Backstage Talks is a magazine <br>
casual, but in depth dialogues <br>
on design and business. Our decisions <br>
shape and influence this complex <br>
world—to have a chance to make the <br>
right ones, we need to talk </p>
</div>
<div id="scroll-box" class="scroller">
<div class="book" id="book_issue-1">1</div>
<div class="book" id="book_issue-2">2</div>
<div class="book" id="book_issue-3">3</div>
<div class="book" id="book_issue-4">4</div>
<div class="book" id="book_issue-5">5</div>
<div class="book" id="book_issue-6">6</div>
</div>
<div class="links">
<h2>info@backstagetalks.com</h2>
<div class="issue-list">
<p id="issue1"><a href="#book_issue-1">Issue #1</a></p>
<p id="issue2"><a href="#book_issue-2">Issue #2</a></p>
<p id="issue3"><a href="#book_issue-3">Issue #3</a></p>
<p id="issue4"><a href="#book_issue-4">Issue #4</a></p>
<p id="issue5"><a href="#book_issue-5">Issue #5</a></p>
<p id="issue6"><a href="#book_issue-6">Issue #6</a></p>
</div>
</div>
</div>