How to scroll every 4 div specifically, with side scrolling buttons?
On a previous question I asked about how to loop and simplify codes of multiple side scrolling buttons in every overflow-x: How to loop and simplify code of multiple side scrolling buttons that scrolls specific row in javascript?
Now with this answered by Elna Haim, I am asking for help, to anyone knowledgeable in javascript on how is it possible to scroll every 4 div specifically and specifically stops and fits the 4 boxes in it when next and back button is clicked.
It can scroll using data-scroll="1280" but every time the screen size changes the scrolling gets broken too, and you will be able to see the div getting cut in half on the code below.
Also there’s a problem in the margin not triggering in the code snippet, I don’t know why.
const nextbtns = document.querySelectorAll('.next')
const backbtns = document.querySelectorAll('.back')
for (let nxt of nextbtns) {
nxt.addEventListener("click", () => {
const con = nxt.getAttribute("data-con");
const target = nxt.getAttribute("data-scroll");
document.querySelector(`#${con}`).scrollLeft += parseInt(target, 10);
});
}
for (let bck of backbtns) {
bck.addEventListener("click", () => {
const con = bck.getAttribute("data-con");
const target = bck.getAttribute("data-scroll");
document.querySelector(`#${con}`).scrollLeft -= parseInt(target, 10);
});
}
.row {
width: 100%;
height: 270px;
overflow-x: hidden;
-ms-overflow-style: none;
scrollbar-width: none;
}
.container {
overflow-x: hidden;
white-space: nowrap;
-ms-overflow-style: none;
scrollbar-width: none;
scroll-behavior: smooth;
transition: scroll-behavior .5s ease-in-out;
}
.box {
width: 24%;
height: 180px;
background-color: #171717;
border-radius: 20px;
display: inline-block;
margin-right: 14;
}
.btns button {
--color: #202020;
background-color: #202020;
padding: 8px 17.5px 12px 17.5px;
border: 3px solid var(--color);
color: grey;
border-radius: 50px;
font-family: 'Arial Black';
position: relative;
bottom: 0px;
}
<html>
<body>
<div class="row">
<a class="btns">
<button type="button" class="back" data-con="con" data-scroll="1321">‹</button>
<button type="button" class="next" data-con="con" data-scroll="1321">›</button>
</a>
<div class="container" id="con">
<center>
<div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div>
</center>
</div>
</div>
</body>
</html>
For the jsfiddle: https://jsfiddle.net/c0f9da82/
The margin is still not working I guess it would only work if codes are saved locally.
Thanks Alot to Elna Haim for answering the previous question and is looking to answer this question again! If anyone else can help answer the question it would be Greatly appreciated!
Also I have another question in: Anyone around Good at both Javascript and Youtube api?, I am using lite-youtube js and I am confused in adding an eventlistener for onStateChange Where I am using lite youtube js by paulirish on github, and asked how is it possible to create an event listener to get the onStateChange end parameter. Thank you very much in advance! to everyone looking to answer my questions!
2
Answers
Instead of scroll, you can replace the visible cards with next 4 cards. I have set different color to each card so you can see it in action. Basically, I am storing all the cards in an array, and appending 4 cards at a time according to the pressed button.
One way to achieve it, is to convert your code to work as a mini library. and to make sure you are handling each row separtly (common way to work with sliders / swipers).
in order to achieve it we need to modify the code a little:
HTML:
each row will be a "stand alone component", and now it will look like this:
Very important each row must have a unique id and each container must have the default page attribute (this will define the page we show by default).
the css also need to be improved, we will use display flex, and gap in the container. and on the box we will apply min-width:
And now the js. its a bit different from what you have currently, but i built it on top of the exist code.
here is a working codesandbox:
https://codesandbox.io/p/sandbox/gallant-payne-kmlzvm
**NOTE: we dont implement any lazy load here. means the browser will load all the rows and all the content at once. might be bad for performace. you need to take it into concideration.
This is a basic implementation of slider library, you can develop it more for your needs.
All together: