I have a simple slider I've put together using the newish CSS Scroll Snap module.
Now I'm stuck with how to show the user what page they're on. In Javascript one typically does this like this or via bootstrap like this... but I'm scratching my head how to detect and display the index of the current visible slide given that it's only a CSS implementation thus far. Any ideas? Or do I have to revert to a js slider?
payload = [{
"url": "https://unsplash.it/801?random",
"filter": "nashville"
}, {"url": "https://unsplash.it/802?random",
"filter": "aden"
}, {
"url": "https://unsplash.it/803?random",
"filter": "mayfair"
}, {
"url": "https://unsplash.it/804?random",
"filter": "lofi"
}, {
"url": "https://unsplash.it/805?random",
"filter": "kelvin"
}, {
"url": "https://unsplash.it/806?random",
"filter": "mayfair"
}]
const init = function(){
var slider = document.getElementById("slider");
for (let i = 0; i < payload.length; i++){
slider.innerHTML += "<section><figure class='" + payload[i].filter + "'><img src='" + payload[i].url + "' /> </figure></section>";
}
//cssScrollSnapPolyfill()
}
init();
img {
display: inline-block;
height: 98vh;
-o-object-fit: cover;
object-fit: cover;
width: 100vw;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
overflow-y: hidden;
}
.slider {
font-family: sans-serif;
-ms-scroll-snap-type: x mandatory;
scroll-snap-type: x mandatory;
display: flex;
-webkit-overflow-scrolling: touch;
overflow-x: scroll;
}
section {
min-width: 100vw;
height: 100vh;
scroll-snap-align: start;
scroll-snap-stop: always;
text-align: center;
position: relative;
}
.pagination {
display: inline-block;
position: fixed;
background: rgba(33, 33, 33, 0.9);
color: #fff;
text-align: center;
border-radius: 13px;
z-index: 4;
width: 70px;
top: 20px;
right: 20px;
}
span {
display: inline-block;
line-height: 28px;
vertical-align: middle;
}
<div class="slider" id="slider">
<div class='pagination'> <span>1 / 5</span></div>
</div>