I'm trying to create something like list of options to select so when user scrolls or swipes he will see the option he was looking for.
But if I scroll once, it scrolls more than one position (e.g. I have element with text "1" on top, I scroll once and I get element with text "4" on top, but element with text "2" expected).
The code I have works well on IOS Chrome and Safari (tested on mobile and actually love it).
How to get the same result on desktop using CSS or JS (jQuery)? Or did I miss a mistake somewhere?
Here's basic HTML and CSS (better to check on full page). (HTML can be whatever is needed: divs inside div tag (below), or options inside select tag, or lis inside ul/ol tag)
.box {
background-color: yellowgreen;
height: 300px;
overflow: hidden;
}
.scroll {
width: 100%;
height: 100%;
background-color: aliceblue;
overflow-y: scroll;
scroll-snap-type: y mandatory;
scroll-snap-stop: always;
}
.scroll div {
display: flex;
align-items: center;
justify-content: center;
height: 40px;
margin: 5px 0;
padding: 10px;
background-color: lightpink;
scroll-snap-align: center;
}
.scroll div:first-of-type {
margin-top: calc((50% - 20px));
}
.scroll div:last-of-type {
margin-bottom: calc((50% - 20px));
}
<div class="box">
<div class="scroll">
<div>List item: 1</div>
<div>List item: 2</div>
<div>List item: 3</div>
<div>List item: 4</div>
<div>List item: 5</div>
<div>List item: 6</div>
<div>List item: 7</div>
<div>List item: 8</div>
<div>List item: 9</div>
<div>List item: 10</div>
</div>
</div>